网站首页 > java教程 正文
[翻译] 上一章中,我们已经学习了如何向PDF文档添加JavaScript。现在让我们学习如何将一个给定的PDF文档拆分为多个文档。
[原文] In the previous chapter, we have seen how to add JavaScript to a PDF document. Let us now learn how to split a given PDF document into multiple documents.
Splitting the Pages in a PDF Document 拆分PDF文档中的页面
[翻译]
您可以使用名为Splitter的类将给定的PDF文档拆分为多个PDF文档。此类用于将给定的PDF文档拆分为若干其他文档。
[原文]
You can split the given PDF document in to multiple PDF documents using the class named Splitter. This class is used to split the given PDF document into several other documents.
- split /splt/ 拆分
- document /'dɑkjumnt/ 文档
- multiple /'mltpl/ 多个
- class /klaes/ 类
[翻译]
以下是拆分现有PDF文档的步骤。
[原文]
Following are the steps to split an existing PDF document.
- following /'fɑlo/ 以下的
- step /step/ 步骤
Step 1: Loading an Existing PDF Document 步骤1:加载现有PDF文档
[翻译]
使用PDDocument类的静态方法load()加载现有PDF文档。该方法接受一个文件对象作为参数。由于这是一个静态方法,您可以使用类名调用它,如下所示。
[原文]
Load an existing PDF document using the static method load() of the PDDocument class. This method accepts a file object as a parameter, since this is a static method you can invoke it using class name as shown below.
File file = new File("path of the document")
PDDocument document = PDDocument.load(file);
- load /lod/ 加载
- static /'staetk/ 静态的
- parameter /p'raemtr/ 参数
- invoke /n'vok/ 调用
Step 2: Instantiate the Splitter Class 步骤2:实例化Splitter类
[翻译]
名为Splitter的类包含拆分给定PDF文档的方法,因此,请按照以下方式实例化此类。
[原文]
The class named Splitter contains the methods to split the given PDF document therefore, instantiate this class as shown below.
Splitter splitter = new Splitter();
- instantiate /n'staeniet/ 实例化
- contain /kn'ten/ 包含
- method /'meθd/ 方法
Step 3: Splitting the PDF Document 步骤3:拆分PDF文档
[翻译]
您可以使用Splitter类的split()方法拆分给定的文档。此方法接受PDDocument类的对象作为参数。
- 将文档拆分为单独的页面,每个页面作为一个独立的文档
- 以列表形式返回所有拆分后的文档
[原文]
You can split the given document using the Split() method of the Splitter class this class. This method accepts an object of the PDDocument class as a parameter.
List<PDDocument> Pages = splitter.split(document);
The split() method splits each page of the given document as an individual document and returns all these in the form of a list.
- split /splt/ 拆分
- individual /nd'vdul/ 单独的
- list /lst/ 列表
Step 4: Creating an Iterator Object 步骤4:创建迭代器对象
[翻译]
为了遍历拆分后的文档列表,您需要获取上一步中获得的列表的迭代器对象,使用listIterator()方法获取迭代器对象,如下所示。
[原文]
In order to traverse through the list of documents you need to get an iterator object of the list acquired in the above step, you need to get the iterator object of the list using the listIterator() method as shown below.
Iterator<PDDocument> iterator = Pages.listIterator();
- traverse /tr'vrs/ 遍历
- iterator /'tretr/ 迭代器
- acquire /'kwar/ 获取
Step 5: Closing the Document 步骤5:关闭文档
[翻译]
最后,使用PDDocument类的close()方法关闭文档,如下所示。
[原文]
Finally, close the document using close() method of PDDocument class as shown below.
document.close();
- finally /'fanli/ 最后
- close /kloz/ 关闭
Example 示例
[翻译]
假设有一个名为sample.pdf的PDF文档,位于*C:\PdfBox_Examples*路径中,该文档包含两页,一页包含图片,另一页包含文本,如下图所示。
[原文]
Suppose, there is a PDF document with name sample.pdf in the path *C:\PdfBox_Examples* and this document contains two pages one page containing image and another page containing text as shown below.
- suppose /s'poz/ 假设
- contain /kn'ten/ 包含
[翻译]
本示例演示如何拆分上述PDF文档。在此,我们将把名为sample.pdf的PDF文档拆分为两个不同的文档sample1.pdf和sample2.pdf。将此代码保存在名为SplitPages.java的文件中。
[原文]
This example demonstrates how to split the above mentioned PDF document. Here, we will split the PDF document named sample.pdf into two different documents sample1.pdf and sample2.pdf. Save this code in a file with name SplitPages.java.
import org.apache.pdfbox.multipdf.Splitter;
import org.apache.pdfbox.pdmodel.PDDocument;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;
public class SplitPages {
public static void main(String[] args) throws IOException {
//Loading an existing PDF document
File file = new File("C:/PdfBox_Examples/sample.pdf");
PDDocument document = PDDocument.load(file);
//Instantiating Splitter class
Splitter splitter = new Splitter();
//splitting the pages of a PDF document
List<PDDocument> Pages = splitter.split(document);
//Creating an iterator
Iterator<PDDocument> iterator = Pages.listIterator();
//Saving each page as an individual document
int i = 1;
while(iterator.hasNext()) {
PDDocument pd = iterator.next();
pd.save("C:/PdfBox_Examples/sample"+ i++ +".pdf");
}
System.out.println("Multiple PDFs created");
document.close();
}
}
- demonstrate /'demnstret/ 演示
- split /splt/ 拆分
[翻译]
使用以下命令从命令提示符编译并执行保存的Java文件。
[原文]
Compile and execute the saved Java file from the command prompt using the following commands.
javac SplitPages.java
java SplitPages
- 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.
Multiple PDFs created
- upon /'pɑn/ 在……时
- display /d'sple/ 显示
[翻译]
如果您检查指定路径,可以观察到生成了多个PDF文件,名称为sample1和sample2,如下图所示。
[原文]
If you verify the given path, you can observe that multiple PDFs were created with names sample1 and sample2 as shown below.
- verify /'verfa/ 验证
- observe /b'zrv/ 观察
猜你喜欢
- 2025-07-06 10.JAVA向PDF中添加多行文本(PDFBOX)
- 2025-07-06 提取PDF中的表格数据——tabula-py库
- 2025-07-06 一次Java内存占用高的排查案例,解释了我对内存问题的所有疑问
- 2025-07-06 已跪,Java全能笔记爆火,分布式/开源框架/微服务/性能调优全有
- 2025-07-06 一线大厂Java八股文合集PDF版分享,内容多达700多页
- 2025-07-06 【Java面试必问】 Spring Boot面试题 35 问(附PDF)
- 2025-07-06 阿里Nacos惊爆安全漏洞,火速升级!(附修复建议)
- 2025-07-06 一文看懂 ZooKeeper,面试再也不用背八股(文末送PDF)
- 2025-07-06 17.从PDF文档生成图像(JAVA+PDFBOX)
- 2025-07-06 7 用Java获取页码及删除页面(PDFBOX)
你 发表评论:
欢迎- 最近发表
-
- 五,网络安全IDA Pro反汇编工具初识及逆向工程解密实战
- 「JAVA8」- Lambda 表达式(java lambda表达式原理)
- 深入探讨Java代码保护:虚拟机保护技术的新时代
- Nginx反向代理原理详解(图文全面总结)
- 逆向拆解日本IT,哪些Java技术栈薪资溢价高
- mybatis 逆向工程使用姿势不对,把表清空了,心里慌的一比
- Spring Boot集成ProGuard轻松实现Java 代码混淆, Java 应用固若金汤
- 从 Java 代码逆向工程生成 UML 类图和序列图
- 人与人相处:尊重是标配,靠谱是高配,厚道是顶配
- Windows系统安装日期如何修改(windows10怎么修改安装日期)
- 标签列表
-
- 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)
本文暂时没有评论,来添加一个吧(●'◡'●)