*******************************************************************************
**原文转自 https://www.cnblogs.com/songdongdong6/p/8627098.html
*******************************************************************************
package com.sdd.Test.PDFTest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.util.PDFImageWriter;
/**
* This example converts a PDF to a multi-page TIFF or a series of bitmap
* images, using the PDF Viewer extension.
*
* Usage is:PDFBox
*
*/
public class PDFToImage {
public static void main(String[] args) {
convertPdf2Jpg("a.pdf","D:/image/");
}
/**
*
* @param pdfFile
* @param jpdDir
*/
@SuppressWarnings("unchecked")
private static void convertPdf2Jpg(String pdfFile,String jpdDir) {
try {
InputStream is = new FileInputStream(pdfFile);
PDDocument pdf = PDDocument.load( is, true );
List<PDPage> pages = pdf.getDocumentCatalog().getAllPages();
int i=0;
for ( PDPage page : pages )
{ ++i;
BufferedImage image = page.convertToImage();
ImageIO.write(image, "jpg", new FileOutputStream(new File(jpdDir+i+".jpg")));
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unused")
private static void pdf2jpg() {
String destinationImageFormat = "jpg";
boolean success = false;
PDDocument pdf;
try {
InputStream is = new FileInputStream("D:481f389b-ec59-4b1a-94cb-e396cd2a990b.pdf");
pdf = PDDocument.load( is, true );
int resolution = 256;
String password = "";
String outputPrefix = "D:481f389b-ec59-4b1a-94cb-e396cd2a990b\\myImageFile";
PDFImageWriter imageWriter = new PDFImageWriter();
success = imageWriter.writeImage(pdf,
destinationImageFormat,
password,
1,
2,
outputPrefix,
BufferedImage.TYPE_INT_RGB,
resolution);
} catch (IOException e) {
e.printStackTrace();
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sdd.Test</groupId>
<artifactId>PDFTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>PDFTest</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jpedal/OpenViewerFX -->
<dependency>
<groupId>org.jpedal</groupId>
<artifactId>OpenViewerFX</artifactId>
<version>7.10.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.6.0</version>
</dependency>
</dependencies>
</project>