专业的JAVA编程教程与资源

网站首页 > java教程 正文

16.合并多个PDF文档(JAVA+PDFBOX)

temp10 2025-06-24 16:39:48 java教程 3 ℃ 0 评论

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.
[上一章中,我们已经学习了如何将一个给定的PDF文档拆分为多个文档。现在让我们学习如何将多个PDF文档合并为一个文档。]


Merging Multiple PDF Documents 合并多个PDF文档

[翻译]
您可以使用名为
PDFMergerUtility的类将多个PDF文档合并为一个PDF文档,此类提供了将两个或更多PDF文档合并为单个PDF文档的方法。

16.合并多个PDF文档(JAVA+PDFBOX)

[原文]
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.

  • merge /mrd/ 合并
  • multiple /'mltpl/ 多个
  • document /'dɑkjumnt/ 文档
  • provide /pr'vad/ 提供

[翻译]
以下是合并多个PDF文档的步骤。

[原文]
Following are the steps to merge multiple PDF documents.

  • following /'fɑlo/ 以下的
  • step /step/ 步骤

Step 1: Instantiating the PDFMergerUtility class 步骤1:实例化PDFMergerUtility类

[翻译]
按照以下方式实例化合并工具类。

[原文]
Instantiate the merge utility class as shown below.

PDFMergerUtility PDFmerger = new PDFMergerUtility();
  • instantiate /n'staeniet/ 实例化
  • utility /ju'tlti/ 工具

Step 2: Setting the destination file 步骤2:设置目标文件

[翻译]
使用以下方法设置目标文件:

  • 设置目标文件名

[原文]
Set the destination files using the setDestinationFileName() method as shown below.

PDFmerger.setDestinationFileName("C:/PdfBox_Examples/data1/merged.pdf");
  • destination /dest'nen/ 目标
  • file /fal/ 文件

Step 3: Setting the source files 步骤3:设置源文件

[翻译]
使用以下方法设置源文件:

  • 添加源文件

[原文]
Set the source files using the addSource() method as shown below.

File file = new File("path of the document")
PDFmerger.addSource(file);
  • source /srs/ 源
  • add /aed/ 添加

Step 4: Merging the documents 步骤4:合并文档

[翻译]
使用
PDFMergerUtility类的mergeDocuments()方法合并文档,如下所示。

[原文]
Merge the documents using the mergeDocuments() method of the PDFmerger class as shown below.

PDFmerger.mergeDocuments();
  • merge /mrd/ 合并
  • document /'dɑkjumnt/ 文档

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.jpgContent File.jpg

  • suppose /s'poz/ 假设
  • path /paeθ/ 路径

[翻译]
本示例演示如何合并上述PDF文档。在此,我们将把名为
sample1.pdfsample2.pdf的PDF文档合并为一个名为merged.pdf的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");
   }
}
  • demonstrate /'demnstret/ 演示
  • merge /mrd/ 合并

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

[原文]
Compile and execute the saved Java file from the command prompt using the following commands.

javac MergePDFs.java 
java MergePDFs 
  • compile /km'pal/ 编译
  • execute /'ekskjut/ 执行
  • command /k'maend/ 命令
  • prompt /prɑmpt/ 提示符

[翻译]
执行后,上述程序会对给定的PDF文档进行加密,并显示以下消息。

[原文]
Upon execution, the above program encrypts the given PDF document displaying the following message.

Documents merged
  • upon /'pɑn/ 在……时
  • display /d'sple/ 显示

[翻译]
如果您检查指定路径,可以观察到一个名为
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

  • verify /'verfa/ 验证
  • observe /b'zrv/ 观察

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表