3

Using PDFBox can read Dynamic PDF created by livecycle. The code below reads then writes back the xml file that used to create the dynamic PDF. I bit concerned as the resulting file is quite large start out with 647kb pdf. The new pdf 14000kb. Anybody know how can reduce the size of the new file produced. Can set some type of compression when writing back to pdf file?

 PDDocument doc = PDDocument.load("filename");
 doc.setAllSecurityToBeRemoved(true);
 PDDocumentCatalog docCatalog = doc.getDocumentCatalog();
 PDAcroForm form = docCatalog.getAcroForm();
 PDXFA xfa = form.getXFA();
 COSBase cos = xfa.getCOSObject();
 COSStream coss = (COSStream) cos;
 InputStream cosin = coss.getUnfilteredStream();
 Document document = documentBuilder.parse(cosin);
 COSStream cosout = new COSStream(new RandomAccessBuffer());
 OutputStream out = cosout.createUnfilteredStream();
 TransformerFactory tFactory = TransformerFactory.newInstance();
 Transformer transformer = tFactory.newTransformer();
 DOMSource source = new DOMSource(xmlDoc);
 StreamResult result = new StreamResult(out);
 transformer.transform(source, result);
 PDXFA xfaout = new PDXFA(cosout);
 form.setXFA(xfaout);
Jason Weh
  • 314
  • 4
  • 12

1 Answers1

3

set a filter:

COSStream cosout = new COSStream(new RandomAccessBuffer());
cosout.setFilters(COSName.FLATE_DECODE);

this will set the Flate filter, which is pretty good in most of the cases.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • Thanks that worked really well now the pdf file is back down close to orginal size of pdf. – Jason Weh Nov 04 '14 at 22:15
  • I'm mainly working with `PDPageContentStream` and in the constructor, it's handling this filter. But the pdf result still is 3-4 times bigger than the original file. Do I have to also set the filters at the end? – ksadjad Dec 02 '19 at 15:21
  • It's possible that the original file had compressed object streams, we don't support these. Tagged PDF files can be bloated as a result if this. But to be sure I would have to see your file. – Tilman Hausherr Dec 02 '19 at 15:31