网站首页 > java教程 正文
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-06 10.JAVA向PDF中添加多行文本(PDFBOX)
- 2025-07-06 提取PDF中的表格数据——tabula-py库
- 2025-07-06 一次Java内存占用高的排查案例,解释了我对内存问题的所有疑问
- 2025-07-06 已跪,Java全能笔记爆火,分布式/开源框架/微服务/性能调优全有
- 2025-07-06 一线大厂Java八股文合集PDF版分享,内容多达700多页
- 2025-07-06 【Java面试必问】 Spring Boot面试题 35 问(附PDF)
- 2025-07-06 阿里Nacos惊爆安全漏洞,火速升级!(附修复建议)
- 2025-07-06 一文看懂 ZooKeeper,面试再也不用背八股(文末送PDF)
- 2025-07-06 15.将一个给定的PDF文档拆分为多个文档(JAVA+PDFBOX)
- 2025-07-06 7 用Java获取页码及删除页面(PDFBOX)
你 发表评论:
欢迎- 最近发表
-
- 五,网络安全IDA Pro反汇编工具初识及逆向工程解密实战
- 「JAVA8」- Lambda 表达式(java lambda表达式原理)
- 深入探讨Java代码保护:虚拟机保护技术的新时代
- Nginx反向代理原理详解(图文全面总结)
- 逆向拆解日本IT,哪些Java技术栈薪资溢价高
- mybatis 逆向工程使用姿势不对,把表清空了,心里慌的一比
- Spring Boot集成ProGuard轻松实现Java 代码混淆, Java 应用固若金汤
- 从 Java 代码逆向工程生成 UML 类图和序列图
- 人与人相处:尊重是标配,靠谱是高配,厚道是顶配
- Windows系统安装日期如何修改(windows10怎么修改安装日期)
- 标签列表
-
- java反编译工具 (77)
- java反射 (57)
- java接口 (61)
- java随机数 (63)
- java7下载 (59)
- java数据结构 (61)
- java 三目运算符 (65)
- java对象转map (63)
- Java继承 (69)
- java字符串替换 (60)
- 快速排序java (59)
- java并发编程 (58)
- java api文档 (60)
- centos安装java (57)
- java调用webservice接口 (61)
- java深拷贝 (61)
- 工厂模式java (59)
- java代理模式 (59)
- java.lang (57)
- java连接mysql数据库 (67)
- java重载 (68)
- java 循环语句 (66)
- java反序列化 (58)
- java时间函数 (60)
- java是值传递还是引用传递 (62)
本文暂时没有评论,来添加一个吧(●'◡'●)