PDFBox bloated PDF file size

主宰稳场 提交于 2019-12-10 10:35:59

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/26726083/pdfbox-bloated-pdf-file-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!