java给pdf文件插入图片、添加水印并盖章

java给pdf添加水印

	//添加水印
   public static void insertWatermark(PdfPageBase page) {
        Dimension2D dimension2D = new Dimension();
        PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN, 14), true);
        dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 3, page.getCanvas().getClientSize().getHeight() / 3);
        PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
        brush.getGraphics().setTransparency(0.4F);
        brush.getGraphics().save();
        brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 3, (float) brush.getSize().getHeight() / 3);
        brush.getGraphics().rotateTransform(-45);
        brush.getGraphics().drawString("陕西省市场监督管理局", font1, PdfBrushes.getViolet(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
        brush.getGraphics().restore();
        brush.getGraphics().setTransparency(1);
        Rectangle2D loRect = new Rectangle2D.Float();
        loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
        page.getCanvas().drawRectangle(brush, loRect);
    }
	//插入图片
   public static void addAdImagesToPdf4Ypggsp(String pdfPath, String adPath, String outputPath, boolean isShiPin) {
        // 创建PdfDocument对象
        PdfDocument doc = new PdfDocument();
        // 加载PDF文档
        doc.loadFromFile(pdfPath);
        PdfPageBase page = null;
        PdfImage image = PdfImage.fromFile(adPath);
        if (isShiPin) {
            page = doc.getPages().insert(doc.getPages().getCount());
            float width = image.getWidth();
            float height = image.getHeight();
            if (width > 1024 || height > 768) {
                page.getCanvas().drawImage(image, 1, 1, image.getWidth() * 0.3f, image.getHeight() * 0.3f);
            }else{
                page.getCanvas().drawImage(image, 1, 1, image.getWidth() * 0.8f, image.getHeight() * 0.8f);
            }
        } else {
            page = doc.getPages().get(doc.getPages().getCount() - 1);
            page.getCanvas().drawImage(image, 100, 200, 300, 230);
        }
        insertWatermark(doc.getPages().get(0));
        insertWatermark(doc.getPages().get(1));
        // 保存文档
        File f = new File(outputPath);
        if (f.exists()) {
            f.delete();
        }
        doc.saveToFile(outputPath);
        doc.close();
    }
	//盖章
	public static boolean addImagesToPdfPage4Ggsp(String pdfPath, String fzImg, boolean isYp, String yzImg, String ldspDate, String pzwh, String yxqz, String outputPath, String fontPath) {
        return addImagesToPdfPage4Ggsp(pdfPath, fzImg, isYp, yzImg, ldspDate, pzwh, yxqz, outputPath, fontPath,0,0);
    }

    public static boolean addImagesToPdfPage4Ggsp(String pdfPath, String fzImg, boolean isYp, String yzImg, String ldspDate, String pzwh, String yxqz, String outputPath, String fontPath,int x_plu,int y_plu) {
        // 创建PdfDocument对象
        PdfDocument doc = new PdfDocument();
        // 加载PDF文档
        try {
            doc.loadFromFile(pdfPath);
        } catch (Exception e) {
            System.out.println("文件不存在异常。。");
            e.printStackTrace();
            doc.close();
            return false;
        }
        PdfPageBase secondPage = doc.getPages().get(doc.getPages().getCount() - 1);
        PdfPageBase page = doc.getPages().get(0);
        PdfImage image = PdfImage.fromFile(fzImg);
        PdfImage image2 = PdfImage.fromFile(yzImg);
        // 放置图片的位置
        if (isYp) {
            page.getCanvas().drawImage(image, 140+x_plu, 580+y_plu, image.getWidth() * 0.5f, image.getHeight() * 0.5f);
        } else {
            page.getCanvas().drawImage(image, 140+x_plu, 580+y_plu, image.getWidth() * 0.9f, image.getHeight() * 0.9f);
        }
        page.getCanvas().drawImage(image2, 343+x_plu, 550+y_plu, 120, 120);
        secondPage.getCanvas().drawImage(image2, 240+x_plu, 300+y_plu, 120, 120);
        // 保存文档
        File f = new File(outputPath);
        if (f.exists()) {
            f.delete();
        }
        doc.saveToFile(outputPath);
        doc.close();
        return true;
    }
    public static void main(String[] args) {
        //pdf添加图片,添加图片完成后,添加水印
        PdfImageHelper.addAdImagesToPdf4Ypggsp("f:\\市场局广告测试盖章\\2019092510033100383.pdf", "f:\\市场局广告测试盖章\\1.jpg", "f:\\市场局广告测试盖章\\2019092510033100383X.pdf", true);

        //盖章
        PdfImageHelper.addImagesToPdfPage4Ggsp("f:\\市场局广告测试盖章\\2019092510033100383.pdf",
                "f:\\市场局广告测试盖章\\f2.png", true, "f:\\市场局广告测试盖章\\yz.png", "",
                "", "", "f:\\市场局广告测试盖章\\2019092510033100383X.pdf", "f:\\市场局广告测试盖章\\Deng.ttf",0,0);
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值