Pdfbox 简明教程

PDFBox - Adding Text

在上一个章节中,我们讨论了如何向 PDF 文档添加页面。在本章节中,我们将讨论如何向现有 PDF 文档添加文本。

In the previous chapter, we discussed how to add pages to a PDF document. In this chapter, we will discuss how to add text to an existing PDF document.

Adding Text to an Existing PDF Document

您可以使用 PDFBox 库向文档添加内容,这为您提供了名为 PDPageContentStream 的类,其中包含在 PDF 文档页面中插入文本、图片和其他类型内容的必要方法。

You can add contents to a document using the PDFBox library, this provides you a class named PDPageContentStream which contains the required methods to insert text, images, and other types of contents in a page of a PDFDocument.

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

Following are the steps to create an empty document and add contents to a page in it.

Step 1: Loading an Existing Document

您可以使用 PDDocument 类的 load() 方法加载现有文档。因此,实例化此类并按如下所示加载所需文档。

You can load an existing document using the load() method of the PDDocument class. Therefore, instantiate this class and load the required document as shown below.

File file = new File("Path of the document");
PDDocument doc = document.load(file);

Step 2: Getting the Required Page

您可以使用 getPage() 方法获取文档中所需的页面。按如下所示将索引传递给此方法来检索所需页面的对象。

You can get the required page in a document using the getPage() method. Retrieve the object of the required page by passing its index to this method as shown below.

PDPage page = doc.getPage(1);

Step 3: Preparing the Content Stream

您可以使用类 PDPageContentStream 的对象插入多种数据元素。您需要将文档对象和页面对象传递给这个类的构造函数,因此,实例化这个类需要传递前面步骤创建的这两个对象,如下所示。

You can insert various kinds of data elements using the object of the class PDPageContentStream. You need to pass the document object and the page object to the constructor of this class therefore, instantiate this class by passing these two objects created in the previous steps as shown below.

PDPageContentStream contentStream = new PDPageContentStream(doc, page);

Step 4: Beginning the Text

在向 PDF 文档插入文本时,您可以使用 PDPageContentStream 类中的 beginText() 和 endText() 方法指定文本的起始点和结束点,如下所示。

While inserting text in a PDF document, you can specify the start and end points of the text using the beginText() and endText() methods of the PDPageContentStream class as shown below.

contentStream.beginText();
………………………..
code to add text content
………………………..
contentStream.endText();

因此,按如下所示使用 beginText() 方法开始文本。

Therefore, begin the text using the beginText() method as shown below.

contentStream.beginText();

Step 5: Setting the Position of the Text

通过使用 newLineAtOffset() 方法,您可以在页面中的内容流上设置位置。

Using the newLineAtOffset() method, you can set the position on the content stream in the page.

//Setting the position for the line
contentStream.newLineAtOffset(25, 700);

Step 6: Setting the Font

您可以使用 PDPageContentStream 类的 setFont() 方法将文本字体设置为所需的样式,如下所示。您需要向此方法传递字体类型和大小。

You can set the font of the text to the required style using the setFont() method of the PDPageContentStream class as shown below. To this method you need to pass the type and size of the font.

contentStream.setFont( font_type, font_size );

Step 7: Inserting the Text

您可以使用 PDPageContentStream 类的 ShowText() 方法将文本插入到页面中,如下所示。该方法以字符串的形式接受所需的文本。

You can insert the text into the page using the ShowText() method of the PDPageContentStream class as shown below. This method accepts the required text in the form of string.

contentStream.showText(text);

Step 8: Ending the Text

插入文本后,您需要使用 PDPageContentStream 类的 endText() 方法结束文本,如下所示。

After inserting the text, you need to end the text using the endText() method of the PDPageContentStream class as shown below.

contentStream.endText();

Step 9: Closing the PDPageContentStream

按如下所示使用 close() 方法关闭 PDPageContentStream 对象。

Close the PDPageContentStream object using the close() method as shown below.

contentstream.close();

Step 10: Saving the Document

添加所需内容后,按如下所示使用 PDDocument 类的 save() 方法保存 PDF 文档。

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

doc.save("Path");

Step 11: Closing the Document

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

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

doc.close();

Example

本例演示如何向文档中的页面添加内容。在这里,我们将创建一个 Java 程序以加载名为 my_doc.pdf 的 PDF 文档,该文档保存在路径 C:/PdfBox_Examples/ 中,并向其中添加一些文本。将该代码保存在名为 AddingContent.java 的文件中。

This example demonstrates how to add contents to a page in a document. Here, we will create a Java program to load the PDF document named my_doc.pdf, which is saved in the path C:/PdfBox_Examples/, and add some text to it. Save this code in a file with name AddingContent.java.

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

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

      //Loading an existing document
      File file = new File("C:/PdfBox_Examples/my_doc.pdf");
      PDDocument document = PDDocument.load(file);

      //Retrieving the pages of the document
      PDPage page = document.getPage(1);
      PDPageContentStream contentStream = new PDPageContentStream(document, page);

      //Begin the Content stream
      contentStream.beginText();

      //Setting the font to the Content stream
      contentStream.setFont(PDType1Font.TIMES_ROMAN, 12);

      //Setting the position for the line
      contentStream.newLineAtOffset(25, 500);

      String text = "This is the sample document and we are adding content to it.";

      //Adding text in the form of string
      contentStream.showText(text);

      //Ending the content stream
      contentStream.endText();

      System.out.println("Content added");

      //Closing the content stream
      contentStream.close();

      //Saving the document
      document.save(new File("C:/PdfBox_Examples/new.pdf"));

      //Closing the document
      document.close();
   }
}

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

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

javac AddingContent.java
java AddingContent

在执行时,上述程序会将给定的文本添加到文档并显示以下消息。

Upon execution, the above program adds the given text to the document and displays the following message.

Content added

如果您在指定路径中验证 PDF 文档 new.pdf ,您可以观察到给定的内容已添加到文档中,如下所示。

If you verify the PDF Document new.pdf in the specified path, you can observe that the given content is added to the document as shown below.

adding text