【发布时间】:2018-01-15 22:56:30
【问题描述】:
朋友们,我使用的是 PDFBox 2.0.6。我已经成功地从 pdf 文件中提取图像,但现在它正在为单个 pdf 页面创建图像。但问题是可能有任何不。 pdf 页面中的图像,我希望每个嵌入的图像都应该被提取为单个图像本身。
这是代码,
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 DemoPdf {
public static void main(String args[]) throws Exception {
//Loading an existing PDF document
File file = new File("C:/Users/ADMIN/Downloads/Vehicle_Photographs.pdf");
PDDocument document = PDDocument.load(file);
//Instantiating the PDFRenderer class
PDFRenderer renderer = new PDFRenderer(document);
File imageFolder = new File("C:/Users/ADMIN/Desktop/image");
for (int page = 0; page < document.getNumberOfPages(); ++page) {
//Rendering an image from the PDF document
BufferedImage image = renderer.renderImage(page);
//Writing the image to a file
ImageIO.write(image, "JPEG", new File(imageFolder+"/" + page +".jpg"));
System.out.println("Image created"+ page);
}
//Closing the document
document.close();
}
}
是否可以在 PDFBox 中将所有嵌入的图像提取为单独的图像,谢谢
【问题讨论】: