How to stamp image on existing PDF and create an anchor?

I have an existing document, onto which I would like to stamp an image at an absolute position.

31st May 2016
admin-marketing

I am able to stamp the image, but I would also like to make this image clickable: when a user clicks on the image I would like the PDF to go to the last page of the document. Here is my code:

 

PdfReader readerOriginalDoc = new PdfReader("src/main/resources/test.pdf");
PdfStamper stamper = new PdfStamper(
    readerOriginalDoc, new FileOutputStream("NewStamper.pdf"));
PdfContentByte content = stamper.getOverContent(1);
Image image = Image.getInstance("src/main/resources/images.jpg");
image.scaleAbsolute(50, 20);
image.setAbsolutePosition(100, 100);
image.setAnnotation(new Annotation(0, 0, 0, 0, 3));
content.addImage(image);
stamper.close();
Any idea how to do this?

 

 

Posted on StackOverflow on Nov 17, 2014 by BYJU SUKUMARAN

You are using a technique that only works when creating documents from scratch.

Please take a look at the AddImageLink example to find out how to add an image and a link to make that image clickable to an existing document:

protected void manipulatePdf(String dest) throws Exception {
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
    ImageData img = ImageDataFactory.create(imgSrc);
    float x = 10;
    float y = 650;
    float w = img.getWidth();
    float h = img.getHeight();
    PdfPage page = pdfDoc.getFirstPage();
    new PdfCanvas(page.newContentStreamAfter(), pdfDoc.getFirstPage().getResources(), pdfDoc)
            .addImage(img, x, y, false);
    Rectangle linkLocation = new Rectangle(x, y, w, h);
    PdfAnnotation annotation = new PdfLinkAnnotation(linkLocation)
            .setHighlightMode(PdfAnnotation.HIGHLIGHT_INVERT)
            .setAction(PdfAction.createGoTo(PdfExplicitDestination.createFit(pdfDoc.getLastPage())))
            .setBorder(new PdfArray(new float[]{0, 0, 0}));
    page.addAnnotation(annotation);
    pdfDoc.close();
}

You already have the part about adding the image right. Note that I create parameters for the position of the image as well as its dimensions:

float x = 10;
float y = 650;
float w = img.getWidth();
float h = img.getHeight();

I use these values to create a Rectangle object:

Rectangle linkLocation = new Rectangle(x, y, x + w, y + h);

This is the location for the link annotation we are creating with the PdfAnnotation class. You need to add this annotation separately using the addAnnotation() method.

You can take a look at the resulting pdf: if you click on the i icon, you jump to the last page of the document.

Click this link if you want to see how to answer this question in iText 5.


Share this article

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now