Free Support Forum - aspose.com

How to Highlight Text in PDF using C# and Aspose.PDF for .NET

You can highlight text in PDF by using Aspose.PDF for .NET and HighlightAnnotation Class in it. This Class has a Property i.e. Color which will be used to highlight text in a PDF document. Once an existing PDF is loaded in the Document object, you will be able to access text on its pages. In order to highlight text in PDF as per your desire, you need to search the target text or word so that HighlightAnnotation can be applied to it. The following code snippet can be used to carry out the complete functionality:

Highlight Text in PDF using C# and Aspose.PDF for .NET

// Load an existing PDF file in which you want to highlight text
Document doc = new Document("PDF_for_Highlighting.pdf");
// Search target text to highlight
TextFragmentAbsorber tfa = new TextFragmentAbsorber("quick brown fox");
doc.Pages[1].Accept(tfa);
// Create a highlight annotation
HighlightAnnotation ha = new HighlightAnnotation(doc.Pages[1], tfa.TextFragments[1].Rectangle);
// Specify highlight color
ha.Color = Color.Yellow;
// Add annotation to highlight text in PDF
doc.Pages[1].Annotations.Add(ha);
// Save the document
doc.Save("PDF_with_Highlighted_Text.pdf");

You can notice in the below screenshot that the target text i.e. “quick brown fox” has been highlighted with the same color which was used in the above code snippet to highlight text in PDF.

More code samples and examples based on different scenarios can also be found in Aspose.PDF forum which can help you in implementing functionality to highlight text in PDF using C# and Aspose.PDF for .NET. The following search result is based upon the similar feature discussed in this topic:

In order to add different types of annotations in a PDF document, please feel free to check the “PDF Annotations” Section in the API Documentation.