[英]Merge Pdf Files Using PDFBox
I have to merge two pdf Files using PdfBox of Apache. 我必须使用Apache的PdfBox合并两个pdf文件。 I have taken physical pdf files to do so. 我已经采用了物理pdf文件。 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: 文件合并完美但后面有一些约束如下:
Please anyone help me achieve this requirement. 请有人帮我实现这个要求。
Call PDFMergerUtility.appendDocument(PDDocument destination, PDDocument source)
instead of merge. 调用PDFMergerUtility.appendDocument(PDDocument destination, PDDocument source)
而不是合并。 To load from a byte array, call PDDocument.load(array)
. 要从字节数组加载,请调用PDDocument.load(array)
。
Btw : I'd discourage to merge PDDocument objects that you created yourself, this fails if you have font subsets (see issue PDFBOX-3243 ). 顺便说一句 :我不鼓励合并您自己创建的PDDocument对象,如果您有字体子集,则会失败(请参阅问题PDFBOX-3243 )。 Better save them (eg in a ByteArrayOutputStream
) and reload and then merge. 最好保存它们(例如在ByteArrayOutputStream
)并重新加载然后合并。 To get a byte array from a ByteArrayOutputStream, use ByteArrayOutputStream.tobytearray()
. 要从ByteArrayOutputStream获取字节数组,请使用ByteArrayOutputStream.tobytearray()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.