package msdev.card;
import com.itextpdf.text.pdf.PdfReader;
import org.apache.pdfbox.pdmodel.PDdocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class PDFToImgUtil {
public static void pdf2ImageDemo(File file, String dstImgFolder, int dpi) { PDdocument pddocument; try { String imgPDFPath = file.getParent(); int dot = file.getName().lastIndexOf('.'); // 获取图片文件名 String imagePDFName = file.getName().substring(0, dot); String imgFolderPath = null; if (dstImgFolder.equals("")) { // 获取图片存放的文件夹路径 imgFolderPath = imgPDFPath + File.separator + imagePDFName; } else { imgFolderPath = dstImgFolder + File.separator + imagePDFName; } if (createDirectory(imgFolderPath)) { pddocument = PDdocument.load(file); PDFRenderer renderer = new PDFRenderer(pddocument); StringBuffer imgFilePath = null; String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName; imgFilePath = new StringBuffer(); imgFilePath.append(imgFilePathPrefix); imgFilePath.append("_"); imgFilePath.append(String.valueOf(1)); imgFilePath.append(".png");// PNG File dstFile = new File(imgFilePath.toString()); BufferedImage image = renderer.renderImageWithDPI(0, dpi); ImageIO.write(image, "png", dstFile);// PNG System.out.println("PDF文档转JPG图片成功!"); } else { System.out.println("PDF文档转JPG图片失败:" + "创建" + imgFolderPath + "失败"); } } catch (IOException e) { e.printStackTrace(); } } public static void pdf2ImageDemo(String PdfFilePath, String dstImgFolder, int dpi) { File file = new File(PdfFilePath); PDdocument pddocument; try { String imgPDFPath = file.getParent(); int dot = file.getName().lastIndexOf('.'); // 获取图片文件名 String imagePDFName = file.getName().substring(0, dot); String imgFolderPath = null; if (dstImgFolder.equals("")) { // 获取图片存放的文件夹路径 imgFolderPath = imgPDFPath + File.separator + imagePDFName; } else { imgFolderPath = dstImgFolder + File.separator + imagePDFName; } if (createDirectory(imgFolderPath)) { pddocument = PDdocument.load(file); PDFRenderer renderer = new PDFRenderer(pddocument); PdfReader reader = new PdfReader(PdfFilePath); int pages = reader.getNumberOfPages();// 获取PDF页数 System.out.println("PDF page number is:" + pages); StringBuffer imgFilePath = null; for (int i = 0; i < pages; i++) { String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName; imgFilePath = new StringBuffer(); imgFilePath.append(imgFilePathPrefix); imgFilePath.append("_"); imgFilePath.append(String.valueOf(i + 1)); imgFilePath.append(".png");// PNG File dstFile = new File(imgFilePath.toString()); BufferedImage image = renderer.renderImageWithDPI(i, dpi); ImageIO.write(image, "png", dstFile);// PNG } System.out.println("PDF文档转JPG图片成功!"); } else { System.out.println("PDF文档转JPG图片失败:" + "创建" + imgFolderPath + "失败"); } } catch (IOException e) { e.printStackTrace(); } } private static boolean createDirectory(String folder) { File dir = new File(folder); if (dir.exists()) { return true; } else { return dir.mkdirs(); } }
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)