//import com.spire.pdf.PdfDocument;
/**
*这种方法如果想等比例的转图片得经过抽图这一步骤,也可能是有方法,但是我不知道
*/
public String pdfToImage(String PdfFilePath,int width,int height) throws IOException {
// 加载PDF文件
if(width == 0) {
width = 1043;
}
if(height == 0) {
height = 1501;
}
// System.out.println("开始读pdf文件");
File file = new File(PdfFilePath);
String path = "";
int dot = file.getName().lastIndexOf('.');
// System.out.println("dot:" + dot);
String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
// System.out.println("imagePDFName:" + imagePDFName);
PdfDocument doc = new PdfDocument();
doc.loadFromFile(PdfFilePath);
// 保存PDF的每一页到图片
BufferedImage image;
for (int i = 0; i < doc.getPages().getCount(); i++) {
String imgFilePathPrefix = historyDataPlugin.pdfPicPath + File.separator + "temp" + File.separator + imagePDFName + ".png";
image = doc.saveAsImage(i);
File fileImg=new File(imgFilePathPrefix);
ImageIO.write(image, "PNG", fileImg);
// path = imgFilePathPrefix;
String name=fileImg.getName();
try{
BufferedImage input = ImageIO.read(fileImg);
Image big = input.getScaledInstance(Integer.valueOf(width), Integer.valueOf(height),Image.SCALE_DEFAULT);
BufferedImage inputbig = new BufferedImage(Integer.valueOf(width), Integer.valueOf(height),BufferedImage.TYPE_INT_BGR);
inputbig.getGraphics().drawImage(input, 0, 0, Integer.valueOf(width), Integer.valueOf(height), null); //画图
File file2 =new File(historyDataPlugin.pdfPicPath); //此目录保存缩小后的关键图
if(file2.exists()){
// System.out.println("多级目录已经存在不需要创建!!");
}else{
//如果要创建的多级目录不存在才需要创建。
file2.mkdirs();
}
ImageIO.write(inputbig, "PNG", new File(historyDataPlugin.pdfPicPath+ File.separator+name)); //将其保存在C:/imageSort/targetPIC/下
path = historyDataPlugin.pdfPicPath+ File.separator+name;
} catch (Exception ex) {
ex.printStackTrace();
}
}
doc.close();
return path;
}
/***
* PDF文件转PNG图片,全部页数
* 这种方法对于部分pdf来说,会转图片失败
* @param PdfFilePath
* pdf完整路径
* @param imgFilePath
* 图片存放的文件夹
* @param dpi
* dpi越大转换后越清晰,相对转换速度越慢
* @return
* @throws IOException
*/
public String pdf2Image(String PdfFilePath/* , int dpi */) throws IOException {
System.out.println("开始读pdf文件");
File file = new File(PdfFilePath);
String path = "";
PDDocument pdDocument;
String imgPDFPath = file.getParent();
int dot = file.getName().lastIndexOf('.');
System.out.println("dot:" + dot);
String imagePDFName = file.getName().substring(0, dot); // 获取图片文件名
System.out.println("imagePDFName:" + imagePDFName);
// String imgFolderPath = null;
// if (dstImgFolder.equals("")) {
// imgFolderPath = pdfPath + File.separator + imagePDFName;// 获取图片存放的文件夹路径
// } else {
// imgFolderPath = dstImgFolder + File.separator + imagePDFName;
// }
System.out.println("imgFolderPath:" + historyDataPlugin.pdfPicPath);
// if (createDirectory(imgFolderPath)) {
pdDocument = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(pdDocument);
/* dpi越大转换后越清晰,相对转换速度越慢 */
PdfReader reader = new PdfReader(PdfFilePath);
int pages = reader.getNumberOfPages();
StringBuffer imgFilePath = null;
for (int i = 0; i < pages; i++) {
String imgFilePathPrefix = historyDataPlugin.pdfPicPath + File.separator + imagePDFName;
imgFilePath = new StringBuffer();
imgFilePath.append(imgFilePathPrefix);
imgFilePath.append(".png");
File dstFile = new File(imgFilePath.toString());
path = imgFilePath.toString();
// 根据pdf大小等比例转图片,此处第二个参数设置为1就是等比例抽取
BufferedImage image = renderer.renderImage(i, 1F);
// BufferedImage image = renderer.renderImageWithDPI(i, dpi);
ImageIO.write(image, "png", dstFile);
}
System.out.println("PDF文档转PNG图片成功!");
// } else {
// System.out.println("PDF文档转PNG图片失败:" + "创建" + imgFolderPath + "失败");
// }
return path;
}
使用的jar包:
fontbox-2.0.1.jar
pdfbox-2.0.1.jar
spire.pdf-2.2.0.jar