Pdfbox 简明教程
PDFBox - Removing Pages
现在我们来了解如何从 PDF 文档中删除页面。
Let us now learn how to remove pages from a PDF document.
Removing Pages from an Existing Document
您可以使用 PDDocument 类中的 removePage() 方法从现有 PDF 文档中删除一个页面。
You can remove a page from an existing PDF document using the removePage() method of the PDDocument class.
Step 1: Loading an Existing PDF Document
使用 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.load(file);
Step 2: Listing the Number of Pages
您可以使用 getNumberOfPages() 方法列出 PDF 文档中存在的页数,如下所示。
You can list the number of pages that exists in the PDF document using the getNumberOfPages() method as shown below.
int noOfPages= document.getNumberOfPages();
System.out.print(noOfPages);
Step 3: Removing the Page
您可以使用 PDDocument 类中的 removePage() 方法从 PDF 文档中删除一个页面。对于此方法,您需要传递要删除的页面的索引。
You can remove a page from the PDF document using the removePage() method of the PDDocument class. To this method, you need to pass the index of the page that is to be deleted.
在为 PDF 文档中的页面指定索引时,请记住这些页面的索引从零开始,即,如果您要删除第 1 页,则索引值必须为 0。
While specifying the index for the pages in a PDF document, keep in mind that indexing of these pages starts from zero, i.e., if you want to delete the 1st page then the index value needs to be 0.
document.removePage(2);
Example
假设我们有一个名为 sample.pdf 的 PDF 文档,它包含三个空页面,如下所示。
Suppose, we have a PDF document with name sample.pdf and it contains three empty pages as shown below.

此示例演示如何从现有 PDF 文档中删除页面。在此,我们将加载上面指定的,名为 sample.pdf 的 PDF 文档,从中删除一个页面,并将其保存在 C:/PdfBox_Examples/ 路径中。将此代码保存在名为 Removing_pages.java 的文件中。
This example demonstrates how to remove pages from an existing PDF document. Here, we will load the above specified PDF document named sample.pdf, remove a page from it, and save it in the path C:/PdfBox_Examples/. Save this code in a file with name Removing_pages.java.
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
public class RemovingPages {
public static void main(String args[]) throws IOException {
//Loading an existing document
File file = new File("C:/PdfBox_Examples/sample.pdf");
PDDocument document = PDDocument.load(file);
//Listing the number of existing pages
int noOfPages= document.getNumberOfPages();
System.out.print(noOfPages);
//Removing the pages
document.removePage(2);
System.out.println("page removed");
//Saving the document
document.save("C:/PdfBox_Examples/sample.pdf");
//Closing the document
document.close();
}
}
使用以下命令从命令提示符处编译并执行已保存的 Java 文件。
Compile and execute the saved Java file from the command prompt using the following commands.
javac RemovingPages.java
java RemovingPages
在执行时,上述程序创建了一个 PDF 文档,其中包含空白页面,显示以下消息。
Upon execution, the above program creates a PDF document with blank pages displaying the following message.
3
page removed
如果您验证指定的路径,您会发现所需的页面已被删除,并且文档中仅剩两页,如下所示。
If you verify the specified path, you can find that the required page was deleted and only two pages remained in the document as shown below.
