企业项目管理、ORK、研发管理与敏捷开发工具平台

网站首页 > 精选文章 正文

17.从PDF文档生成图像(JAVA+PDFBOX)

wudianyun 2025-07-01 23:43:01 精选文章 6 ℃

Generating an Image from a PDF Document 从PDF文档生成图像

[翻译]
PDFBox库提供了一个名为PDFRenderer的类,该类可以将PDF文档渲染为AWT BufferedImage。

[原文]
PDFBox library provides you a class named PDFRenderer which renders a PDF document into an AWT BufferedImage.

  • render /'rendr/ 渲染
  • BufferedImage /'bfrd 'md/ 缓冲图像

[翻译]
以下是将PDF文档生成为图像的步骤。

[原文]
Following are the steps to generate an image from a PDF document.

  • generate /'denret/ 生成
  • 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.

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

Step 2: Instantiating the PDFRenderer Class 步骤2:实例化PDFRenderer类

[翻译]
名为PDFRenderer的类可以将PDF文档渲染为AWT BufferedImage。因此,您需要按照以下方式实例化此类。该类的构造函数接受一个文档对象;请传递在上一步中创建的文档对象,如下所示。

[原文]
The class named PDFRenderer renders a PDF document into an AWT BufferedImage. Therefore, you need to instantiate this class as shown below. The constructor of this class accepts a document object; pass the document object created in the previous step as shown below.

PDFRenderer renderer = new PDFRenderer(document);
  • instantiate /n'staeniet/ 实例化
  • constructor /kn'strktr/ 构造函数

Step 3: Rendering Image from the PDF Document 步骤3:从PDF文档渲染图像

[翻译]
您可以使用Renderer类的renderImage()方法渲染特定页面中的图像。在此方法中,您需要传递包含要渲染图像的页面索引。

[原文]
You can render the image in a particular page using the method renderImage() of the Renderer class, to this method you need to pass the index of the page where you have the image that is to be rendered.

BufferedImage image = renderer.renderImage(0);
  • render /'rendr/ 渲染
  • index /'ndeks/ 索引

Step 4: Writing the Image to a File 步骤4:将图像写入文件

[翻译]
您可以使用write()方法将上一步渲染的图像写入文件。在此方法中,您需要传递三个参数:

  • 渲染的图像对象。
  • 表示图像类型的字符串(jpg或png)。
  • 需要保存提取图像的文件对象。

[原文]
You can write the image rendered in the previous step to a file using the write() method. To this method, you need to pass three parameters -

  • The rendered image object.
  • String representing the type of the image (jpg or png).
  • File object to which you need to save the extracted image.
ImageIO.write(image, "JPEG", new File("C:/PdfBox_Examples/myimage.jpg"));
  • write /rat/ 写入
  • parameter /p'raemtr/ 参数
  • extract /k'straekt/ 提取

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

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

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

document.close();
  • close /kloz/ 关闭

Example 示例

[翻译]
假设我们有一个名为sample.pdf的PDF文档,位于C:\PdfBox_Examples\路径中,该文档的第一页包含一个图像,如下图所示。

[原文]
Suppose, we have a PDF document sample.pdf in the path C:\PdfBox_Examples\ and this contains an image in its first page as shown below.

Sample Image

  • suppose /s'poz/ 假设
  • contain /kn'ten/ 包含

[翻译]
本示例演示如何将上述PDF文档转换为图像文件。在此,我们将提取PDF文档第一页中的图像,并将其保存为myimage.jpg。将此代码保存为PdfToImage.java。

[原文]
This example demonstrates how to convert the above PDF document into an image file. Here, we will retrieve the image in the 1st page of the PDF document and save it as myimage.jpg. Save this code as PdfToImage.java

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
public class PdfToImage {

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

      //Loading an existing PDF document
      File file = new File("C:/PdfBox_Examples/sample.pdf");
      PDDocument document = PDDocument.load(file);
       
      //Instantiating the PDFRenderer class
      PDFRenderer renderer = new PDFRenderer(document);

      //Rendering an image from the PDF document
      BufferedImage image = renderer.renderImage(0);

      //Writing the image to a file
      ImageIO.write(image, "JPEG", new File("C:/PdfBox_Examples/myimage.jpg"));
       
      System.out.println("Image created");
       
      //Closing the document
      document.close();

   }
}
  • demonstrate /'demnstret/ 演示
  • retrieve /r'triv/ 检索
  • convert /kn'vrt/ 转换

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

[原文]
Compile and execute the saved Java file from the command prompt using the following commands.

javac PdfToImage.java 
java PdfToImage
  • compile /km'pal/ 编译
  • execute /'ekskjut/ 执行
  • command /k'maend/ 命令
  • prompt /prɑmpt/ 提示符

[翻译]
执行后,上述程序会提取给定PDF文档中的图像,并显示以下消息。

[原文]
Upon execution, the above program retrieves the image in the given PDF document displaying the following message.

Image created
  • upon /'pɑn/ 在……时
  • display /d'sple/ 显示

[翻译]
如果您检查指定路径,可以观察到图像已生成并保存为myimage.jpg,如下图所示。

[原文]
If you verify the given path, you can observe that the image is generated and saved as myimage.jpg as shown below.

Generateimage

  • verify /'verfa/ 验证
  • observe /b'zrv/ 观察
最近发表
标签列表