Pdfbox 简明教程

PDFBox - Creating a PDF Document

现在,我们来了解如何使用 PDFBox 库创建 PDF 文档。

Let us now understand how to create a PDF document using the PDFBox library.

Creating an Empty PDF Document

您可以通过实例化 PDDocument 类创建一个空的 PDF 文档。您可以使用 Save() 方法按所需位置保存文档。

You can create an empty PDF Document by instantiating the PDDocument class. You can save the document in your desired location using the Save() method.

以下是要创建空的 PDF 文档的步骤。

Following are the steps to create an empty PDF document.

Step 1: Creating an Empty Document

属于 org.apache.pdfbox.pdmodel 包的 PDDocument 类是 PDFDocument 的内存表示。因此,通过实例化此类,您可以如以下代码块中所示创建一个空的 PDFDocument。

The PDDocument class that belongs to the package org.apache.pdfbox.pdmodel, is an In-memory representation of the PDFDocument. Therefore, by instantiating this class, you can create an empty PDFDocument as shown in the following code block.

PDDocument document = new PDDocument();

Step 2: Saving the Document

创建文档后,需要将该文档保存到所需路径中,您可以通过使用 PDDocument 类的 Save() 方法做到这一点。此方法接受一个字符串值作为参数,该字符串值代表要存储文档的路径。以下是 PDDocument 类 save() 方法的原型。

After creating the document, you need to save this document in the desired path, you can do so using the Save() method of the PDDocument class. This method accepts a string value, representing the path where you want to store the document, as a parameter. Following is the prototype of the save() method of the PDDocument class.

document.save("Path");

Step 3: Closing the Document

在任务完成后,最后,您需要使用 close () 方法关闭 PDDocument 对象。以下是 PDDocument 类 close() 方法的原型。

When your task is completed, at the end, you need to close the PDDocument object using the close () method. Following is the prototype of the close() method of PDDocument class.

document.close();

Example

此示例演示了 PDF 文档的创建。在此,我们将创建一个名为 my_doc.pdf 的 Java 程序以生成 PDF 文档,并将其保存在路径 C:/PdfBox_Examples/ 中。将此代码保存在名为 Document_Creation.java. 的文件中

This example demonstrates the creation of a PDF Document. Here, we will create a Java program to generate a PDF document named my_doc.pdf and save it in the path C:/PdfBox_Examples/. Save this code in a file with name Document_Creation.java.

import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;

public class Document_Creation {

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

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

      //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 Document_Creation.java
java Document_Creation

在执行以上程序时,创建了一个 PDF 文档,显示了以下消息。

Upon execution, the above program creates a PDF document displaying the following message.

PDF created

如果验证指定路径,则可以找到创建的 PDF 文档,如下所示。

If you verify the specified path, you can find the created PDF document as shown below.

my doc saved

由于这是一个空文档,因此如果尝试打开此文档,将出现提示,显示错误消息,如下面的屏幕截图所示。

Since this is an empty document, if you try to open this document, this gives you a prompt displaying an error message as shown in the following screenshot.

empty pdf