Skip to main content Skip to footer

Programmatically Find Text and Add Jumplinks to Found Text

It is possible to find specified text on PDF pages while also adding a jumplink to another page of the PDF in DsPDF by using the FindText method, DestinationFit class and LinkAnnotation class. To demonstrate, we’ll focus on adding a set of two different jumplinks that will link to two separate pages.

First, we can establish through custom function how the document is initialized, what text to find, and the page indexes we want to use. We will also want to run our custom function twice, each instance having our different parameters we just created. Altogether, it will look like the following: 

        private static void AddLink(string inputPath, string outputPath)
        {
            // Generate file stream from file path
            using Stream pdfStream = new FileStream(path: inputPath, mode: FileMode.Open, access: FileAccess.Read);
            GcPdfDocument document = new();

            // Load the document from file stream
            document.Load(stream: pdfStream);

            // Text to find
            string findText1 = "Link to next page";
            string findText2 = "Link to previous page";

            // Destination pages
            int destinationPage1 = 1;
            int destinationPage2 = 0;

            // Find text and add jumplink to another page
            FindTextAndAddLink(document: document, findText: findText1, destinationPage: destinationPage1);
            FindTextAndAddLink(document: document, findText: findText2, destinationPage: destinationPage2);

            // Save the document
            // document.Save(fileName: outputPath);
        }

Now, we can create our other custom function, which will determine how to find the text in the document and what to do when the desired text(s) are found. We will find the position of the text using FindText, use DestinationFit to link the page destination, and then create the links with LinkAnnotation for each result. For example: 

        private static void FindTextAndAddLink(GcPdfDocument document, string findText, int destinationPage)
        {
            // Find the positions in the document where the text is present
            IList<FoundPosition> foundPositions = document.FindText(findTextParams: new FindTextParams(text: findText, wholeWord: true, matchCase: false));

            if (foundPositions.Count > 0)
            {
                // Destination to the page
                DestinationFit linkDestination = new DestinationFit(pageIndex: destinationPage);

                // Generate a link annotation for the first occurence of the text to the specified page
                foreach (FoundPosition foundPosition in foundPositions)
                {
                    foreach (Quadrilateral bound in foundPosition.Bounds)
                    {
                        // Generate link at the rectangle coordinates of the text and specify the destination page
                        LinkAnnotation link = new LinkAnnotation(rect: bound.ToRect(), dest: linkDestination);

                        // Add link annotation to the document
                        document.Pages[index: foundPosition.PageIndex].Annotations.Add(item: link);
                    }
                }
            }
        }

See the following for a working sample: LinkToNewPage.zip

Tye Glenz