Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image positioning in iText - Java

I am trying to read one PDF and copy its data into another PDF. The first PDF contains some text and images and I wish to write an image in the second PDF exactly where the text ends(which is basically the end of the PDF file). RIght now it just prints at the top. How can I make this change?

PdfReader reader = null;
reader = new PdfReader(Var.input);
Document document=new Document();
PdfWriter writer = null;
writer = PdfWriter.getInstance(document,new FileOutputStream(Var.output));
PdfImportedPage page = writer.getImportedPage(reader, 1); 
reader.close();  
document.open();
PdfContentByte cb = writer.getDirectContent();
// Copy first page of existing PDF into output PDF
document.newPage();
cb.addTemplate(page, 0, 0);

// Add your new data / text here
Image image = null;
image = Image.getInstance (Var.qr);
document.add(image);
document.close();
like image 712
Anurag Ramdasan Avatar asked Oct 22 '12 23:10

Anurag Ramdasan


People also ask

How to set position of image in Java?

You can insert the image in a desired position on the document using the method setFixedPosition() of the Image class.

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.


1 Answers

Try this:

First get the location/co-ords of where the image needs to go, then simply add the second line from below to your code so the image is inserted at that location "X, Y"

Image image = Image.getInstance(String RESOURCE);
image.setAbsolutePosition(X, Y);
writer.getDirectContent().addImage(image);

Take a look here for some examples in iText 5: https://itextpdf.com/en/resources/examples/itext-5-legacy/chapter-3-adding-content-absolute-positions

like image 93
sorifiend Avatar answered Sep 22 '22 01:09

sorifiend