Pdfbox 简明教程
PDFBox - Reading Text
在前一章中,我们已经了解如何向现有 PDF 文档中添加文本。在本章中,我们将讨论如何从现有 PDF 文档中读取文本。
In the previous chapter, we have seen how to add text to an existing PDF document. In this chapter, we will discuss how to read text from an existing PDF document.
Extracting Text from an Existing PDF Document
提取文本是 PDF 框库的主要功能。您可以使用 PDFTextStripper 类中的 getText() 方法提取文本。此类从给定的 PDF 文档中提取所有文本。
Extracting text is one of the main features of the PDF box library. You can extract text using the getText() method of the PDFTextStripper class. This class extracts all the text from the given PDF document.
以下是从现有 PDF 文档中提取文本的步骤。
Following are the steps to extract text from an existing PDF document.
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 document = PDDocument.load(file);
Step 2: Instantiate the PDFTextStripper Class
PDFTextStripper 类提供了从 PDF 文档中检索文本的方法,因此,如下所示实例化此类。
The PDFTextStripper class provides methods to retrieve text from a PDF document therefore, instantiate this class as shown below.
PDFTextStripper pdfStripper = new PDFTextStripper();
Step 3: Retrieving the Text
您可以使用 PDFTextStripper 类中的 getText() 方法从 PDF 文档中读取/检索页面内容。对于此方法,您需要传递文档对象作为参数。此方法检索给定文档中的文本,并以字符串对象的形式返回它。
You can read/retrieve the contents of a page from the PDF document using the getText() method of the PDFTextStripper class. To this method you need to pass the document object as a parameter. This method retrieves the text in a given document and returns it in the form of a String object.
String text = pdfStripper.getText(document);
Example
假设我们有一个 PDF 文档,其中包含一些文本,如下所示。
Suppose, we have a PDF document with some text in it as shown below.
此示例演示如何从上述 PDF 文档中读取文本。在此,我们将创建一个 Java 程序,并加载名为 new.pdf 的 PDF 文档,该文档保存在 C:/PdfBox_Examples/ 路径中。将此代码保存在名为 ReadingText.java 的文件中。
This example demonstrates how to read text from the above mentioned PDF document. Here, we will create a Java program and load a PDF document named new.pdf, which is saved in the path C:/PdfBox_Examples/. Save this code in a file with name ReadingText.java.
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
public class ReadingText {
public static void main(String args[]) throws IOException {
//Loading an existing document
File file = new File("C:/PdfBox_Examples/new.pdf");
PDDocument document = PDDocument.load(file);
//Instantiate PDFTextStripper class
PDFTextStripper pdfStripper = new PDFTextStripper();
//Retrieving text from PDF document
String text = pdfStripper.getText(document);
System.out.println(text);
//Closing the document
document.close();
}
}
使用以下命令从命令提示符处编译并执行已保存的 Java 文件。
Compile and execute the saved Java file from the command prompt using the following commands.
javac ReadingText.java
java ReadingText
在执行时,上述程序检索给定 PDF 文档中的文本,并按如下所示显示它。
Upon execution, the above program retrieves the text from the given PDF document and displays it as shown below.
This is an example of adding text to a page in the pdf document. we can add as many lines
as we want like this using the ShowText() method of the ContentStream class.