PDFBox臃肿的PDF文件大小

时间:2014-11-04 01:01:21

标签: java pdf pdfbox xdp

使用PDFBox可以阅读由livecycle创建的动态PDF。下面的代码读取然后写回用于创建动态PDF的xml文件。我有点担心,因为得到的文件非常大,从647kb pdf开始。新的pdf 14000kb。任何人都知道如何减少生成的新文件的大小。写回pdf文件时可以设置某种类型的压缩吗?

 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 个答案:

答案 0 :(得分:2)

设置过滤器:

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

这将设置Flate过滤器,这在大多数情况下非常好。