PDF转图片、图片转PDF

PDF转图片

依赖

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.12</version>
</dependency>

转换代码

    /**
      * 功能描述: PDF转图片
      * @param pdfPath PDF文件路径
      * @param imageDir 图片存放路径
      * @return void
      * @throws
      */
    public static void PdfToImages(String pdfPath, String imageDir) {
        PDDocument document = null;
        try {
            document = PDDocument.load(new File(pdfPath));
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            for (int page = 0; page < document.getNumberOfPages(); ++page) {
                BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 144); // 设置DPI以控制图片质量
                Thumbnails.of(bim)
                        .outputQuality(1) // 设置输出图片质量(0-1之间的值,1表示原始质量)
                        .size(bim.getWidth(), bim.getHeight()) // 设置输出图片尺寸
                        .toFile(imageDir+File.separator+"image" + page + ".jpg"); // 输出图片的文件路径和名称
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            if(document !=null){
                try {
                    document.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

图片转PDF

依赖

            <dependency>
                <groupId>com.itextpdf</groupId>
                <artifactId>itextpdf</artifactId>
                <version>5.5.13.3</version>
            </dependency>

代码

    public static File Pdf(List<String> imageUrllist, String outputPdfFilePath) {
    // 283是 PDF 100mm * 100mm 大小
        Document doc = new Document(new RectangleReadOnly(283F, 283F), 0, 0, 0, 0); //new一个pdf文档
        try {
            PdfWriter.getInstance(doc, new FileOutputStream(outputPdfFilePath)); //pdf写入
            doc.open();//打开文档
            for (int i = 0; i < imageUrllist.size(); i++) {  //循环图片List,将图片加入到pdf中
                doc.newPage();  //在pdf创建一页
                Image png1 = Image.getInstance(imageUrllist.get(i)); //通过文件路径获取image
                float heigth = png1.getHeight();
                float width = png1.getWidth();
                png1.setAlignment(Image.MIDDLE);
                int percent = getPercent(heigth, width);
                png1.scalePercent(percent);// 表示是原来图像的比例;
                doc.add(png1);
            }
            doc.close();
        } catch (Exception e) {
           throw new RuntimeException(e);
        }
        File outputPdfFile = new File(outputPdfFilePath);  //输出流
        if (!outputPdfFile .exists()) {
            return null;
        }
        return outputPdfFile ; //反回文件
    }

    /**
      * 功能描述: 图片缩放比例
      * @param h 高
      * @param w 宽
      * @return int
      * @throws
      */
    public static int getPercent(float h, float w) {
        int p = 0;
        float p2 = 0.0f;
        // 283 是pdf的像素 根据实际情况调整  1mm 约等于 2.83像素
        if (h > w) {
            p2 = 283 / h * 100;
        } else {
            p2 = 283 / w * 100;
        }
        p = Math.round(p2);
        return p;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值