PDFtoolkit VCL
Edit, enhance, secure, merge, split, view, print PDF and AcroForms documents
Compatibility
Delphi C++Builder

Making Select Keywords Active with Link Annotations in PDF

Want to make instances of a particular text or keyword linked to a destination elsewhere in the document? Read on!
By Ramya H. N.

PDFtoolkit allows you to find occurrences of a word or phrase in any page of a PDF document with its search routines SearchFirst and SearchNext.

A link annotation could be inserted in these occurrences to a destination somewhere else in the document using the InsertAnnotation method with any of the following actions:

By adding a link annotation to the occurrence of a particular word or phrase using PDFtoolkit, you can make that word or phrase an active link in the PDF file that users can interact with.

Here are the steps to add link annotations to all occurences of a given search text in a PDF file:

  1. Load the input file: A call to the Loadfromfile() method loads the input file into the gtPDFDocument component.
     ...
     gtPDFDocument.Loadfromfile('Input.pdf');
     ...
    
  2. Search for all instances of a text in the input file: The method SearchAll() returns the number of the instances of a given text in the input PDF file. This method populates the objects array of string list SearchResult with all PDF text elements containing the search text. With these text elements, you can identify the locations where the link annotation needs to be inserted.
     ...
     SearchResult := TStringList.Create;
     Count := gtPDFDocument.SearchAll(
                                      Text,
                                      [stWholeWord],
                                      SearchResult
                                     );
     ...
    
  3. Insert the link annotation for each identified instance of the search text: To highlight the location of the link annotations, you can use TgtPDFLineAnnotation. Create instances of TgtPDFLineAnnotation and TgtPDFLinkAnnotation, and set their properties.

    Next, insert link and line annotations at each instance of the search text in the PDF document - a call made to InsertAnnotation() with the link annotation object and PageNumber as arguments.
     ...
     LineAnnot := TgtPDFLineAnnotation.Create;
     LineAnnot.LineBeginingStyle := laNone;
     LineAnnot.LineEndingStyle := laNone;
     LinkAnnot := TgtPDFLinkAnnotation.Create;
     URI := TgtPDFURIAction.Create ;
     URI.URI := edLink.Text;
     LinkAnnot.Action := URI;
     for LI := 0 to Count - 1 do
     begin
       LinkAnnot.RectLeft := TgtPDFTextElement(SearchResult.
         Objects[LI]).XCordOrigin;
       LinkAnnot.RectTop := TgtPDFTextElement(
         SearchResult.Objects[LI]).YCordOrigin;
       LinkAnnot.RectRight := LinkAnnot.RectLeft
         + PDFDoc.GetCharWidth(Trim('Text'),
         TgtPDFTextElement(SearchResult.Objects[LI]).Font);
        LinkAnnot.RectBottom := LinkAnnot.RectTop -
          TgtPDFTextElement(SearchResult.Objects[LI]).
          Font.Height;
        LineAnnot.gtLine(LinkAnnot.RectLeft,
          LinkAnnot.RectBottom + 2, LinkAnnot.RectRight,
          LinkAnnot.RectBottom + 2);
        gtPDFDocument.InsertAnnotation(
          LinkAnnot, TgtPDFTextElement(
          SearchResult.Objects[LI]).PageNumber);
        gtPDFDocument.InsertAnnotation(LineAnnot,
          TgtPDFTextElement(SearchResult.Objects[LI]).
          PageNumber);
      end;
     ...
    
  4. Save the enhanced output file and display: The output file is saved when a call is made to the SaveToFile() method.
     ...
     gtPDFDocument.SaveToFile('Output.pdf');
     ...
    

This PDF file contains link annotation with URI action. When the mouse pointer moves over the search text ("Pascal"), the link annotation will be displayed. Clicking the annotation opens the link in a browser.

---o0O0o---

Downloads:

---o0O0o---

Our Developer Tools
eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

XtremePDFConverter VCL

A Delphi/C++Builder component to intelligently convert PDF to user-friendly Word RTF documents.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers

PDFOne (for Java™)

A Java™ PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java™ applications.

XtremeFontEngine (for Java)

Java font engine to render glyphs from Type 1, Type 2 (CFF), and TrueType fonts

Our Office Productivity Applications
Free PDF Reader

A free, fast, and portable application for viewing, printing and converting PDF documents.

Privacy | Legal | Feedback | Newsletter | Resellers © 2002-2013 Gnostice Information Technologies Private Limited. All rights reserved.

This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels. Windows XP users are advised to use Microsoft ClearType Tuning for optimal experience. Also, please use the latest version of a standards-compliant browser such as Firefox, Opera, or Dragon (Chromium).