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

How To Add Hyperlinks To Images In A PDF Document

"I have a list of URLs that need to be added to a set of images that repeat in every page of a PDF document."
By Mohammed Najeemudheen & Shine Babu

When images are used as hyperlinks, it adds a small but significant touch of interactivity to PDF documents. In this tip, we will show you how to add a set of URLs to images in a PDF document.

Here, we assume that each page in the document have the same set of images and that we need to add the URLs in the order we find the images in each page.

The code for this is pretty simple. First, we put the URLs (URIs) in a list. Then, we load each page in the document. Next, we identify where the images are located on the pages. We then create a link annotation for each image and set the annotation's rectangle to the dimensions of the image. Finally, each link annotation is set to a URL that corresponds to the order of the image in the page.

var
  LinkAnnot : TgtPDFLinkAnnotation;
  URI : TgtPDFURIAction;
  LImageList : TgtPDFPageElementList;
  LStringLinks : TStringList;
  StrLink : String;
  LI,LJ,LK : Integer;
  LLeft, LRight, LBottom, LTop,LWidth, LHeight: Double;
begin
  LStringLinks := TStringList.Create;
  PDFDoc.LoadFromFile('Input.pdf');
  PDFDoc.OpenAfterSave := True;
 
  // Creates a link annotation object
  LinkAnnot := TgtPDFLinkAnnotation.Create;
  LinkAnnot.Contents := 'PDFtoolkit';

  // Creates a URI object
  URI := TgtPDFURIAction.Create ;

  // Sets the properties of the link annotation
  LinkAnnot.BackgroundColor := clBlue;
  LinkAnnot.AnnotTitle := 'LinkAnnotion';
  LinkAnnot.Name := 'Link1';
  LinkAnnot.BorderWidth := 0;

  // Creates a list of URIs
  LStringLinks.Add('www.gmail.com');
  LStringLinks.Add('www.adobe.com');
  LStringLinks.Add('www.rediffmail.com');

 // for each page in the document ...
 for LJ:= 1 to PDFDoc.PageCount do
  begin
    // Creates a list of image items in a given page
    LImageList := 
          PDFDoc.GetPageElements(LJ,[etImage],muPixels);
    LK := 0;
    // for each image in the list ...
    for LI:= 0 to LImageList.Count - 1 do
    begin
      if LK < LStringLinks.Count then
      begin
        // Sets the URI property of the link annotation 
        // action to a corresponding URL from the list
        URI.URI := LStringLinks[LK];
        LinkAnnot.Action :=  URI;

        // Obtains the width and height of image
        LWidth := TgtPDFImageElement(
           LImageList.Items[LI]).XCoordinateScalingFactor;
        LHeight := TgtPDFImageElement(
           LImageList.Items[LI]).YCoordinateScalingFactor;

        LLeft := LImageList.Items[LI].XCordOrigin;
        LTop := LImageList.Items[LI].YCordOrigin + LHeight ;
        LRight := LImageList.Items[LI].XCordOrigin +LWidth ;
        LBottom := LImageList.Items[LI].YCordOrigin;

        // Specifies the annotation rectangle
        LinkAnnot.SetBounds(LLeft, LTop, LRight, LBottom);

        // Adds the link annotation on the image
        PDFDoc.InsertAnnotation(LinkAnnot,LJ);
        Inc(LK);
          if LK = LStringLinks.Count then
            LK := 0;
      end;
    end;
  end;
  // Saves the PDF document
  PDFDoc.SaveToFile('LinkAnnot.pdf');

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