pdf转png图片

maven依赖

    <dependencies>
            <!--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.20</version>
            </dependency>
    </dependencies>

Java代码:

  private static void pdf2image1() {
        File file = new File("C:/Users/v/Desktop/doc/测试.html.pdf");
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for(int i=0;i<pageCount;i++){
                BufferedImage image = renderer.renderImageWithDPI(i, 296);
//                          BufferedImage image = renderer.renderImage(i, 2.5f);
                ImageIO.write(image, "PNG", new File("C:/Users/v/Desktop/doc/pdfbox_image"+i+".png"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  

posted @ 2020-12-07 16:56  易不易  阅读(237)  评论(0编辑  收藏  举报