How to highlight text in WinForms 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.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to highlight text in WinForms PDF using C# and VB.NET.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!