Image Positioning in iText - Java

I am trying to read one PDF file and copy its data to another PDF file. The first PDF file contains text and images, and I want to write the image in the second PDF, where the text ends (which is basically the end of the PDF file). RIght now it just prints at the top. How to do it?

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(); 
+4
source share
2 answers

You should use PdfStamper instead of PdfWriter with imported pages. Your approach discards all interactive material. You can also use the idea of ​​sorifiend.

To determine where the text on this page ends, look at the iText file in action, an example of the 2nd edition of ShowTextMargins , which parses a PDF file and declares a rectangle displaying a text box.

+2
source

Try the following:

First find the location / coordinates where the image should go, and then just add the second line below in your code so that the image is inserted in this place "X, Y"

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

Take a look at a working example: http://itextpdf.com/examples/iia.php?id=70

+6
source

Source: https://habr.com/ru/post/1441404/


All Articles