需求
将 pdf 拆分成多个图片 且清晰
依赖
<dependency>
<groupId>com.kenai.nbpwr</groupId>
<artifactId>com-sun-pdfview</artifactId>
<version>1.0.5-201003191900</version>
<type>nbm</type>
</dependency>
具体代码
package com.example.demo.utils;
import com.example.demo.pojo.PdfImg;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
/**
* <p>Title : Pdf2Image </p>
* <p>Description : PDF 转换 png 图片格式 </p>
*
* @author huifer
* @date 2019 年 1 月 2 日
*/
public class Pdf2Image {
private static final int BUFFER_SIZE = 2 * 1024;
public static void main(String[] args) throws Exception {
String read = “D:\\qfile\\ 科技查新报告(智慧排涝).pdf”;
ArrayList<File> strings = pdf2img(read);
String newF = “E:\\mck\\PDF2\\fa\\” + read.split(“\\\\”)[read.split(“\\\\”).length – 1]
+ “.zip”;
PdfImg pdfImg = new PdfImg();
pdfImg.setPdf(read);
pdfImg.setZip(newF);
System.out.println(pdfImg);
FileOutputStream fos2 = new FileOutputStream(new File(newF));
toZip(strings, fos2);
}
public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
byte[] buf = new byte[BUFFER_SIZE];
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println(“ 压缩完成,耗时:” + (end – start) + ” ms”);
// 删除压缩列表中的文件
for (int i = 0; i < srcFiles.size(); i++) {
srcFiles.get(i).delete();
}
} catch (Exception e) {
throw new RuntimeException(“zip error from ZipUtils”, e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static ArrayList<File> pdf2img(String read) {
ArrayList<File> ps = new ArrayList<>();
try {
String filePath = read;
String[] split = filePath.split(“\\\\”);
File file = new File(filePath);
RandomAccessFile raf;
raf = new RandomAccessFile(file, “r”);
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);
int num = pdffile.getNumPages();
for (int i = 0; i < num; i++) {
PDFPage page = pdffile.getPage(i);
int width = (int) page.getBBox().getWidth();
int height = (int) page.getBBox().getHeight();
Rectangle rect = new Rectangle(0, 0, width, height);
int rotation = page.getRotation();
Rectangle rect1 = rect;
if (rotation == 90 || rotation == 270) {
rect1 = new Rectangle(0, 0, rect.height, rect.width);
}
BufferedImage img = (BufferedImage) page.getImage(
rect.width *2, rect.height*2,
rect1,
null,
true,
true
);
File file1 = new File(
“E:\\mck\\PDF2\\fa\\” + split[split.length – 1] + “-” + i + “.png”);
ps.add(file1);
ImageIO.write(img, “png”, file1);
}
} catch (FileNotFoundException e1) {
System.err.println(e1.getLocalizedMessage());
} catch (IOException e) {
System.err.println(e.getLocalizedMessage());
}
return ps;
}
}