Add Image in new page to existing PDF file - java

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?

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.

Related

iText - add Table at the end of last page IF Space available

I am using iText 2.1.7
I write a pdf document for which the page size and contant can differ each time. What i want to achieve is a table at the very last page and at the bottom of that page. I am aware of the method 'writeSelectedRows', but with this it could happen that i overwrite text on the page, because i dont know if there is space for my table.
So in conclusion:
If i have reached the last page i want to add my table at the bottom of the page. But if the table does not fit, i want a new page and on this page i want to add the table at the bottom.
I could not find a solution so far.
Use this http://developers.itextpdf.com/de/node/1910 to calculate the height of a table and then check the available space on a page with PdfWriter.GetVerticalPostition(). You might want to consider your Document.BottomMargin or some other offset in your space calculation. (v4.1.6)
Check out this link:
http://developers.itextpdf.com/content/best-itext-questions-stackoverview/general-questions-about-itext/itext7-how-find-out-current-cursor-position-page
I suppose that once you know the current cursor position on this page, you can write a simple if-statement to add a new page, or not.
Kind regards,
Joris

Insert images in different pdf pages

I am trying to set a list of images in a PDF document using iText with Java, i could just insert some of them in the first page but i don't know how to jump to the next pages in order to put the rest of my pictures
for(int i = 0; i < 25; i++) {
Image myImg = Image.getInstance("/home/code/img"+i+".png");
imgPaper.setAbsolutePosition(50, 728-(y*58));
document.add(myImg);
y++;
}
The OP clarified his question in a comment
i have already another pages, i just want how to jump to themĀ 
You seem to be creating a new document using a PdfWriter. That class is designed to create a pdf one page after the other. As soon as you start a new page, all former ones are written to file.
Thus, in this process you cannot jump to arbitrary pages. You have to add all information for a page while it is the current one.
If, after creating a multi page document, you need to manipulate the content of its pages, first close the document (finishing it), read it into a PdfReader, and apply a PdfStamper which allows you to manipulate arbitrary pages of an existing PDF.
Alternatively, especially if your images constitute something like a water mark or header/footer Logos, consider using page events in your pdf creation process with the PdfWriter.
try to add a new line to your document
document.add( Chunk.NEWLINE );
link for info:
How to insert blank lines in PDF?

Add content under and over PDFs with acrofield in Android with iText

I have a problem with my Android app: I need to take some PDF with acrofields, and add some content, but not in another page, or in a page in the middle, just under the content of the existing PDF. And then, when I finished, I have to attach another PDF file with acrofield to this.
For now, I have 3 pages: the first PDF with acrofield, the content that I want to add, and the second PDF with acrofield.
Is there a way to compact all in less pages? Because I have a lot of blank space between one page and the other, and I need to do this.

iText - add content to the bottom of an existing page

I want to add a piece of text to every page of a PDF file. This answer in SO works fine. But, the text is added to the top of the page. I would like to add my text to the bottom of each page. How do I do this?
Here is the relevant part of the code.
while (iteratorPDFReader.hasNext()) {
PdfReader pdfReader = iteratorPDFReader.next();
// Create a new page in the target for each source page.
while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
document.newPage();
pageOfCurrentReaderPDF++;
currentPageNumber++;
page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
cb.addTemplate(page, 0, 0);
document.add(new Paragraph("My Text here")); //As per the SO answer
}
pageOfCurrentReaderPDF = 0;
}
The code is part of a function which accepts a folder, reads the PDF files in it and merges them into one single file. So, I would like to add the text in the above loop itself, instead of iterating the file once again.
If you want to automatically add content to every page, you need a page event.
This is explained in chapter 5 of my book" iText in Action - Second Edition".
If you don't own a copy of the book, you can consult the examples here.
You can also find solutions by looking for the keyword Header / Footer.
The example you're referring to doesn't look correct at first sight. Sure, you can use "two passes", one to create the content, and another to add headers or footers, but the suggested solution is different from the recommended solution: http://itextpdf.com/examples/iia.php?id=118
You are copying the mistake in your question: why on earth would you import the document you've just created into a new document, thus throwing away all possible interactivity you've added to that document? It just doesn't make sense. It's unbelievable that this answer received that many up-votes. I'm the original developer of iText and I'm not at all happy with that answer!
In your case, there may be no need to create the document in memory first and to add the footer afterwards. Just take a look at http://itextpdf.com/examples/iia.php?id=104
You need to create a PdfPageEvent implementation (for instance using PdfPageEventHelper) and you need to implement the onEndPage() method.
Documented caveats:
Do not use onStartPage() to add content,
Do not add anything to the Document object passed to the page event,
Unless you specified a different page size, the lower-left corner has the coordinate x = 0; y = 0. You need to take that into account when adding the footer. The y-value for the footer is lower than the y-value for the header.
For more info: consult my book.
Have a look at chapter 6 of iText in Action, 2nd edition, especially at subsection 6.4.1: Concatenating and splitting PDF documents.
Listing 6.22, ConcatenateStamp.java, shows you how you should create a PDF from copies of pages (in your case: all pages) of multiple other PDFs; the sample additionally adds a new "Page X of Y" footer; this demonstrates how you can add content at given positions on the pages while merging the source files.
Perhaps this may be of assistance here... I suspect you want to do something like the following:
cb.addTemplate(page, 0, 0);
document.add(new Paragraph("My Text here"));
document.setFooter(new HeaderFooter("Footnote goes here"));
}
pageOfCurrentReaderPDF = 0;

Different margins for first and last page in iText PDF

Is there a way to specify different margins for my first and last pages in iText?
I have a large header to be placed in first page and a large footer to be present in the last page.
The trouble is when I position the footer absolutely the content of the page might overlap it because of the absolute positioning.
yes.
before creating last page, set margins and then call newpage() method of Document object.

Categories