Question

I am new to itext in java. I have an existing pdf of 2 pages. I need to add 2 new pages to it and then add an image to the 3rd page and then add four small rectangles and some text in the 4th page. On searching I got codes for adding new pages and codes for adding images to the existing pdf separately. Column text was used to add text to a new page, I searched for adding image to column text but I cannot find it. getUnderContent helped me to add image at the bottom of 2nd page. I want the image to be added in 3rd page. And the 4th page gets more complicated. I add the rectangle and text using PdfContentByte. This should be done by creating a new page. Any ideas?

Was it helpful?

Solution

Based on your comments, I assume that you are using PdfStamper and that you're able to add an image to an existing page. This is, for instance, done using getUnderContent() and its addImage() method. Now you need to add an extra page.

In PdfStamper, you can use the insertPage() method to achieve this:

stamper.insertPage(pageNum, rectangle);

In this line pageNum is an int value indication the page number where you want to insert the new page, and rectangle is the size of the page. For instance:

stamper.insertPage(reader.getNumberOfPages() + 1, reader.getPageSize(1));

Once you have inserted the page, you can get the "over" or "under" content, and add an image to that PdfContentByte using the addImage() method. You might want to replace reader.getPageSize(1) with a Rectangle object that corresponds with the dimensions of the image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top