Skip to main content

How to: Highlight Search Results in a PDF File

  • 4 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.

public static void HighlightResult(PdfGraphics graphics, PdfTextSearchResults result, SolidBrush brush)
    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));

        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.

public static void HighlightResult(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 DevExpress.Pdf;
using System.Drawing;

class Program {
    static void Main(string[] args) {
        // 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

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

            // Comment the following using statement if you use annotations
            using (var brush = new SolidBrush(Color.FromArgb(130, 55, 155, 255)))

            // Get search results from the FindText method call
            // with search text and search parameters
            PdfTextSearchResults result = documentProcessor.FindText(word, searchParameters);
                foreach (string word in words) {
                    // Highlight the result
                    while (result.Status == PdfTextSearchStatus.Found) {
                        using (PdfGraphics graphics = documentProcessor.CreateGraphics())
                            HighlightResult(graphics, result, brush);
                    // Use this method call to add annotations:
                    // HighlightResult(documentProcessor, result);
                    result = documentProcessor.FindText(word, searchParameters);
            // Save the document
See Also