PdfStamperImp enhancements for keeping the integrity of the original PDF
- From: Tenesi <bnasri (at) gmail.com>
- Date: Thu, 9 Feb 2006 16:14:34 +0100
Thanks for the prompt good news.
Would you please let me know what is wrong with this snippet and What would be the right approach to do it?
FileOutputStream os= new FileOutputStream("final.pdf"); // The target
com.lowagie.text.pdf.PdfReader baseR = new com.lowagie.text.pdf.PdfReader("base.pdf"); // The base file
com.lowagie.text.pdf.PdfStamper update = new
com.lowagie.text.pdf.PdfStamper(baseR, os, '\0',true); // An stamper
prepared for incremental changes
com.lowagie.text.pdf.PdfWriter stamperImp =update.getWriter(); // The corresponding writer of the appender
//XXX: Add new page?
stamperImp.setPageEmpty(false);
stamperImp.newPage();
//Fetch the new page from the additions
com.lowagie.text.pdf.PdfReader reader2 = new com.lowagie.text.pdf.PdfReader("additions.pdf");
com.lowagie.text.pdf.PdfImporte
dPage ipage= stamperImp.getImportedPage(reader2,1);
//XXX:Get the under layer to add the imprtedpage into the new page in target?
// A getUnderContent(baseR.getNumberOfPages()+1) on the PdfStamper
itself will not help as the newPage is not counted properly
com.lowagie.text.pdf.PdfContentByte under = stamperImp.getDirectContentUnder();
/*Add the page now this operation throws:
java.lang.NullPointerException
at
com.lowagie.text.pdf.PdfContentByte.addTemplate(PdfContentByte.java:1898)
under.addTemplate(ipage, 1, 0, 0, 1, 0, 0);*/
//commit the updates
update.close();
os.close();
Thanks again for your help in advance and best regards