pdf转图片截长图

@Test
    public void testPdf2JPG() {

        PDDocument pdDocument = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            File file = new File("/Users/hanfeng/Downloads/11.pdf");
            if (file.isFile()) {
                pdDocument = PDDocument.load(file);
                PDFRenderer renderer = new PDFRenderer(pdDocument);
                /* dpi越大转换后越清晰,相对转换速度越慢 */
                int pages = pdDocument.getNumberOfPages();


                BufferedImage destImage = null;
                int startHeight = 0;
                for (int i = 0; i < pages; i++) {
                    logger.info("开始截取并上传第" + i + "张图片");
                    BufferedImage image = renderer.renderImageWithDPI(i, 200);

                    int width = image.getWidth();
                    int height = image.getHeight();

                    logger.info("width:{}, height:{}", width, height);
                    if (Objects.isNull(destImage)) {
                        destImage = new BufferedImage(width, height * pages, BufferedImage.TYPE_INT_RGB);
                    }

                    int[] imageArrange = new int[width * height];
                    imageArrange = image.getRGB(0, 0, width, height, imageArrange,
                            0, width);


                    destImage.setRGB(0, startHeight, width,  height, imageArrange, 0, width);
                    startHeight += height;
                }


                byteArrayOutputStream = new ByteArrayOutputStream();
                ImageIO.write(destImage, JPG, byteArrayOutputStream);

                    File dstFile = new File("/Users/hanfeng/Downloads/" + IdGen.uuid() + "." + JPG);//输出磁盘目录
                    ImageIO.write(destImage, JPG, dstFile);//输出磁盘


//
//                ResultDO<String> result = ZaUploadManager.uploadFile(UploadConstant.Systype.CLS, UploadConstant.FileNamespace.SECONDHAND_CAR,
//                        byteArrayOutputStream.toByteArray(), IdGen.uuid() + "." + JPG, "bss");
//                if (result.isSuccess() == false) {
//                    throw new BizException("文件上传失败[" + result.getMsg() + "]");
//                }
//

//                logger.info("上传图片返回的消息:" + result.getMsg() + "---上传图片返回的地址:" + result.getModule());


            }

        } catch (Exception e) {
            logger.error("pdf文件转图片出错:", e);
        } finally {
            try {
                if (!Objects.isNull(byteArrayOutputStream)) {
                    byteArrayOutputStream.close();
                }
                if (!Objects.isNull(pdDocument)) {
                    pdDocument.close();
                }
            } catch (IOException e) {
                logger.error("error:{}", e);
            }

        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值