Follow this step‑by‑step tutorial to learn how to search for text and highlight in PDF using C#, guiding you through loading a PDF document, locating specific text, creating highlight annotations in various colors and shapes, and applying those annotations to emphasize the text—all without installing any third‑party software. It’s the perfect guide for developers who want to improve PDF readability or draw attention to important information using native C# tools and techniques.
Steps to Search Text and Highlight in PDF using C#
- Configure Conholdate.Total for .NET library from the NuGet package manager to highlight any text in PDF
- Load the input PDF file with the Document class object to search some text and highlight it
- Search text in the PDF to highlight
- Create HighlightAnnotation while specifying the color and shape
- Apply the highlight annotation to the searched text
- Save the output PDF file with highlighted text
These instructions detail the straightforward method to highlight text in PDF using C#. The process begins with searching for a particular string within the document. Once located, you can then proceed to highlight this text. Additionally, there’s the option to select the color for highlighting, such as yellow, red, or any other preferred color, and to choose the shape of the highlight to meet your specific needs. This flexibility allows for a customized approach to emphasizing key portions of text in your PDF documents, enhancing both the visual appeal and the functional utility of the highlighted content.
Code to Highlight Words in PDF using C#
This code sample shows how to highlight text in a PDF using C#. It starts by using TextFragmentAbsorber to locate the desired string on the specified page. Then a HighlightAnnotation is created, where you define the highlight’s color, opacity, and other properties. After configuring the annotation, it is applied to the found text, producing the exact highlight you defined. Finally, the document is saved, leaving the selected text prominently highlighted for quick identification and reference.
We’ve walked you through highlighting specific text in a PDF using C#. Ready to move on and extract PDF content programmatically? Dive into our detailed guide on how to Read PDF in C#.