Back to Devexpress

How to: Highlight Search Results in a PDF File

officefileapi-119467-pdf-document-api-examples-pdf-graphics-and-additional-content-how-to-highlight-search-results-in-a-document.md

latest8.3 KB
Original Source

How to: Highlight Search Results in a PDF File

  • Nov 20, 2025
  • 4 minutes to read

This example highlights search results in a document. You can use PDF Graphics to draw filled rectangles around text or add annotations to highlight search results.

Convert Page Coordinates to World Coordinates

Simple

The PdfDocumentProcessor.FindText method returns the search results in the page coordinates. To draw filled rectangles that correspond to the document area containing found text, you need to convert page coordinates to world coordinates since the PdfGraphics.FillRectangle method uses world coordinates.

In this example, the conversion is made in the following method:

cs
static void HighlightResultWithGraphics(PdfDocumentProcessor processor, PdfTextSearchResults result) {
    using (PdfGraphics graphics = processor.CreateGraphicsPageSystem()) {
        for (int i = 0; i < result.Rectangles.Count; i++) {
            using (var brush = new DXSolidBrush(Color.FromArgb(130, 55, 155, 255)))
                RectangleF rect = new RectangleF(new PointF((float)result.Rectangles[i].Left, (float)result.Page.CropBox.Height),
                    new SizeF((float)result.Rectangles[i].Width, (float)result.Rectangles[i].Height));
                graphics.FillRectangle(brush, rect);
        }
        graphics.AddToPageForeground(result.Page);
    }
}
vb
Private Shared Sub HighlightResultWithGraphics(processor As PdfDocumentProcessor, result As PdfTextSearchResults)
    Using graphics As PdfGraphics = processor.CreateGraphicsPageSystem()
        For i As Integer = 0 To result.Rectangles.Count - 1
            Using brush As New DXSolidBrush(Color.FromArgb(130, 55, 155, 255))
                graphics.FillRectangle(brush, result.Rectangles(i))
            End Using
        Next
        graphics.AddToPageForeground(result.Page)
    End Using
End Sub

Use PDF Graphics

Create a RectangleF object around the search result. Use the PdfGraphics.FillRectangle method to draw a rectangle around search text. The PdfGraphics.AddToPageForeground method allows you apply filled rectangles to the page.

cs
}

//This method uses PdfGraphics to highlight text
static void HighlightResultWithGraphics(PdfDocumentProcessor processor, PdfTextSearchResults result)
{
    using (PdfGraphics graphics = processor.CreateGraphicsPageSystem())
    {
        for (int i = 0; i < result.Rectangles.Count; i++)
        {
            RectangleF rect = new RectangleF(new PointF((float)result.Rectangles[i].Left, (float)result.Rectangles[i].Top- (float)result.Rectangles[i].Height),
                new SizeF((float)result.Rectangles[i].Width, (float)result.Rectangles[i].Height));
            using (var brush = new DXSolidBrush(Color.FromArgb(130, 55, 155, 255)))
                graphics.FillRectangle(brush, rect);
        }
cs
End Using
End Sub

'This method uses PdfGraphics to highlight text
Private Shared Sub HighlightResultWithGraphics(ByVal processor As PdfDocumentProcessor, ByVal result As PdfTextSearchResults)
    Using graphics As PdfGraphics = processor.CreateGraphicsPageSystem()
        For i As Integer = 0 To result.Rectangles.Count - 1
            Dim rect As New RectangleF(New PointF(CSng(result.Rectangles(i).Left), CSng(result.Rectangles(i).Top - result.Rectangles(i).Height)),

Use Annotations

Call the PdfDocumentProcessor.AddTextMarkupAnnotation method to add an annotation to the search results. Use the PdfTextSearchResults.Rectangles property to access the document areas that contain search text. The PdfAnnotationData.Color property specifies the annotation color.

cs
//This method uses annotations to highlight text
static void HighlightResultWithAnnotations(PdfDocumentProcessor processor, PdfTextSearchResults result)
{
    PdfDocumentFacade facade = processor.DocumentFacade;
    PdfPageFacade page = facade.Pages[result.Page.GetPageIndex()];

    for (int i = 0; i < result.Rectangles.Count; i++)
    {
        PdfTextMarkupAnnotationFacade annotation =
                  page.AddTextMarkupAnnotation(result.Rectangles[i], PdfTextMarkupAnnotationType.Highlight);
        if (annotation != null)
        {
            annotation.Color = new PdfRGBColor(0.2, 0.6, 0);
        }
    }
}
cs
graphics.FillRectangle(brush, rect)
            End Using
        Next
        graphics.AddToPageForeground(result.Page)
    End Using
End Sub

'This method uses annotations to highlight text
Private Shared Sub HighlightResultWithAnnotations(ByVal processor As PdfDocumentProcessor, ByVal result As PdfTextSearchResults)
    Dim facade As PdfDocumentFacade = processor.DocumentFacade
    Dim page As PdfPageFacade = facade.Pages(result.Page.GetPageIndex())

    For i As Integer = 0 To result.Rectangles.Count - 1
        Dim annotation As PdfTextMarkupAnnotationFacade = page.AddTextMarkupAnnotation(result.Rectangles(i), PdfTextMarkupAnnotationType.Highlight)

Search and Highlight Text

The following code sample calls the PdfDocumentProcessor.FindText method to search text. (If you wish to highlight the results, you can also use the HighlightResult method - as described above.)

View Example

cs
//Create a PDF document processor.
using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor())
{
    //Define search words
    string[] words = { "Get", "DX-RX809", "HD", "DX-B5000" };

    //Load a PDF document
    documentProcessor.LoadDocument(@"..\..\..\Document.pdf");

    //Specify the search parameters
    PdfTextSearchParameters searchParameters = new PdfTextSearchParameters();
    searchParameters.CaseSensitive = true;
    searchParameters.WholeWords = true;

    foreach (string word in words)
    {
        PdfTextSearchResults result;
        //Get the search results from the FindText method call with search text and search parameters
        while ((result = documentProcessor.FindText(word, searchParameters)).Status == PdfTextSearchStatus.Found)
        {
            //HighlightResultWithGraphics(documentProcessor, result);
            HighlightResultWithAnnotations(documentProcessor, result);
        }
    }
    //Save the document
cs
Using documentProcessor As New PdfDocumentProcessor()
    'Define search words
    Dim words As String() = {"Get", "DX-RX809", "HD", "DX-B5000"}

    'Load a PDF document
    documentProcessor.LoadDocument("..\..\..\Document.pdf")

    'Specify the search parameters
    Dim searchParameters As New PdfTextSearchParameters()
    searchParameters.CaseSensitive = True
    searchParameters.WholeWords = True

    For Each word As String In words
        Dim result As PdfTextSearchResults
        'Get the search results from the FindText method call with search text and search parameters
        Do While InlineAssignHelper(result, documentProcessor.FindText(word, searchParameters)).Status = PdfTextSearchStatus.Found
            'HighlightResultWithGraphics(documentProcessor, result)
            HighlightResultWithAnnotations(documentProcessor, result)
        Loop
    Next

See Also

Get Started - Load and Edit a PDF File