C # How to highlight text in different colors in PDF,
Highlighted text helps readers quickly and effectively obtain key information of articles. In a PDF file, different texts, keywords, sentences, and other texts in different colors are highlighted, so that readers can effectively distinguish the meanings of different highlighted texts during reading. In the following example, I use Free Spire. PDF for. NET for operations (https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-NET.html), you can refer to the following steps:
The original file is as follows:
Step 1: Initialize and loadPDFDocument instance
PdfDocument pdf = new PdfDocument(@"C:\Users\Administrator\Desktop\C#.pdf");
Step 2: Call FindText () to find the specified text "C #"
PdfTextFind[] result1 = null;result1 = pdf.Pages[0].FindText("C#").Finds;
Step 3. traverse all search results and highlight the text in blue"C #"
foreach (PdfTextFind find in result1) { find.ApplyHighLight(Color.Blue); }
Step 4: Call FindText () to find the specified text "Microsoft"
Optional textfind [] result2 = null; result2 = pdf. Pages [0]. FindText ("Microsoft"). Finds;
Step 5: traverse all search results and highlight the text in purple"Microsoft"
foreach (PdfTextFind find in result2){ find.ApplyHighLight(Color.Purple)}
Step 6: Save the document
pdf.SaveToFile("result.pdf", FileFormat.PDF);
After completing the preceding operations, Debug and run the project and generate a document (which can be viewed in the bin> Debug folder of the project), as shown in:
The above is a simple operation on C # text search and highlighting in PDF. I hope it will help you. Thank you for reading!