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

Learn to use the TgtPDFDocument.SearchAll() method.
By Mamatha

The PDFtoolkit API has several methods for finding text in a PDF document. The TgtPDFDocument.SearchAll() finds all instances of a specified text.

public procedure SearchAll(
    const Asearchtext: string; 
    AOptions: TgtSearchTypes; 
    var SearchList: TgtStringList; 
    StartPage: Integer = 1
);

After the search is performed, you can use the SearchList to add link annotations. This code example shows how to add go-to and remote go-to link annotations using the SearchAll method.

procedure TForm1.Button1Click(Sender: TObject);
var
  Count, LI: Integer;
  Result1, Result2: TgtStringList;
  LineAnnot: TgtPDFLineAnnotation;
  URI: TgtPDFURIAction;
  GoToRAct: TgtPDFGoToRAction;
  LinkAnnot1, LinkAnnot2: TgtPDFLinkAnnotation;
begin
  PDFDoc.MeasurementUnit := muPixels;
  
  // Create the string lists
  Result1 := TgtStringList.Create;
  Result2 := TgtStringList.Create;

  // Load the input document into the PDFDoc component
  PDFDoc.LoadFromFile('Input.pdf');

  // Create instance of TgtPDFLineAnnotation and assign the properties
  LineAnnot := TgtPDFLineAnnotation.Create;
  LineAnnot.LineBeginingStyle := laNone;
  LineAnnot.LineEndingStyle := laNone;

  // Search all the instances of Text1 in the document and add them to the string list
  PDFDoc.SearchAll(Text1, [stWholeWord], Result1);

  if Result1 <> nil then
    Count := Result1.Count;

  // If the Text1 is present in the document, add the link annotation with URI action
  if Count > 0 then
  begin

    // Add the link and line annotation to all the instances of Text1 in the document
    for LI := 0 to Count - 1 do
    begin
      // Create instance of TgtPDFLinkAnnotation and assign the properties
      LinkAnnot1 := TgtPDFLinkAnnotation.Create;
      URI := TgtPDFURIAction.Create;

      URI.URI := edLink.Text;
      LinkAnnot1.Action := URI;
      LineAnnot.LineInteriorColor := clBlue;
      LinkAnnot1.RectLeft := TgtSearchElement(Result1.Objects[LI])
        .BoundingRect.Left;
      LinkAnnot1.RectTop := TgtSearchElement(Result1.Objects[LI])
        .BoundingRect.Top;
      LinkAnnot1.RectRight := TgtSearchElement(Result1.Objects[LI])
        .BoundingRect.Right;
      LinkAnnot1.RectBottom := TgtSearchElement(Result1.Objects[LI])
        .BoundingRect.Bottom;

      LineAnnot.gtLine(LinkAnnot1.RectLeft, LinkAnnot1.RectBottom + 2,
        LinkAnnot1.RectRight, LinkAnnot1.RectBottom + 2);

      PDFDoc.InsertAnnotation(LinkAnnot1,
        TgtSearchElement(Result1.Objects[LI]).PageNum);

      PDFDoc.InsertAnnotation(LineAnnot,
        TgtSearchElement(Result1.Objects[LI]).PageNum);

      LinkAnnot1.Free;
      URI.Free;
    end;
  end;

  // Search all the instances of Text2 in the document and add them to the string list
  PDFDoc.SearchAll(Text2, [stWholeWord], Result2);
  
  if Result2 <> nil then
    Count := Result2.Count;
  // If the Text2 is present in the document, add the link annotation with GoToRAct action
  if Count > 0 then
  begin
    // Add the link and line annotation to all the instances of Text2 in the document
    for LI := 0 to Count - 1 do
    begin
      // Create instance of TgtPDFLinkAnnotation and assign the properties
      LinkAnnot2 := TgtPDFLinkAnnotation.Create;
      GoToRAct := TgtPDFGoToRAction.Create;

      GoToRAct.FileName := edFileName.Text;
      GoToRAct.OpenInNewWindow := True;
      GoToRAct.DestinationType := dtFitH;
      GoToRAct.PageNo := 2;
      LinkAnnot2.Action := GoToRAct;

      LineAnnot.LineInteriorColor := clRed;
      LinkAnnot2.RectLeft := TgtSearchElement(Result2.Objects[LI])
        .BoundingRect.Left;
      LinkAnnot2.RectTop := TgtSearchElement(Result2.Objects[LI])
        .BoundingRect.Top;
      LinkAnnot2.RectRight := TgtSearchElement(Result2.Objects[LI])
        .BoundingRect.Right;
      LinkAnnot2.RectBottom := TgtSearchElement(Result2.Objects[LI])
        .BoundingRect.Bottom;

      LineAnnot.gtLine(LinkAnnot2.RectLeft, LinkAnnot2.RectBottom + 2,
        LinkAnnot2.RectRight, LinkAnnot2.RectBottom + 2);

      PDFDoc.InsertAnnotation(LinkAnnot2,
        TgtSearchElement(Result2.Objects[LI]).PageNum);

      PDFDoc.InsertAnnotation(LineAnnot,
        TgtSearchElement(Result2.Objects[LI]).PageNum);

      LinkAnnot2.Free;
      GoToRAct.Free;
    end;
  end;

  // Free the string lists  and line annotation
  LineAnnot.Free;

  Result1.Free;
  Result2.Free;

  // Save the changes made to the document
  PDFDoc.SaveToFile('Output.pdf');
end;

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

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

PDFOne .NET

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

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

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.

Our Java developer tools
XtremeDocumentStudio (for Java)

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

PDFOne (for Java)

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

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

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