Image positioning in iText - Java

18,600

Solution 1

You should use a PdfStamper instead of a PdfWriter with imported pages. Your approach throws away all interactive contents. You can use sorifiend's idea there, too.

To determine where the text on the given page ends, have a look at the iText in Action, 2nd edition example ShowTextMargins which parses a PDF and ads a rectangle showing the text margin.

Solution 2

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

Share:
18,600
Anurag Ramdasan
Author by

Anurag Ramdasan

website: anuragramdasan.com Twitter : @anuragwho

Updated on June 04, 2022

Comments

  • Anurag Ramdasan
    Anurag Ramdasan almost 2 years

    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();
    
  • Anurag Ramdasan
    Anurag Ramdasan over 11 years
    Oh this is great. But is there any way I can determine X and Y as where the text ends? or at the bottom center of the last page?
  • sorifiend
    sorifiend over 11 years
    I understand there are a couple of ways to do this, but from your code it looks like mkl's suggestion of text margins will be the easiest way to get X and Y. Another way that takes a bit more work is to read and write parts from the original PDF piece by piece and calculate the X and Y location as you go, it is slower but that way you know exactly where the image can fit, and you can add images in and around the text.
  • mkl
    mkl over 11 years
    Actually Anurag may have to somewhat change the sample i pointed to. It finds the margin if all text. That corresponds to what he said but if his pdf contains a footer, then the sample also sees it as part of the text. Consequently he'd have to adapt the code to take footers into account.
  • Anurag Ramdasan
    Anurag Ramdasan over 11 years
    Thanks. It worked exactly How I wanted to. :) But is there any way I can place text with co-ordinates using pdf stamper too?
  • Anurag Ramdasan
    Anurag Ramdasan over 11 years
    Thanks. It worked exactly How I wanted to. :) But is there any way I can place text with co-ordinates using pdf stamper too?
  • mkl
    mkl over 11 years
    Yes you can. I will have to look up how, though... My use cases don't include adding text to existing documents.
  • Anurag Ramdasan
    Anurag Ramdasan over 11 years
    I had to add a set of images and text, turns out Images are easy but text is a little tough to work with. Some help would be appreciated :)
  • mkl
    mkl over 11 years
    Have a look at chapter 6 of iText in Action, 2nd Edition, available at manning.com/lowagie2/samplechapter6.pdf for free. Subsection 6.3.1 should show you the way. And that whole chapter is good to read anyways...