How to highlight text in a PDF using C# and VB.NET
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can highlight text in PDF document using C# and VB.NET.
Steps to highlight text in PDF programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Parsing; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Interactive Imports Syncfusion.Pdf.Parsing Imports System.Drawing
- Use the following code snippet to highlight text in PDF document.
C#
//Loads the document PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf"); //Gets the first page from the document PdfLoadedPage page = document.Pages[0] as PdfLoadedPage; //Add a list to maintain extracted text information List<TextData> extractedText = new List<TextData>(); //Extract text from first page page.ExtractText(out extractedText); foreach (TextData textData in extractedText) { if (textData.Text == "Hello World") { //Add text markup annotation on the bounds of highlighting text PdfTextMarkupAnnotation textmarkup = new PdfTextMarkupAnnotation(textData.Bounds); //Sets the markup annotation type as HighLight textmarkup.TextMarkupAnnotationType = PdfTextMarkupAnnotationType.Highlight; //Sets the content of the annotation textmarkup.Text = "Highlight Text"; //Sets the highlighting color textmarkup.TextMarkupColor = new PdfColor(Color.LightPink); //Add annotation into page page.Annotations.Add(textmarkup); } } //Save and close the document document.Save("HighlightText.pdf"); document.Close(true);
VB.NET
'Loads the document Dim document As New PdfLoadedDocument("Input.pdf") 'Gets the first page from the document Dim page As PdfLoadedPage = TryCast(document.Pages(0), PdfLoadedPage) 'Add a list to maintain extracted text information Dim extractedText As New List(Of TextData)() 'Extract text from first page page.ExtractText(extractedText) For Each textData As TextData In extractedText If textData.Text = "Hello World" Then 'Add text markup annotation on the bounds of highlighting text Dim textmarkup As New PdfTextMarkupAnnotation(textData.Bounds) 'Sets the markup annotation type as HighLight textmarkup.TextMarkupAnnotationType = PdfTextMarkupAnnotationType.Highlight 'Sets the content of the annotation textmarkup.Text = "Highlight Text" 'Sets the highlighting color textmarkup.TextMarkupColor = New PdfColor(Color.LightPink) 'Add annotation into page page.Annotations.Add(textmarkup) End If Next 'Save and close the document document.Save("HighlightText.pdf") document.Close(True)
A complete working sample can be downloaded from HighlightTextInPDF.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for working with annotations, where you can find other options like add, delete, and modify the annotation from the PDF document.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to adding various types of annotation to PDF document.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.