Skip to main content

How to: Highlight Search Results in a PDF File

  • 3 minutes to read

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

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.

static void HighlightResultWithGraphics(PdfDocumentProcessor processor, PdfTextSearchResults result)
{
    using (PdfGraphics graphics = processor.CreateGraphics()) 
    {
        for (int i = 0; i < result.Rectangles.Count; i++)
        {
            RectangleF rect = new RectangleF(new PointF((float)result.Rectangles[i].Left, (float)result.Page.CropBox.Height - (float)result.Rectangles[i].Top),
                new SizeF((float)result.Rectangles[i].Width, (float)result.Rectangles[i].Height));
            using (var brush = new SolidBrush(Color.FromArgb(130, 55, 155, 255)))
                graphics.FillRectangle(brush, rect);
        }
        graphics.AddToPageForeground(result.Page, 72, 72);
    }
}

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.

static void HighlightResultUsingAnnotations(PdfDocumentProcessor processor, PdfTextSearchResults result)
{
    for (int i = 0; i < result.Rectangles.Count; i++)
    {
        PdfTextMarkupAnnotationData annotation =
        processor.AddTextMarkupAnnotation(result.PageNumber, result.Rectangles[i], PdfTextMarkupAnnotationType.Highlight);
        if (annotation != null)
        {
            annotation.Color = new PdfRGBColor(0.2, 0.6, 0);
        }
    }
}

Search and Highlight Text

The code example below shows how to use 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

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);
            HighlightResultUsingAnnotations(documentProcessor, result);
        }
    }
    //Save the document
    documentProcessor.SaveDocument(@"..\..\Result.pdf");
}
See Also