package pdfCutImage;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
public class Test {
public static void testPdf2jpg() throws InvalidPasswordException, IOException {
long start = System.currentTimeMillis();
String filepath = "D:\\TT.pdf";//源文件路径
PDDocument document = null;
File pdfFile = new File(filepath);
document = PDDocument.load(pdfFile, (String) null);
int size = document.getNumberOfPages();
List<BufferedImage> piclist = new ArrayList<BufferedImage>();
String savePath = "D:\\pdfToImg\\";//输出图片路径
new File(savePath).mkdirs();
for (int i = 0; i < size; i++) {
BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i, 130, ImageType.RGB);
piclist.add(image);
FileOutputStream out = new FileOutputStream(savePath.concat("第" + (i + 1) + "页.jpg"));
ImageIO.write(image, "jpg", out);
image.flush();
out.close();
}
document.getResourceCache();
document.close();
long end = System.currentTimeMillis();
System.out.println((end - start)/1000);
}
public static void main(String[] args) {
try {
testPdf2jpg();
} catch (IOException e) {
e.printStackTrace();
}
}
}
maven pom.xml
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.9</version>
</dependency>