PDFBox PDF肿的PDF文件大小 [英] PDFBox bloated PDF file size

查看:703
本文介绍了PDFBox PDF肿的PDF文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

设置过滤器:

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

这将设置Flate过滤器,在大多数情况下,这是相当不错的.

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

这篇关于PDFBox PDF肿的PDF文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆