Free Support Forum - aspose.com

How to add a Hyperlink to existing image in PDF?

How to add a Hyperlink to existing image in PDF to make it clickable ?

@MKamal88

Thanks for contacting support.

You may please use following code snippet in order to add hyperlink to an existing image inside PDF document.

ImagePlacementAbsorber absorber = new ImagePlacementAbsorber();
Document doc = new Document(dataDir + "FullPolicy.pdf");
doc.Pages[1].Accept(absorber);
foreach(ImagePlacement img in absorber.ImagePlacements)
{
 LinkAnnotation link = new LinkAnnotation(img.Page, img.Rectangle);
 link.Action = new GoToURIAction("http://www.aspose.com");
 img.Page.Annotations.Add(link);
}
// Save the document
doc.Save(dataDir + "ImageWithHyperlink.pdf"); 

In case of any further assistance, please feel free to let us know.

I got this error in Saving

An unhandled exception of type ‘System.IndexOutOfRangeException’ occurred in Aspose.Pdf.dll

Additional information: At most 4 elements (for any collection) can be viewed in evaluation mode.

@MKamal88

It seems you are using API without any license. The exception you are facing is about limitation of trial version. We request you to please use a valid license and in case you do not have one, please consider applying 30-days temporary license in order to evaluate API without any limitation.

Sorry asad, now it’s working fine but give me no result, it seems no images has been grabbed by absorber and don’t went to the loop

@MKamal88

Would you please share your sample PDF document with us, so that we can test the scenario in our environment and address it accordingly.

5423639.pdf (2.8 MB)

Here it’s

@MKamal88

Thanks for sharing sample PDF.

We have tested the scenario in our environment and observed that your PDF document does not contain any image resource on its first page however, it contains several images in its other pages. In case you were using same code snippet which we have shared earlier, it is possible to face an exception as previous code snippet searches only first page for images.

You may please use following line of code and modify previous code snippet accordingly to get other images.

doc.Pages.Accept(absorber);

Using above line of code, API was able to add hyperlink to every found image in your document. For your reference, an output PDF is also attached. In case of any further assistance, please feel free to let us know.

ImageWithHyperLink.pdf

Thanks Asaad for your help,
what if I want to make a link on this ?
Capture.JPG (81.4 KB)

@MKamal88

Thanks for sharing your concerns.

We have logged an investigation ticket as PDFNET-45638 in our issue tracking system for your requirements. We will further investigate whether it is feasible to achieve what you require or not. As soon as we have some updates regarding investigation, we will let you know. Please spare us little time.

We are sorry for the inconvenience.

Thanks asad, I’m waiting