We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add image-link into existing document

Hey,

I need to add a hyperlink that is presented by an image to the top of a page of an existing pdf document.

As I could see LinkAnnotations does not provide such functionality of adding an image to it.

Can you help me?

Regards

Mark

@bruegmann

Thank you for contacting support.

You can add a hyperlink to an image and add that image to paragraph collection of the page. Below is a code snippet for your kind reference.

        String img = dataDir + @"aspose-logo.jpg";
        Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
        Aspose.Pdf.Page page = doc.Pages.Add();
        page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
        Aspose.Pdf.Image image = new Aspose.Pdf.Image();
        image.File = img;
        image.Hyperlink = new WebHyperlink("www.aspose.com");
        image.HorizontalAlignment = HorizontalAlignment.Left;
        image.VerticalAlignment = VerticalAlignment.Top;
        page.Paragraphs.Add(image);
        doc.Save(dataDir + "ImageLink_18.9.1.pdf");

Generated PDF file has also been attached for your kind reference ImageLink_18.9.1.pdf. For this PDF document page margins are set to zero, if your existing PDF file has page margins set then the image will be added while considering margins.

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thank you for the quick response.

I tried your solution but the positioning is not working for some documents. I am using the following code for every page of an existing pdf document:

page.Paragraphs.Add(
   New Aspose.Pdf.Image With {
      .ImageStream = GetLinkGraphic(),
      .Hyperlink = New Aspose.Pdf.WebHyperlink(item.Link),
      .HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left,
      .VerticalAlignment = Aspose.Pdf.VerticalAlignment.Top,
      .Margin = New Aspose.Pdf.MarginInfo(0,0,0,0)
   }
)

The image is not located on the top left corner. And is not considering rotation on some other document.

Here is the result with graphic links to www.google.com: ezsgq03e.pdf (142.3 KB)

Since I am not able to test it with the newest Aspose.Pdf-Version 18.9. Is it working better in this version?

Regards

Mark

@bruegmann

We have tested the same code snippet but within a foreach loop because there are more pages in your PDF document, and the latest version of Aspose.PDF for .NET API is working as expected. An image with hyperlink is being added at top left corner of the page, as per your requirements. Generated PDF file has been attached for your kind reference. TestLink_18.9.1.pdf

        String img = dataDir + @"aspose-logo.jpg";
        Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + "ezsgq03e.pdf");
        foreach (Aspose.Pdf.Page page in doc.Pages)
        {
            page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
            Aspose.Pdf.Image image = new Aspose.Pdf.Image();
            image.File = img;
            image.Hyperlink = new WebHyperlink("www.aspose.com");
            image.HorizontalAlignment = HorizontalAlignment.Left;
            image.VerticalAlignment = VerticalAlignment.Top;
            page.Paragraphs.Add(image);
        }
        doc.Save(dataDir + "TestLink_18.9.1.pdf");

In case you are facing problem with rotation, please create a separate topic while sharing all the details and we will be glad to help you.

I left that part out but my sample looks like yours, sorry.

So setting the pages’ margin did the trick. Can you tell me if setting the margin of the page will affect any existing content of the current pdf.

And ok to the “separate rotation topic”.

@bruegmann

We are glad to know that things are working in your environment. Setting the margin will not affect existing content however the margins will be considered if you add any new content. Please feel free to contact us if you need any further assistance.