I'm using iText 4.2.1 to generate my pdf reports. So basically I have a PDF template which contains a cover page, end page and a content page (that just contains an image header).
I'm using PdfCopy & PdfImportedPage to copy my template and PageStamp to add my content dynamically.
Need: I need to use the content page many times: as much as content pages in my report.
Problem: If I use pdfCopy.createPageStamp(importedPage) and ColumnText.showTextAligned to add some text the stamp persists over the next content pages. Thus my content page n°2 contains the text of the 1st one (added by PageStamp) and its own text (added by another PageStamp).
Here is an example of code:
// Init Document doc = new Document(); PdfCopy pdfCopy = new PdfCopy( doc, new FileOutputStream( new File("Result.pdf") ) ); doc.open(); PdfReader pdfReader = new PdfReader( "pdf-template.pdf" ); // Page 1 PdfImportedPage importedPage1= pdfCopy.getImportedPage(pdfReader, 2); String text1= "Text of the first page - 1"; PageStamp stamp1 = pdfCopy.createPageStamp( importedPage1 ); ColumnText.showTextAligned( stamp.getOverContent(), Element.ALIGN_CENTER, new Phrase(text1), 400, 500, 0 ); stamp.alterContents(); pdfCopy.addPage(importedPage1); // Page 2 PdfImportedPage importedPage2= pdfCopy.getImportedPage(pdfReader, 2); String text2 = "Text of the second page - 2"; PageStamp stamp2 = pdfCopy.createPageStamp( importedPage2 ); ColumnText.showTextAligned( stamp2.getOverContent(), Element.ALIGN_CENTER, new Phrase(text2), 200, 700, 0 ); stamp2.alterContents(); pdfCopy.addPage(importedPage2); // Closing doc.close();
--> In the 2nd page I will see my text1 and my text2
I tried using the same PdfImportedPage: same result.
I tried using the same PageStamp: same result.
I tried using a different PdfReader: it works but is this really the solution??
Thanks for your help.