11.如何从现有 PDF 文档中读取文本(JAVA+PDFBOX)

yumo6661周前 (07-14)技术文章12

[翻译] 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. [原文] 上一章中,我们已经了解了如何向现有 PDF 文档添加文本。在本章中,我们将讨论如何从现有 PDF 文档中读取文本。

Extracting Text from an Existing PDF Document 从现有 PDF 文档中提取文本

[翻译] 提取文本是 PDFBox 库的主要功能之一。您可以使用 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.

  • extract /k'straekt/ 提取
  • feature /'fitr/ 功能
  • library /'labrri/ 库
  • method /'meθd/ 方法

[翻译] 以下是从现有 PDF 文档中提取文本的步骤。

[原文] Following are the steps to extract text from an existing PDF document.

  • following /'fɑlo/ 以下的
  • step /step/ 步骤

Step 1: Loading an Existing PDF Document 步骤 1:加载现有 PDF 文档

[翻译] 使用 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.

  • load /lod/ 加载
  • static /'staetk/ 静态的
  • parameter /p'raemtr/ 参数
  • invoke /n'vok/ 调用
File file = new File("path of the document")
PDDocument document = PDDocument.load(file);

Step 2: Instantiate the PDFTextStripper Class 步骤 2:实例化 PDFTextStripper 类

[翻译] PDFTextStripper 类提供了从 PDF 文档中检索文本的方法,因此,请按照以下方式实例化此类。

[原文] The PDFTextStripper class provides methods to retrieve text from a PDF document therefore, instantiate this class as shown below.

  • instantiate /n'staeniet/ 实例化
  • retrieve /r'triv/ 检索
  • therefore /'derfr/ 因此
PDFTextStripper pdfStripper = new PDFTextStripper();

Step 3: Retrieving the Text 步骤 3:检索文本

[翻译] 您可以使用 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.

  • content /'kɑntent/ 内容
  • pass /paes/ 传递
  • return /r'trn/ 返回
  • string /str/ 字符串
String text = pdfStripper.getText(document);

Step 4: Closing the Document 步骤 4:关闭文档

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

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

  • finally /'fanli/ 最后
  • close /kloz/ 关闭
document.close();

Example 示例

[翻译] 假设我们有一个包含一些文本的 PDF 文档,如下图所示。

[原文] Suppose, we have a PDF document with some text in it as shown below.

  • suppose /s'poz/ 假设
  • document /'dɑkjumnt/ 文档

[翻译] 本示例演示如何从上述 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.

  • demonstrate /'demnstret/ 演示
  • mention /'menn/ 提及
  • save /sev/ 保存
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.

  • compile /km'pal/ 编译
  • execute /'ekskjut/ 执行
  • command /k'maend/ 命令
  • prompt /prɑmpt/ 提示符
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.

  • upon /'pɑn/ 在……时
  • display /d'sple/ 显示
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.

相关文章

6.Java中加载PDF文档(PDFBOX,并对其进行一些操作如加一页)

[翻译]在前面的示例中,您已经了解了如何创建新文档并向其中添加页面。本章将教您如何加载系统中已有的PDF文档,并对其进行一些操作。[原文]In the previous examples, you h...

编程基础!Java程序员的10道XML面试题

| 责编:王迪如今,面对web开发人员的Java各种面试中,XML面试题在各种编程工作的面试中很常见。XML是一种成熟的技术,经常作为从一个平台到其他平台传输数据的标准。XML面试问题包括用于转换XM...

终于拿到阿里架构师分享的557页深入理解Java模块系统文档

前言Java 9通过模块系统将零散的拼图拼凑到了一起,模块系统成了Java平台的核心而不是扩展功能。Java的模块系统必须有所妥协。它不仅要保持对大量现有代码的支持,使其不至于破坏现有的生态系统,还...

2020年最新Java全套教程注解(java基础注解)

注解1. 注解概述1.1 注解解释注释: 解释代码,给程序员看 注解: Java语言中的类、方法、变量、参数和包等都可以被标注。和Javadoc不同,Java标注可以通过反射获取标注内容。在编译...

3W字详解Java集合!这可能是你能看到的最详细的一篇文章了

数据结构作为每一个开发者不可回避的问题,而 Java 对于不同的数据结构提供了非常成熟的实现,这一个又一个实现既是面试中的难点,也是工作中必不可少的工具,在此,笔者经历漫长的剖析,将其抽丝剥茧的呈现出...