导入pom文件
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.22</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.31</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>net.sf.cssbox</groupId>
<artifactId>pdf2dom</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.22</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.49</version>
</dependency>
整个pdf转换成图片
package pdfToPng;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Pdf2Png {
public static void pdf2png(String fileAddress, String filename, String type) {
long startTime = System.currentTimeMillis();
File file = new File(fileAddress + "\\" + filename + ".pdf");
try {
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144);
ImageIO.write(image, type, new File(fileAddress + "\\" + filename + "_" + (i + 1) + "." + type));
}
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + ((endTime - startTime) / 1000.0) + "秒");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
pdf2png("C:\\Users\\MES\\Desktop\\test1", "1", "png");
}
}
*确定起始页和终止页
package pdfToPng;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class Pdf3Png {
public static void pdf3png(String fileAddress, String filename, int indexOfStart, int indexOfEnd, String type) {
long startTime = System.currentTimeMillis();
File file = new File(fileAddress + "\\" + filename + ".pdf");
try {
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = indexOfStart; i < indexOfEnd; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 144);
ImageIO.write(image, type, new File(fileAddress + "\\" + filename + "_" + (i + 1) + "." + type));
}
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + ((endTime - startTime) / 1000.0) + "秒");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
pdf3png("C:\\Users\\MES\\Desktop\\", "1", 1, 1, "png");
}
}
使用文件流整个pdf转换成图片
package pdfToPng;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
public class Pdf4Png {
public static void pdfToImage(String fileAddress, String filename, String type) {
long startTime = System.currentTimeMillis();
List<Map<String, String>> list = new ArrayList<>();
Map<String, String> resultMap = null;
PDDocument pdDocument = null;
String fileName = null;
String imgPath = null;
try {
File FilePath = new File(fileAddress + "\\" + filename + ".pdf");
FileInputStream inputStream = new FileInputStream(FilePath);
int dpi = 296;
pdDocument = PDDocument.load(inputStream);
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pageCount = pdDocument.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
resultMap = new HashMap<>();
fileName = filename + "_" + (i + 1) + "." + type;
imgPath = fileAddress + "\\" + fileName;
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, type, new File(imgPath));
resultMap.put("fileName", fileName);
resultMap.put("filePath", imgPath);
list.add(resultMap);
}
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + ((endTime - startTime) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pdDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws FileNotFoundException {
pdfToImage("C:\\Users\\MES\\Desktop\\", "1", "png");
}
}