Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iText - How to stamp image on existing PDF and create an anchor

Tags:

itext

I have an existing document, onto which I would like to stamp an image at an absolute position. I am able to do this, 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 ?

like image 890
BYJU SUKUMARAN Avatar asked Nov 17 '14 23:11

BYJU SUKUMARAN


People also ask

What is PdfStamper in iText?

PdfStamper(PdfReader reader, OutputStream os, char pdfVersion) Starts the process of adding extra content to an existing PDF document. PdfStamper(PdfReader reader, OutputStream os, char pdfVersion, boolean append) Starts the process of adding extra content to an existing PDF document, possibly as a new revision.

Does iText 7 support .NET core?

iText 7 Core is the latest version of our innovative PDF library - to program PDF documents in Java or . NET (C#). iText is a more versatile, programmable and enterprise-grade PDF solution that allows you to embed its functionalities within your own software for digital transformation.

What is iText format?

iText is a Java library originally created by Bruno Lowagie which allows to create PDF, read PDF and manipulate them. The following tutorial will show how to create PDF files with iText. This tutorial assumes that you have basis Java and Eclipse knowledge. iText has a hierarchical structure.


1 Answers

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:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    Image img = Image.getInstance(IMG);
    float x = 10;
    float y = 650;
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    img.setAbsolutePosition(x, y);
    stamper.getOverContent(1).addImage(img);
    Rectangle linkLocation = new Rectangle(x, y, x + w, y + h);
    PdfDestination destination = new PdfDestination(PdfDestination.FIT);
    PdfAnnotation link = PdfAnnotation.createLink(stamper.getWriter(),
            linkLocation, PdfAnnotation.HIGHLIGHT_INVERT,
            reader.getNumberOfPages(), destination);
    link.setBorder(new PdfBorderArray(0, 0, 0));
    stamper.addAnnotation(link, 1);
    stamper.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.getScaledWidth();
float h = img.getScaledHeight();

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 result here: link_image.pdf If you click on the i icon, you jump to the last page of the document.

like image 129
Bruno Lowagie Avatar answered Oct 30 '22 20:10

Bruno Lowagie