【记录】pdf添加水印(文字、图片水印)

先上效果图 由于透明度设置的比较低,可能看不太清右上角的是文字和图片的水印,图片打码了。

在这里插入图片描述

在这里插入图片描述

该实现中有word转pdf的方法,如有需要请移步另一个word转pdf的帖子,小白一个写的比较冗余哈。

pom

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

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-asian</artifactId>
        <version>5.2.0</version>
    </dependency>

业务逻辑

@Override
    public void loadPdf(HttpServletRequest request, HttpServletResponse response, PdfDTO pdfDTO) throws Exception {
   
        String realPath = request.getSession().getServletContext().getRealPath(File.separator);
        long docTime = System.currentTimeMillis();

        //包含doc、docx下载到本地
        if (pdfDTO.getStrUrl().contains(".doc") || pdfDTO.getStrUrl().contains(".docx")) {
   

            logger.info("realPath路径为:" + realPath);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String fileAdd = sdf.format(new Date());
            String inputPath = realPath + File.separator + fileAdd + File.separator;
            logger.info("inputPath路径为:" + inputPath);
            File file = new File(inputPath);
            if (!file.exists() && !file.isDirectory()) {
   
                file.mkdir();
            }

            long newPdfTime = System.currentTimeMillis();
            String fileName = docTime + ".doc";
            String newPdf = newPdfTime + ".pdf";
            String rePath = inputPath + File.separator + File.separator + fileName;
            String newPdfPath = inputPath + File.separator + File.separator + newPdf;
            //将下载的doc、docx保存到本地
            downloadFile(pdfDTO.getStrUrl(), rePath);
            logger.info("保存的doc路径:" + realPath);

            long l = System.currentTimeMillis();
            //读取本地doc、docx 转换成pdf
            PdfUtil.doc2pdf(rePath, newPdfPath);
            logger.info("doc转pdf生成的新pdf路径:" + newPdfPath);
            long l1 = System.currentTimeMillis();
            logger.info("生成pdf用时为:" + (l1 - l));

            //把转完的pdf打上水印
            // 要输出的pdf文件
            Date date = new Date();
            SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date1 = sdff.format(date);
            String waterPdf = System.currentTimeMillis() + ".pdf";
            String waterPdfPath = inputPath + File.separator + File.separator + waterPdf;
            logger.info("打完水印的路径为:" + waterPdfPath);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(waterPdfPath)));
            waterMark(bos, newPdfPath, pdfDTO.getType());

            //将打完水印的pdf输出到前台
            downPdf(request, response, waterPdfPath);

            //删除文件夹下所有内容
            deleteDir(inputPath);

            return;
        }

        long l = System.currentTimeMillis();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1 = simpleDateFormat.format(date);

       /* String outputFilePath = "D:\\b.pdf";
        String returnUrl = request.getScheme() + "://" + request.getServletPath() + ":" + request.getServerPort() + request.getContextPath() + "/unload/";*/

        logger.info("realPath路径为:" + realPath);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String fileAdd = sdf.format(new Date());
        String inputPath = realPath + File.separator + fileAdd + File.separator;
        logger.info("inputPath路径为:" + inputPath);
        File file = new File(inputPath);
        if (!file.exists() && !file.isDirectory()) {
   
            file.mkdir();
        }

        String fileName = l + ".pdf";
        logger.info("fileName文件名:" + fileName);
        String rePath = inputPath + File.separator + File.separator + fileName;
        logger.info("rePath保存路径以及文件名:" + rePath);
        // 给获取到的pdf添加水印 
        waterMark(new URL(pdfDTO.getStrUrl()), rePath, pdfDTO.getType());
        // 下载pdf
        downPdf(request, response, rePath);
        // 删除之前生成的pdf
        deletePdf(inputPath, fileName);
    }

主要实现

//给PDF添加水印
    //inputFile 文件路径+名称
    //outputFile 添加水印后输出文件保存的路径+名称
    //waterMarkName 添加水印的内容
    public static void waterMark(BufferedOutputStream bos, String input, Integer type) {
   
        try {
   
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            String format =
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值