使用PDFBox复制pdf可以像iText一样小吗? [英] Can duplicating a pdf with PDFBox be small like with iText?
问题描述
我正在阅读PDF并输出包含原始PDF的多个副本的PDF。我通过对 PDFBox 和 iText 。如果我单独复制每个页面,iText会创建一个小得多的输出。
I am reading in a PDF and outputting a PDF with multiple copies of the original PDF in it. I test by doing the same thing for both PDFBox and iText. iText creates a much smaller output if I duplicate each page individually.
问题:还有另一种方法可以在PDFBox中执行此操作,从而产生较小的输出PDF。
The question: Is there another way to do this in PDFBox that results in smaller output PDFs.
对于一个示例输入文件,使用这两个工具为输出生成两个副本:
For one example input file, generating two copies to the output with both tools:
- 原始PDF大小:30K
- PDFBox(v 1.7.1)生成PDF:84K
- iText(v 5.3.4)生成PDF:35K
PDFBox的Java代码(很遗憾对您造成错误处理)。注意它是如何反复读取输入并将其作为一个整体重复:
Java code for PDFBox (sorry to inflict error handling on you). Notice how it reads the input over and over and duplicates it as a whole:
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执行此操作的代码。注意它如何逐页加载输入文件并将每个页面传送到输出:
Code to do it with iText. Notice how it loads the input file page by page and transfers each page to the output:
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文件,并且对存储的影响极小。
Using the following solution, I was able to create a PDF file with many duplicate pages and have a minimal impact on storage.
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 (页)
但它没有按预期工作。很明显, add
和 import
函数之间存在差异。我将不得不检查源或文档以了解原因。无论如何,这应该可以帮助您使用PDFBox为您的需求设计解决方案。
In my first attempt I used, samplePdf.addPage(page)
but it didn't work as expected. So obviously there is a difference between the add
and import
functions. I'll have to check the source or documentation to see why. Anyway, this should help you devise a solution for your needs with PDFBox.
这篇关于使用PDFBox复制pdf可以像iText一样小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!