By Date: <-- -->
By Thread: <-- -->

PdfStamperImp enhancements for keeping the integrity of the original PDF



Hello everybody,

My aim is to add one page from a Pdf to  another Pdf without touching its previous content (incremental update). For that the Catalog, Pages tree and some other objects should be updated into new xref-ref table for sure. The idea is not to touch the previosu structure (PdfCopy or PdfCopyFields are not approperiate) because it might have been signed and we don't want to invalidated the byteranges.

Q1: Is this a right assumption or it can be done with the current library without changes?


Reading the documentation and looking into the source codes of PdfStamper, PdfStamperImp and PdfContentByte, It seems some refactoring is needed to overcome the restrictions to be able to do this. I tried to use the followin code at the user application level:


 com.lowagie.text.pdf.PdfReader pr = new com.lowagie.text.pdf.PdfReader("originalfiletobe updated.pdf");
 com.lowagie.text.pdf.PdfStamper update = new com.lowagie.text.pdf.PdfStamper(pr, os, '\0',true);
   
 com.lowagie.text.pdf.PdfReader reader2 = new com.lowagie.text.pdf.PdfReader(baos.toByteArray());
 com.lowagie.text.pdf.PdfStamperImp stamperImp =(com.lowagie.text.pdf.PdfStamperImp)update.getWriter();
 
stamperImp.newPage();
com.lowagie.text.pdf.PdfContentByte directContent = update.getWriter().getDirectContent();
directContent.reset();
com.lowagie.text.pdf.PdfImportedPage ipage= update.getImportedPage(reader2,1);
directContent.addTemplate(ipage, 1, 0, 0, 1, 0, 0,stamperImp.getPageStamp(pr.getNumberOfPages()));
update.close();


It obviously will not work as the way StamperImp initializes and keeps track of its pageResources is not the way the PdfContentByte is designed to follow (It rather awaits this info be encapsulated in a PdfDocument instance)


Q2: Does the code  suffer an understanding problem or it might had made sense, provided that the pageResources were set properly? Is there a work around for this or anybody know of a possibility to overcome this problem?


Q3: What would you suggest to do to achieve this goal of "ADDING" a page (or some other extra info which needs  updating  previous objects in the pdf document (Catalog/ Pages ...) ) to a Pdf deploying the incremental update possibilty provided in PDF-Specifications?


Tnx
Tenesi

--