Highlight Searched Texts with Different Colors in C#/VB.NET

In our previous article, we have demonstrated how to find and highlight selected text in PDF. But at sometimes, you may want different parts to be highlighted with different background colors. To help users change the highlight color easily, our develop team has created and added a new method ApplyHighLight(Color highlightColor) in Spire.PDF 3.5 and above. In the following section, a simple example is made to show how this function works.

Code Snippet

Step 1: Initialize a new object of PdfDocument class and load the test file.

PdfDocument pdf = new PdfDocument("test.pdf");

Step 2: Call FindText() method to find specified text and return the results in PdfTextFind[] array. Highlight the searched texts with the specified background color using ApplyHighLight(Color highlightColor). Following code will highlight "Spire.PDF" with green.

PdfTextFind[] result1 = null;
result1 = pdf.Pages[0].FindText("Spire.PDF").Finds;
foreach (PdfTextFind find in result1)
{
    find.ApplyHighLight(Color.Green);
}

Step 3: Repeat step 2 to highlight ".NET" with red.

PdfTextFind[] result2 = null;
result2 = pdf.Pages[0].FindText(".NET").Finds;
foreach (PdfTextFind find in result2)
{
    find.ApplyHighLight(Color.Red);
}

Step 4: Save the file and launch to review.

pdf.SaveToFile("result.pdf",FileFormat.PDF);
System.Diagnostics.Process.Start("result.pdf");

Output:

Highlight Searched Texts with Different Colors in C#, VB.NET

Full Code:

[C#]
using Spire.Pdf;
using Spire.Pdf.General.Find;
using System.Drawing;


namespace HighlightText
{
    class Program
    {
        static void Main(string[] args)
        {
            //load test file
            PdfDocument pdf = new PdfDocument("test.pdf");
            //highlight searched text with Green
            PdfTextFind[] result1 = null;
            result1 = pdf.Pages[0].FindText("Spire.PDF").Finds;
            foreach (PdfTextFind find in result1)
            {
                find.ApplyHighLight(Color.Green);
            }
            //highlight searched text with Red
            PdfTextFind[] result2 = null;
            result2 = pdf.Pages[0].FindText(".NET").Finds;
            foreach (PdfTextFind find in result2)
            {
                find.ApplyHighLight(Color.Red);
            }
            //save and review
            pdf.SaveToFile("result.pdf", FileFormat.PDF);
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.General.Find
Imports System.Drawing


Namespace HighlightText
	Class Program
		Private Shared Sub Main(args As String())
			'load test file
			Dim pdf As New PdfDocument("test.pdf")
			'highlight searched text with Green
			Dim result1 As PdfTextFind() = Nothing
			result1 = pdf.Pages(0).FindText("Spire.PDF").Finds
			For Each find As PdfTextFind In result1
				find.ApplyHighLight(Color.Green)
			Next
			'highlight searched text with Red
			Dim result2 As PdfTextFind() = Nothing
			result2 = pdf.Pages(0).FindText(".NET").Finds
			For Each find As PdfTextFind In result2
				find.ApplyHighLight(Color.Red)
			Next
			'save and review
			pdf.SaveToFile("result.pdf", FileFormat.PDF)
			System.Diagnostics.Process.Start("result.pdf")
		End Sub
	End Class
End Namespace