当前位置: 首页 > 工具软件 > javapdf > 使用案例 >

java PDF转换图片(IMG)

佴博实
2023-12-01
 public static void pdfImg(String inputPdf, long ItemId, String outPut){
        //加载PDF文件
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile(inputPdf);

        //保存PDF的每一页到图片
        BufferedImage image;
        for (int i = 0; i < doc.getPages().getCount(); i++) {
            image = doc.saveAsImage(i);
            String path = outPut+ItemId+"_img_"+(i+1)+".png";
            File file = new File( String.format(path, i));
            try {
                ImageIO.write(image, "PNG", file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        doc.close();
    }

 public static void main(String ages[]) throws IOException {
        String pdf = "C:\\Users\\zskx\\Desktop\\pdf\\123.pdf";
        String dstImgFolder = "C:\\Users\\zskx\\Desktop\\pdf\\";
        pdfImg(pdf,123L,dstImgFolder);
    }

加群:687942640

 类似资料: