PDF转图片,合并后加水印

 public static void main(String[] args) {
        System.out.println("开始");
        System.out.println(DateTime.now());
        DZZZ_TPExecutor dzzz_tpExecutor = new DZZZ_TPExecutor();
        String path = DZZZ_TPExecutor.class.getResource("/").getPath().replaceFirst("/","");
        List<String> ftpdfPathpng = new ArrayList<>();
        File file = new File(path+"dywqd.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, 120); // Windows native DPI
                // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图
                ImageIO.write(image, "png", new File(path+"dywqd"+"_"+(i+1)+"."+"png"));
                ftpdfPathpng.add(path+"dywqd"+"_"+(i+1)+"."+"png");
            }
            String mergrPdfPath = path+"hb.png";
            dzzz_tpExecutor.mergePDFFilespng(ftpdfPathpng,mergrPdfPath);
            for (int i = 0; i < ftpdfPathpng.size(); i++) {
                File file1 = new File(ftpdfPathpng.get(i));
                if (file1.exists()) {
                    file1.delete();
                }
            }
            OutputStream os = null;
            try {
                Image srcImg = ImageIO.read(new File(mergrPdfPath));

                BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
                        srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);

                // 1、得到画笔对象
                Graphics2D g = buffImg.createGraphics();

                // 2、设置对线段的锯齿状边缘处理
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                        RenderingHints.VALUE_INTERPOLATION_BILINEAR);

                g.drawImage(
                        srcImg.getScaledInstance(srcImg.getWidth(null),
                                srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
                        null);

                // 4、水印图片的路径 水印图片一般为gif或者png的,这样可设置透明度
                ImageIcon imgIcon = new ImageIcon(path+"qz.png");
                /*  //可以设置水印的长宽
            imgIcon.setImage(imgIcon.getImage().getScaledInstance(600, 200, 1));*/

                // 5、得到Image对象
                Image img = imgIcon.getImage();

                float alpha = 1f; // 透明度
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));

                // 6、水印图片的位置
                g.drawImage(img, 300, 100, null);
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
                // 7、释放资源
                g.dispose();

                // 8、生成图片
                os = new FileOutputStream(path+"hbqz.png");
                ImageIO.write(buffImg, "png", os);

                System.out.println("图片完成添加水印图片");

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (null != os)
                        os.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("结束");
            System.out.println(DateTime.now());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
 /**
     * 合并jpg
     * @param fileList 要合并的jpg文件路径
     * @param mergrPdfPath 合成的新路径
     * @return
     */
    public boolean mergePDFFilespng(List<String> fileList, String mergrPdfPath) {
        boolean result = false;
        // 生成新图片
        BufferedImage destImage = null;
        // 计算新图片的长和高
        int allw = 0;int allh = 0;int allwMax = 0;int allhMax = 0;
        // 获取总长、总宽、最长、最宽
        try {
            debugUtil = new DebugUtil(Level.INFO);
            debugUtil.begin();
            List<BufferedImage> listImg = new ArrayList<>();
            for (int i = 0; i < fileList.size(); i++) {
                File fileImg = new File(fileList.get(i));
                BufferedImage img = ImageIO.read(fileImg);
                allw += img.getWidth();
                allh += img.getHeight();
                if (img.getWidth() > allwMax) {
                    allwMax = img.getWidth();
                }
                if (img.getHeight() > allhMax) {
                    allhMax = img.getHeight();
                }
                listImg.add(img);
            }
            // 创建新图片
            if (false) {
                destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
            } else {
                destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
            }
            // 合并所有子图片到新图片
            int wx = 0, wy = 0;
            for (BufferedImage img : listImg) {
                int w1 = img.getWidth();
                int h1 = img.getHeight();
                // 从图片中读取RGB
                int[] ImageArrayOne = new int[w1 * h1];
                ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
                if (false) { // 水平方向合并
                    destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
                } else { // 垂直方向合并
                    destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
                }
                wx += w1;
                wy += h1;
            }
            File outFile = new File(mergrPdfPath);
            ImageIO.write(destImage, "jpg", outFile);// 写图片
            debugUtil.end();
            debugUtil.writeLog("合并jpg",null);
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            destImage = null;
            System.gc();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值