Pdfbox 简明教程

PDFBox - Adding Pages

在上一章中,我们已经了解了如何创建 PDF 文档。创建 PDF 文档后,您需要向其中添加页面。现在,我们来了解如何在 PDF 文档中添加页面。

In the previous chapter, we have seen how to create a PDF document. After creating a PDF document, you need to add pages to it. Let us now understand how to add pages in a PDF document.

Adding Pages to a PDF Document

您可以通过实例化 PDPage 类创建空页面,并使用 PDDocument 类的 addPage() 方法将其添加到 PDF 文档中。

You can create an empty page by instantiating the PDPage class and add it to the PDF document using the addPage() method of the PDDocument class.

以下是创建空文档并向其中添加页面的步骤。

Following are the steps to create an empty document and add pages to it.

Step 1: Creating an Empty Document

通过如下所示实例化 PDDocument 类来创建一个空 PDF 文档。

Create an empty PDF document by instantiating the PDDocument class as shown below.

PDDocument document = new PDDocument();

Step 2: Creating a Blank Page

PDPage 类表示 PDF 文档中的页面,因此您可以通过实例化此类来创建一个空页面,如下面的代码块所示。

The PDPage class represents a page in the PDF document therefore, you can create an empty page by instantiating this class as shown in the following code block.

PDPage my_page = new PDPage();

Step 3: Adding Page to the Document

您可以使用 PDDocument 类的 addPage() 方法将页面添加到 PDF 文档中。您需要将 PDPage 对象作为参数传递给此方法。

You can add a page to the PDF document using the addPage() method of the PDDocument class. To this method you need to pass the PDPage object as a parameter.

因此,将上一步中创建的空白页面添加到 PDDocument 对象,如下面的代码块所示。

Therefore, add the blank page created in the previous step to the PDDocument object as shown in the following code block.

document.addPage(my_page);

通过这种方式,您可以向 PDF 文档中添加任意数量的页面。

In this way you can add as many pages as you want to a PDF document.

Step 4: Saving the Document

添加完所有页面后,使用 PDDocument 类的 save() 方法保存 PDF 文档,如下面的代码块所示。

After adding all the pages, save the PDF document using the save() method of the PDDocument class as shown in the following code block.

document.save("Path");

Step 5: Closing the Document

最后,使用 PDDocument 类的 close() 方法关闭文档,如下所示。

Finally close the document using the close() method of the PDDocument class as shown below.

document.close();

Example

此示例演示了如何创建 PDF 文档并向其中添加页面。在此,我们将创建一个名为 my_doc.pdf 的 PDF 文档,并进一步向其中添加 10 个空白页面,并将其保存在路径 C:/PdfBox_Examples/ 中。将此代码保存在名为 Adding_pages.java. 的文件中

This example demonstrates how to create a PDF Document and add pages to it. Here we will create a PDF Document named my_doc.pdf and further add 10 blank pages to it, and save it in the path C:/PdfBox_Examples/. Save this code in a file with name Adding_pages.java.

package document;

import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

public class Adding_Pages {

   public static void main(String args[]) throws IOException {

      //Creating PDF document object
      PDDocument document = new PDDocument();

      for (int i=0; i<10; i++) {
         //Creating a blank page
         PDPage blankPage = new PDPage();

         //Adding the blank page to the document
         document.addPage( blankPage );
      }

      //Saving the document
      document.save("C:/PdfBox_Examples/my_doc.pdf");
      System.out.println("PDF created");

      //Closing the document
      document.close();

   }
}

使用以下命令从命令提示符编译并执行已保存的 Java 文件 −

Compile and execute the saved Java file from the command prompt using the following commands −

javac Adding_pages.java
java Adding_pages

执行后,上述程序会创建一个空白页面的 PDF 文档,显示以下消息 −

Upon execution, the above program creates a PDF document with blank pages displaying the following message −

PDF created

如果您验证指定的路径,则可以在下面的屏幕截图中找到创建的 PDF 文档。

If you verify the specified path, you can find the created PDF document as shown in the following screenshot.

create document