Skip to main content

How to: Highlight Search Results in a PDF File

  • 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:

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);
    }
}

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.

}

//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);
        }

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.

//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);
        }
    }
}

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

//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
See Also