Pdfbox 简明教程

PDFBox - Merging Multiple PDF Documents

在前一章中,我们已经了解如何将给定的 PDF 文档拆分为多个文档。现在我们来了解如何合并多个 PDF 文档为一个文档。

In the previous chapter, we have seen how to split a given PDF document into multiple documents. Let us now learn how to merge multiple PDF documents as a single document.

Merging Multiple PDF Documents

借助名为 PDFMergerUtility class 的类,可以将多个 PDF 文档合并到一个 PDF 文档中,此类提供将两个或更多 PDF 文档合并到一个 PDF 文档的方法。

You can merge multiple PDF documents into a single PDF document using the class named PDFMergerUtility class, this class provides methods to merge two or more PDF documents in to a single PDF document.

以下是合并多个 PDF 文档的步骤。

Following are the steps to merge multiple PDF documents.

Step 1: Instantiating the PDFMergerUtility class

按如下所示实例化合并实用程序类。

Instantiate the merge utility class as shown below.

PDFMergerUtility PDFmerger = new PDFMergerUtility();

Step 2: Setting the destination file

按如下所示使用 setDestinationFileName() 方法设置目标文件。

Set the destination files using the setDestinationFileName() method as shown below.

PDFmerger.setDestinationFileName("C:/PdfBox_Examples/data1/merged.pdf");

Step 3: Setting the source files

按如下所示使用 addSource() 方法设置源文件。

Set the source files using the addSource() method as shown below.

File file = new File("path of the document")
PDFmerger.addSource(file);

Step 4: Merging the documents

按如下所示使用 PDFmerger 类的 mergeDocuments() 方法合并文档。

Merge the documents using the mergeDocuments() method of the PDFmerger class as shown below.

PDFmerger.mergeDocuments();

Example

假设我们有两个 PDF 文档 sample1.pdfsample2.pdf ,位于 C:\PdfBox_Examples\ 路径下,如下所示。

Suppose, we have two PDF documents — sample1.pdf and sample2.pdf, in the path C:\PdfBox_Examples\ as shown below.

image file
content file

此示例演示如何合并上述 PDF 文档。在此我们将名为 sample1.pdfsample2.pdf 的 PDF 文档合并到单个 PDF 文档 merged.pdf 中。将此代码保存在名为 MergePDFs.java. 的文件中。

This example demonstrates how to merge the above PDF documents. Here, we will merge the PDF documents named sample1.pdf and sample2.pdf in to a single PDF document merged.pdf. Save this code in a file with name MergePDFs.java.

import org.apache.pdfbox.multipdf.PDFMergerUtility;
import java.io.File;
import java.io.IOException;
public class MergePDFs {
   public static void main(String[] args) throws IOException {
      File file1 = new File("C:\\EXAMPLES\\Demo1.pdf");
      File file2 = new File("C:\\EXAMPLES\\Demo2.pdf");

      //Instantiating PDFMergerUtility class
      PDFMergerUtility PDFmerger = new PDFMergerUtility();

      //Setting the destination file
      PDFmerger.setDestinationFileName("C:\\Examples\\merged.pdf");

      //adding the source files
      PDFmerger.addSource(file1);
      PDFmerger.addSource(file2);

      //Merging the two documents
      PDFmerger.mergeDocuments();
      System.out.println("Documents merged");
   }
}

使用以下命令从命令提示符处编译并执行已保存的 Java 文件。

Compile and execute the saved Java file from the command prompt using the following commands.

javac MergePDFs.java
java MergePDFs

执行完上述程序之后,会对给定的 PDF 文档加密并显示以下信息。

Upon execution, the above program encrypts the given PDF document displaying the following message.

Documents merged

如果您验证给定的路径,您可以观察到创建了名为 merged.pdf 的 PDF 文档,其中包含源文档的所有页面,如下所示。

If you verify the given path, you can observe that a PDF document with name merged.pdf is created and this contains the pages of both the source documents as shown below.

merged