java pdf转jpg 多张图片合成一张

@Test
    public void demo7 () throws Exception {
        String pathpdf = "C:\\Users\\lenovo\\Desktop\\文档.pdf";
        String pathjpg = "C:\\Users\\lenovo\\Desktop\\文档.jpg";
        File file = new File(pathpdf);
        if(file.exists()) {
            PDDocument dest = null;
            //得到输入流
            InputStream stream = new FileInputStream(file);
            // 加载解析PDF文件
            dest = PDDocument.load(stream);
            PDFRenderer pdfRenderer = new PDFRenderer(dest);
            PDPageTree pages = dest.getPages();
            //获得PDF总页数,方便下边生成图片
            int pageCount = pages.getCount();
            BufferedImage[] imageArrery = new BufferedImage[pageCount];
            //图片上下排布,所有的图片加起来就是,总图片高度
            int nHeight = 0;
            //图片宽度
            int mWidth = 0;
            int[][] ImageArrays = new int[pageCount][]; 
            for (int i = 0; i < pageCount; i++) {
                BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
                imageArrery[i] = bim;
                mWidth = bim.getWidth();
                nHeight += bim.getHeight();
                int width = imageArrery[i].getWidth();  
                 int height = imageArrery[i].getHeight();  
                 // 从图片中读取RGB 像素
                ImageArrays[i] = new int[width * height];
                ImageArrays[i] = imageArrery[i].getRGB(0, 0, width, height,  ImageArrays[i], 0, width);  
            }
            int mHeight = 0;
            BufferedImage bgmImage = new BufferedImage(mWidth, nHeight, BufferedImage.TYPE_INT_RGB);
            for (int i = 0; i < ImageArrays.length; i++) {  
                bgmImage.setRGB(0, mHeight, mWidth, imageArrery[i].getHeight(),ImageArrays[i], 0, mWidth);  
                mHeight += imageArrery[i].getHeight();  
            }  
            ImageIO.write(bgmImage, "jpg", new File(pathjpg));
            stream.close();
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值