标签:rgs 一个 -- doc users 网上 jar mamicode out
在网上找了一下现成的转换工具,有的要收费,就自己在网上找了一个JAVA实现,参考链接:https://blog.csdn.net/romantic_pk/article/details/53019542
引入pdfbox-app.jarr依赖
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox-app --> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-app</artifactId> <version>2.0.2</version> </dependency>
新建一个测试类PDF2IMGTestByPdfbox.java
package com.theorydance.pdf2images; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.PDFRenderer; public class PDF2IMGTestByPdfbox { public static void main(String[] args) { File file = new File("C:/Users/Administrator/Desktop/Mini/ID1/卷一.pdf"); try { PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); for (int i = 0; i < pageCount; i++) { // 方式1,第二个参数是设置缩放比(即像素),DPI是每英寸的像素点,长度单位,一般WEB为72即可,照片打印用300,在这里使用72放大后比较模糊,选用144比较好 BufferedImage image = renderer.renderImageWithDPI(i, 144);// 方式2,第二个参数是设置缩放比(即像素) // BufferedImage image = renderer.renderImage(i, 2.5f); String filename = "pdfbox_image_"+i+".png"; ImageIO.write(image, "PNG", new File("C:/Users/Administrator/Desktop/Mini/ID1/img3/" + filename)); System.out.println(filename); } } catch (IOException e) { e.printStackTrace(); } } }
输出结果如图:
标签:rgs 一个 -- doc users 网上 jar mamicode out
原文地址:https://www.cnblogs.com/TheoryDance/p/11875477.html