网站首页 > java教程 正文
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文档的方法。
[原文]
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.pdf和sample2.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.pdf和sample2.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/ 观察
猜你喜欢
- 2025-06-24 Java 25 在 JEP 519 中集成了紧凑对象头
- 2025-06-24 「Java必修课」Java 8之例说Stream的合并
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)