问题
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