在Java编程中,如何使用java将PDF文档生成图片?
以下是使用使用java将PDF文档生成图片的示例程序。
package com.yiibai;
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 ExtractImageFromPdf {
public static void main(String args[]) throws Exception {
String workpath = "F:/worksp/javaexamples/java_apache_pdf_box/";
// Loading an existing PDF document
File file = new File(workpath+"InsertImage_OP.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(workpath+"ExtractImage_OP.png"));
System.out.println("Image created");
// Closing the document
document.close();
}
}
执行上面示例代码,得到以下结果 -
Image created