Find and Highlight Text with Custom Color in PDF in C#, VB.NET

Find function is essential to any PDF reader, which makes it easy for user to locate the specified content in a PDF document. In this post, I’ll show you how to find and highlight searched text with custom color programmatically using .NET PDF library with C# and VB.NET. By using this component, you can also create the find and highlight feature in your own .NET application.

Step 1: Download Spire.PDF and add following namespace to your own project.

[C#]

using Spire.Pdf;

using Spire.Pdf.General.Find;

using System.Drawing;

[VB.NET]

Imports Spire.Pdf

Imports Spire.Pdf.General.Find

Imports System.Drawing

Step 2: Use following code snippet to find and highlight text in the PDF document.

[C#]

PdfDocument pdf = new PdfDocument(@”sample.pdf”);

PdfTextFind[] result = null;

foreach (PdfPageBase page in pdf.Pages)

{

    result = page.FindText(“SQL”).Finds;

    foreach (PdfTextFind find in result)

    {

        find.ApplyHighLight(Color.BlueViolet);

    }

}

pdf.SaveToFile(“result.pdf”);

System.Diagnostics.Process.Start(“result.pdf”);

[VB.NET]

Dim pdf As New PdfDocument(“sample.pdf”)

Dim result As PdfTextFind() = Nothing

For Each page As PdfPageBase In pdf.Pages

    result = page.FindText(“SQL”).Finds

    For Each find As PdfTextFind In result

        find.ApplyHighLight(Color.BlueViolet)

    Next

Next

pdf.SaveToFile(“result.pdf”)

System.Diagnostics.Process.Start(“result.pdf”)

Output

result-find&highlight

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s