pdfbox2.0.1将pdf转图片

使用pdfbox2.0.1将pdf转图片

依赖jar(gradle)

compile group: ‘org.apache.pdfbox’, name: ‘pdfbox’, version: ‘2.0.1’
compile group: ‘org.apache.pdfbox’, name: ‘pdfbox-tools’, version: ‘2.0.1’

public class PDF2Img {

    public static void main(String[] args) {
    	Long time = System.currentTimeMillis();
        PDF2ImagPDFbox("E:\\test\\test.pdf");
        Long time1 = System.currentTimeMillis();
        System.out.println("耗时:"+(time1-time)/1000+"s");
    }

    public static void PDF2ImagPDFbox(String filePath) {
        PDDocument pdf = null;
        try {
            pdf = PDDocument.load(new File(filePath));
            PDFRenderer pdfRenderer = new PDFRenderer(pdf);
            PDPageTree pageTree = pdf.getPages();
            int pageCounter = 0;
            for(PDPage page : pageTree){
                float width = page.getCropBox().getWidth();
                float scale = 1.0f;
                if(width >720){
                    scale = 720/width;
                }
                BufferedImage bim = pdfRenderer.renderImage(pageCounter,scale,ImageType.RGB);
                ImageIOUtil.writeImage(bim, "E:\\test\\imgs\\"+(pageCounter++) + ".jpg", 300);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(笔记)