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');
This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels.
Windows users are advised to use Microsoft ClearType Tuning for optimal experience.
Linux and other users can enable font smoothing, as supported by their OS.
Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.