使用PDFBox合并Pdf文件 [英] Merge Pdf Files Using PDFBox
问题描述
我必须使用Apache的PdfBox合并两个pdf文件。我已经采用了物理pdf文件。
下面是代码:
I have to merge two pdf Files using PdfBox of Apache. I have taken physical pdf files to do so. Below is the code:
PDFMergerUtility ut = new PDFMergerUtility();
ut.addSource(path1);
ut.addSource(path2);
ut.setDestinationFileName(path3);
ut.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
文件合并完美但后面有一些约束如下:
Files are merged perfectly but then have some constraints as below:
- 我在代码本身创建第一个文件,所以它是PDDocument对象。
- 我必须与第一个文件合并的文件是字节数组格式。
- 我不需要保存合并的文件,但需要将其转换为字节数组。
请有人帮助我达到这个要求。
Please anyone help me achieve this requirement.
推荐答案
致电 PDFMergerUtility.appendDocument(PDDocument destination,PDDocument source)
而不是合并。要从字节数组加载,请调用 PDDocument.load(array)
。
Btw :我不鼓励合并您自己创建的PDDocument对象,如果您有字体子集,则会失败(请参阅问题 PDFBOX-3243 )。最好保存它们(例如在 ByteArrayOutputStream
中)并重新加载然后合并。要从ByteArrayOutputStream获取字节数组,请使用 ByteArrayOutputStream.tobytearray()
。
Btw: I'd discourage to merge PDDocument objects that you created yourself, this fails if you have font subsets (see issue PDFBOX-3243). Better save them (e.g. in a ByteArrayOutputStream
) and reload and then merge. To get a byte array from a ByteArrayOutputStream, use ByteArrayOutputStream.tobytearray()
.
这篇关于使用PDFBox合并Pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!