java-使用PDFBox复制pdf是否可以像使用iText一样小?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-使用PDFBox复制pdf是否可以像使用iText一样小?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3277字,纯文字阅读大概需要5分钟。
内容图文
![java-使用PDFBox复制pdf是否可以像使用iText一样小?](/upload/InfoBanner/zyjiaocheng/695/0207a989a44a4deaaa7b76dea3fb5834.jpg)
我正在阅读PDF并输出其中包含原始PDF的多个副本的PDF.我对PDFBox和iText做同样的事情进行测试.如果我分别复制每个页面,iText会创建一个更小的输出.
问题:在PDFBox中还有另一种方法可以实现较小的输出PDF.
对于一个示例输入文件,使用两个工具生成两个副本到输出:
>原始PDF大小:30K
> PDFBox(v 1.7.1)生成的PDF:84K
> iText(v 5.3.4)生成的PDF:35K
PDFBox的Java代码(很抱歉给您造成错误处理).请注意,它是如何反复读取输入并将其整体复制的:
PDFMergerUtility merger = new PDFMergerUtility();
PDDocument workplace = null;
try {
for (int cnt = 0; cnt < COPIES; ++cnt) {
PDDocument document = null;
InputStream stream = null;
try {
stream = new FileInputStream(new File(sourceFileName));
document = PDDocument.load(stream);
if (workplace == null) {
workplace = document;
} else {
merger.appendDocument(workplace, document);
}
} finally {
if (document != null && document != workplace) {
document.close();
}
if (stream != null) {
stream.close();
}
}
}
OutputStream out = null;
try {
out = new FileOutputStream(new File(destinationFileName));
workplace.save(out);
} finally {
if (out != null) {
out.close();
}
}
} catch (COSVisitorException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (workplace != null) {
try {
workplace.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用iText进行编码的代码.请注意,它是如何逐页加载输入文件并将每页传输到输出的:
Document document = null;
PdfReader reader = null;
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = new FileInputStream(new File(sourceFileName));
outputStream = new FileOutputStream(new File(destinationFileName));
document = new Document();
PdfCopy copy = new PdfSmartCopy(document, outputStream);
document.open();
reader = new PdfReader(inputStream);
// loop over the pages in that document
int pdfPageNo = reader.getNumberOfPages();
for (int page = 0; page < pdfPageNo;) {
PdfImportedPage onePage = copy.getImportedPage(reader, ++page);
// duplicate each page N times
for (int i = 0; i < COPIES; ++i) {
copy.addPage(onePage);
}
}
copy.freeReader(reader);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (document != null) {
document.close();
}
try {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
// do nothing
}
}
两者都被这个包围:
public class Duplicate {
/** The original PDF file */
private static final String sourceFileName = "PDF_CI_US2CA.pdf";
/** The resulting PDF file. */
private static final String destinationFileName = "itext_output.pdf";
private static final int COPIES = 2;
public static void main(String[] args) {
...
}
}
解决方法:
使用以下解决方案,我能够创建具有许多重复页面的PDF文件,并且对存储的影响最小.
PDDocument samplePdf = null;
try {
samplePdf = PDDocument.load(PDF_PATH);
PDPage page = (PDPage) samplePdf.getDocumentCatalog().getAllPages().get(0);
for(int i = 0; i < COPIES; i++) {
samplePdf.importPage(page);
}
samplePdf.save(SAVE_PATH); //$NON-NLS-1$
} catch (IOException e) {
e.printStackTrace();
} catch (COSVisitorException e) {
e.printStackTrace();
}
在我的第一次尝试中,我使用了samplePdf.addPage(page),但是没有按预期工作.因此,显然添加和导入功能之间存在差异.我必须检查源代码或文档以了解原因.无论如何,这应该有助于您使用PDFBox设计解决方案.
内容总结
以上是互联网集市为您收集整理的java-使用PDFBox复制pdf是否可以像使用iText一样小?全部内容,希望文章能够帮你解决java-使用PDFBox复制pdf是否可以像使用iText一样小?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。