将PDF转为图片呢,有两种方式:
产品 | 特点 |
---|
Apache 的 PDF box | 速度稍慢一点,但可以接受;免费; | E-iceblue 的 Spire.PDF for Java | 转换效果很好;速度快;功能强大,支持转多种格式;收费;如果不购买,转换过后会添加一些水印文字; |
一、PDF Box的使用
1. 所需依赖
<!--PDF转图?-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.20</version>
</dependency>
<!--需要改变字体的话,加上这个-->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.9</version>
</dependency>
2. 代码实现
2.1 pdf的生成多张图片
新建一个 PdfUtil 工具类
public class PdfUtil {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PdfUtil.class);
public static void pdf2Image(String pdfFilePath, String dstImgFolder, int dpi) {
List<File> fileList = new ArrayList<File>();
File file = new File(pdfFilePath);
PDDocument pdDocument;
UUID uuid = UUID.randomUUID();
String uuId = uuid.toString();
try {
String imgFolderPath = null;
if (dstImgFolder.equals("")) {
imgFolderPath = dstImgFolder + File.separator + uuId;
} else {
imgFolderPath = dstImgFolder + File.separator + uuId;
}
if (createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
PdfReader reader = new PdfReader(pdfFilePath);
int pages = reader.getNumberOfPages();
StringBuffer imgFilePath;
for (int i = 0; i < pages; i++) {
String imgFilePathPrefix = imgPdfPath + File.separator + imgFolderPath;
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
imgFilePath.append("_");
imgFilePath.append((i + 1));
imgFilePath.append(".png");
File dstFile = new File(imgFilePath.toString());
BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, "png", dstFile);
fileList.add(dstFile);
}
log.info("PDF文档转PNG图片成功!");
return fileList;
}else {
System.out.println("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");
return null;
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
private boolean createDirectory(String folder) {
File dir = new File(folder);
if (dir.exists()) {
return true;
} else {
return dir.mkdirs();
}
}
public void delFolder(String folderPath) {
try {
delAllFile(folderPath);
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);
delFolder(path + "/" + tempList[i]);
flag = true;
}
}
return flag;
}
2.3 PDF生产成一张图片
新建一个 PdfUtil 工具类
public class PdfUtil {
public static final int DEFAULT_DPI = 150;
public static void pdf2multiImage(String pdfFile, String outPath, int dpi) {
if (ObjectUtil.isEmpty(dpi)) {
dpi = DEFAULT_DPI;
}
try (PDDocument pdf = PDDocument.load(new FileInputStream(pdfFile))) {
int actSize = pdf.getNumberOfPages();
List<BufferedImage> picList = Lists.newArrayList();
for (int i = 0; i < actSize; i++) {
BufferedImage image = new PDFRenderer(pdf).renderImageWithDPI(i, dpi, ImageType.RGB);
picList.add(image);
}
ImageUtil.yPic(picList, outPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
新建 ImageUtil 类
public class ImageUtil {
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ImageUtil.class);
public static void yPic(List<BufferedImage> picList, String outPath) {
if (picList == null || picList.size() <= 0) {
log.info("图片数组为空!");
return;
}
try {
int height = 0,
width = 0,
offsetHeight = 0,
tmpHeight = 0,
picNum = picList.size();
int[] heightArray = new int[picNum];
BufferedImage buffer = null;
List<int[]> imgRgb = new ArrayList<int[]>();
int[] tmpImgRgb;
for (int i = 0; i < picNum; i++) {
buffer = picList.get(i);
heightArray[i] = offsetHeight = buffer.getHeight();
if (i == 0) {
width = buffer.getWidth();
}
height += offsetHeight;
tmpImgRgb = new int[width * offsetHeight];
tmpImgRgb = buffer.getRGB(0, 0, width, offsetHeight, tmpImgRgb, 0, width);
imgRgb.add(tmpImgRgb);
}
offsetHeight = 0;
BufferedImage imageResult = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < picNum; i++) {
tmpHeight = heightArray[i];
if (i != 0) {
offsetHeight += tmpHeight;
}
imageResult.setRGB(0, offsetHeight, width, tmpHeight, imgRgb.get(i), 0, width);
}
File outFile = new File(outPath);
ImageIO.write(imageResult, "png", outFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
|