企业项目管理、ORK、研发管理与敏捷开发工具平台

网站首页 > 精选文章 正文

15.将一个给定的PDF文档拆分为多个文档(JAVA+PDFBOX)

wudianyun 2025-07-01 23:43:48 精选文章 5 ℃

[翻译] 上一章中,我们已经学习了如何向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.pdfsample2.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文件,名称为
sample1sample2,如下图所示。

[原文]
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/ 观察

最近发表
标签列表