how to extract image from PDF in java:
public static void readImageFromPDF(String filePath, String imagePath) {
try {
// 加载PDF文件
PDDocument document = PDDocument.load(filePath);
// 将PDF根据每页读入到list中
List<?> pages = document.getDocumentCatalog().getAllPages();
Iterator<?> iter = pages.iterator();
while (iter.hasNext()) {
PDPage page = (PDPage) iter.next();
PDResources resources = page.getResources();
// 取出图片
Map<?, ?> images = resources.getImages();
if (images != null) {
Iterator<?> imageIter = images.keySet().iterator();
while (imageIter.hasNext()) {
String key = (String) imageIter.next();
PDXObjectImage image = (PDXObjectImage) images.get(key);
File file = getUniqueFileName(key, image.getSuffix(), imagePath);
System.out.println("file name: " + file.getName());
// 将图片写入到已经定义好的文件中
image.write2file(file);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成唯一的图片名称
* @param prefix 图片的前缀
* @param suffix 图片的后缀,也即图片的格式
* @param imagePath 图片的存储路径
* @return 返回结果为创建好的文件
*/
private static File getUniqueFileName(String prefix, String suffix, String imagePath) {
String uniqueName = null;
File f = null;
while (f == null || f.exists()) {
uniqueName = prefix + "-" + imageCounter;
f = new File(imagePath + uniqueName + "." + suffix);
imageCounter++;
}
return f;
}
分享到:
相关推荐
本书以初学者为起点,循序渐进地介绍了面向对象的Java编程语言,系统地讨论了Java的基本概念和编程技术。全书共分为18章,首先从基本的Java理论开始,讲解了Java的基本数据类型和控制结构,Java中的方法、数组和字符...
How to learn English.pdf
How To Get Input From User In Java Program
How to read a book efficiently. Part one The dimensions of reading Part two The third level of reading Part three Approaches to different kinds of reading Part four The ultimate goals of reading
### Java How to Program, Early Objects, 11th Edition #### 核心知识点概览 本书《Java How to Program, Early Objects, 11th Edition》由Deitel出版社出版,是一本面向初学者和有一定编程基础的学习者介绍Java...
Learn to use the Java Persistence API (JPA) and other related APIs as found in the Java EE 8 platform from the perspective of one of the specification creators. A one-of-a-kind resource, this in-depth...
How to invoke Java web service in ASP
《股票大作手操盘术》是传奇交易员杰西·利弗莫尔(Jesse Livermore)在1940年所著的一本经典交易书籍。这本书详细阐述了他在股票市场的具体交易策略和方法,虽然已经过去了很多年,但其交易理念至今仍然对投资者...
Functional Programming in Java How functional techniques improve your Java programs 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传...
how to work with crystal report in sap business one
环境:1.通过eclipse创建maven项目,最终项目目录如下:从pdf文件中提取文本进行打印输出public class ReadPdf {public s
这个是合集~一般是这里最全的了 另外加的 PDF是英文的 .为 GCC4.1的HOWTO <br>目录 <br>ADSM-Backup-html<br>Advocacy<br>Automount-html<br>Battery-Powered-html<br>BootPrompt-HOWTO<br>Bridge-......
C How to Program(8th) 英文无水印pdf 第8版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
The author is Paul Deitel, Oracle Java Champion, you can learn a lot about Java programming skill from this book.
How to automate Excel from MFC and Visual C++ 2005 or Visual C++ .NET to fill or obtain data in a range using arrays