I am trying to copy existing pdf file into some new file using itextpdf library in Java. I am using version 5.5.10 of itextpdf. I am facing different issues with both ways : PDFStamper and PdfCopy. When I use PDFStamper class, I observe that new file size is increased by large margin, although nothing new items were added. Here is code piece :
String currFile="C:\misc\pdffiles\AcroJS.pdf" ;
String dest = "C:\misc\pdffiles\AcroJS_copy.pdf" ;
PdfReader reader = new PdfReader(currFile) ;
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(dest)) ;
stamper.close() ;
reader.close() ;
Some observations are : 7 MB(original) to 13 MB (Approx, new file) , 116 KB > 119 KB (Approx)
I was expecting approximate same file size when just copying existing pdf file. I am not able to figure out why size is increasing that much.
I have tried PdfCopy class as well. I Followed 2 approaches with PdfCopy:
- Copy page by page.
- Call setMergeFields() on pdfcopy object then call pdfcopy.addDocument(reader) ;
But problem in both approaches is that it is throwing away some non-content metadata from pdf file and hence new pdf is breaking when opened by Adobe reader. For example my pdf contains dictionary object PdfName.S . In this case newly created pdf file is just 2KB (original was 1.6 MB) , it clearly means nothing is copied into document and it is broken.
My original requirement is very simple : copy existing pdf to new pdf file, without increase in size, without throwing away necessary items. Obiviously It is not like, copy, paste and then rename. Because in next step, I have some processings to do with pdf content. Any help will be much appreciated.
OS : Windows 10 Pro Java : 1.8.101 itext : 5.5.10
thanks