网站首页 > 精选文章 正文
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/ 观察
猜你喜欢
- 2025-07-01 Java中的native方法(native java api)
- 2025-07-01 JAVA工程师面试考试测试题(Java面试题HR必备)
- 2025-07-01 干货 | Java语言这10个语言特性你知道吗?
- 2025-07-01 JavaCPP快速入门(官方demo增强版)
- 2025-07-01 Java编程入门第一课:HelloWorld(java编程如何入门)
- 2025-07-01 JVM系列之ClassLoader(jvm classloader)
- 2025-07-01 深入理解Java虚拟机之自己编译JDK
- 2025-07-01 函数入口一定是main吗?为什么不指定编译生成文件名, 默认是a.out?
- 2025-07-01 新手小白学Java|零基础入门笔记|原来学Java可以这么简单
- 2025-07-01 15.将一个给定的PDF文档拆分为多个文档(JAVA+PDFBOX)
- 07-03CentOS7系统如何修改主机名(更改centos主机名)
- 07-03Ubuntu1804 及以上版本的 Coredump 相关设置
- 07-03Linux中如何修改ip地址?(linux系统怎么更改ip地址)
- 07-03Linux系统日常运维九大核心技能(linux运维都干什么)
- 07-03Linux 日志管理攻略:用 journalctl 揪出服务器安全隐患
- 07-03Linux下快速安装ollama和deepseek并使用web界面
- 07-03RockyLinux9.5下使用ollama搭建本地AI大模型DeepSeek
- 07-03Linux 下的 PM2 完整指南(linux /media)
- 最近发表
-
- CentOS7系统如何修改主机名(更改centos主机名)
- Ubuntu1804 及以上版本的 Coredump 相关设置
- Linux中如何修改ip地址?(linux系统怎么更改ip地址)
- Linux系统日常运维九大核心技能(linux运维都干什么)
- Linux 日志管理攻略:用 journalctl 揪出服务器安全隐患
- Linux下快速安装ollama和deepseek并使用web界面
- RockyLinux9.5下使用ollama搭建本地AI大模型DeepSeek
- Linux 下的 PM2 完整指南(linux /media)
- Rocky Linux 9常用命令备忘录(不定时更新)
- Rocky Linux 9 系统初始化与安全加固脚本
- 标签列表
-
- 向日葵无法连接服务器 (32)
- git.exe (33)
- vscode更新 (34)
- dev c (33)
- git ignore命令 (32)
- gitlab提交代码步骤 (37)
- java update (36)
- vue debug (34)
- vue blur (32)
- vscode导入vue项目 (33)
- vue chart (32)
- vue cms (32)
- 大雅数据库 (34)
- 技术迭代 (37)
- 同一局域网 (33)
- github拒绝连接 (33)
- vscode php插件 (32)
- vue注释快捷键 (32)
- linux ssr (33)
- 微端服务器 (35)
- 导航猫 (32)
- 获取当前时间年月日 (33)
- stp软件 (33)
- http下载文件 (33)
- linux bt下载 (33)