简体   繁体   English

使用PDFBox合并Pdf文件

[英]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: 文件合并完美但后面有一些约束如下:

  1. I am creating 1st file in the code itself,so it is PDDocument object. 我在代码本身创建第一个文件,所以它是PDDocument对象。
  2. The file which I have to merge with 1st file is in byte array format. 我必须与第一个文件合并的文件是字节数组格式。
  3. I don't need to save the merged files but need convert it as byte array. 我不需要保存合并的文件,但需要将其转换为字节数组。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM