Back to Devexpress

How to: Bookmark Search Results in a PDF File

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

latest3.5 KB
Original Source

How to: Bookmark Search Results in a PDF File

  • Sep 19, 2023
  • 2 minutes to read

Important

You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use these examples in production code.

This example shows how to create a bookmark with a destination that displays the page as follows:

View Example

csharp
using System.Collections.Generic;
using DevExpress.Pdf;

using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor())
{
    // Load a document
    documentProcessor.LoadDocument(@"..\..\Document.pdf");

    // Define search words
    string[] words = { "DX-B5000", "DX-RX800" };

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

    foreach (string word in words)
    {
        // Get search results
        PdfTextSearchResults results = documentProcessor.FindText(word, searchParameters);

        // If the text is found, create a destination that positions the found text
        // at the upper window corner
        if (results.Status == PdfTextSearchStatus.Found)
        {
            PdfXYZDestination destination = new PdfXYZDestination(results.Page, 0, results.Rectangles[0].Top, null);

            // Create a bookmark associated with the destination
            PdfBookmark bookmark = new PdfBookmark() { Title = word, Destination = destination };

            // Add the bookmark to the bookmark list
            documentProcessor.Document.Bookmarks.Add(bookmark);
        }
    }
    // Save the modified document
    documentProcessor.SaveDocument(@"..\..\Result.pdf");
}
vb
Imports System.Collections.Generic
Imports DevExpress.Pdf

Using documentProcessor As New PdfDocumentProcessor()

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

    ' Define search words
    Dim words() As String = { "DX-B5000", "DX-RX800" }

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

    For Each word As String In words
        ' Get search results
        Dim results As PdfTextSearchResults = documentProcessor.FindText(word, searchParameters)

        ' If the text is found, create a destination that positions the found text
        ' at the upper window corner
        If results.Status = PdfTextSearchStatus.Found Then
            Dim destination As New PdfXYZDestination(results.Page, 0, results.Rectangles(0).Top, Nothing)

            ' Create a bookmark associated with the bookmark
            Dim bookmark As New PdfBookmark() With {.Title = word, .Destination = destination}

            ' Add the bookmark to the bookmark list
            documentProcessor.Document.Bookmarks.Add(bookmark)
        End If
    Next word
    ' Save the modified document
    documentProcessor.SaveDocument("..\..\Result.pdf")
End Using

See Also

Get Started - Load and Edit a PDF File