java 给pdf添加水印

引用

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

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

<dependency>
    <groupId>org.dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>2.1.3</version>
</dependency>

工具类

	/**
     * 斜角排列、全屏多个重复的花式文字水印
     *
     * @param bytes             需要加水印的PDF读取输入流
     * @param output            输出生成PDF的输出流
     * @param waterMarkString   水印字符
     * @param xAmout            x轴步长
     * @param yAmout            y轴步长
     * @param opacity           水印透明度
     * @param rotation          水印文字旋转角度,一般为45度角
     * @param waterMarkFontSize 水印字体大小
     * @param color             水印字体颜色
     */
    public static void stringWaterMark(byte[] bytes, FileOutputStream output, String waterMarkString, int xAmout, int yAmout, float opacity, float rotation, int waterMarkFontSize, BaseColor color) {
        try {

            PdfReader reader = new PdfReader(bytes);
            PdfReader.unethicalreading = true;
            PdfStamper stamper = new PdfStamper(reader, output);

            // 添加中文字体
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

            int total = reader.getNumberOfPages() + 1;

            PdfContentByte over;
            // 给每一页加水印
            for (int i = 1; i < total; i++) {
                Rectangle pageRect = stamper.getReader().getPageSizeWithRotation(i);
                // 计算水印每个单位步长X,Y
//                float x = pageRect.getWidth() / xAmout;
//                float y = pageRect.getHeight() / yAmout;
				// 间隔长度
                xAmout += (waterMarkString.length() * 10);
                //计算水印个数
                float x = pageRect.getWidth() / xAmout;
                float y = pageRect.getHeight() / yAmout;

                over = stamper.getOverContent(i);
                PdfGState gs = new PdfGState();
                // 设置透明度为
                gs.setFillOpacity(opacity);

                over.setGState(gs);
                over.saveState();

                over.beginText();
                over.setColorFill(color);
                over.setFontAndSize(baseFont, waterMarkFontSize);

                for (int n = 0; n < x + 1; n++) {
                    for (int m = 0; m < y + 1; m++) {
                        BigDecimal xAmoutBig = new BigDecimal(xAmout);
                        BigDecimal yAmoutBig = new BigDecimal(yAmout);
                        BigDecimal mBig = new BigDecimal(m);
                        BigDecimal nBig = new BigDecimal(n);
                        Float x_afferent = xAmoutBig.multiply(nBig).floatValue();
                        Float y_afferent = yAmoutBig.multiply(mBig).floatValue();
                        over.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x_afferent, y_afferent, rotation);
                    }
                }

                over.endText();
            }
            stamper.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	/**
     * @param response
     * @param fileName        文件存放名称
     * @param uploadPath         文件地址
     * @return void
     * @Description //TODO 下载文件
     **/
    public static void downFile(HttpServletResponse response, String fileName, String uploadPath) {
        //下載文件
        String filePath = uploadPath + File.separator + fileName;
        //判断文件是否存在
        File file = new File(filePath);
        if (!file.exists()) {
            throw new RuntimeException("文件不存在");
        }
        FileInputStream fis = null;
        OutputStream myout = null;
        BufferedInputStream buff = null;
        try {
            if (file.exists()) {
                response.setHeader("Content-Disposition",
                        "attachment;filename=" + new String((fileName).getBytes("UTF-8"), "ISO8859-1"));
                response.setContentLength((int) file.length());
                //暴露Header
                response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
                // 定义输出类型
                response.setContentType("application/octet-stream");
                fis = new FileInputStream(file);
                buff = new BufferedInputStream(fis);
                // 相当于我们的缓存
                byte[] b = new byte[1024];
                // 该值用于计算当前实际下载了多少字节
                long k = 0;
                // 从response对象中得到输出流,准备下载
                myout = response.getOutputStream();
                // 开始循环下载
                while (k < file.length()) {
                    int j = buff.read(b, 0, 1024);
                    k += j;
                    myout.write(b, 0, j);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (myout != null) {
                try {
                    myout.flush();
                    myout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (buff != null) {
                try {
                    buff.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * @param filePath 文件地址
     * @return boolean
     * @Description //TODO 删除单个文件
     * @Param
     **/
    public static boolean deleteFile(String filePath) {
        File file = new File(filePath);
        // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                LOGGER.info("删除单个文件" + filePath + "成功!");
                return true;
            } else {
                LOGGER.error("删除单个文件" + filePath + "失败!");
                return false;
            }
        } else {
            LOGGER.error("删除单个文件失败:" + filePath + "不存在!");
            return false;
        }
    }

调用类

	public void queryView(HttpServletResponse response, String viewNo, String pageNo) {
		// 输入地址
		String url = "http://....";
		// 输出文件夹
		String outViewUrl = "D:/";
        String fileName = "测试.pdf";
        String outUrl = outViewUrl + File.separator + fileName;

        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File file = new File(outUrl);
            Boolean fileCreate = true;
            if (!file.exists()) {
                Files.createDirectories(Path.of(file.getParent()));
                // 在服务器上创建file
                fileCreate = file.createNewFile();
            }
            // 创建成功
            if (fileCreate) {
                fileOutputStream = new FileOutputStream(file);
                URL urlConn = new URL(url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) urlConn.openConnection();
                httpURLConnection.setRequestMethod("GET");
                // 响应
                Integer code = httpURLConnection.getResponseCode();
                if (code == 200) {
                    inputStream = httpURLConnection.getInputStream();
                    // 输入流转成bytes
                    byte[] bytes = inputStream.readAllBytes();
                    HttpRequest.stringWaterMark(bytes, fileOutputStream, waterMarkString, 100, 100, 0.2f, 45, 20, new BaseColor(165, 165, 165));
                    HttpRequest.downFile(response, fileName, outViewUrl);
                }
                // 处理完删除临时文件
                HttpRequest.deleteFile(outUrl);
            }
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
            if (inputStream != null){
                try{
                    inputStream.close();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
	}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 iText 库来实现在 PDF添加水印的功能,以下是一个示例代码: ```java import com.itextpdf.kernel.color.Color; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.font.PdfUIFontFactory; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfPage; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.canvas.PdfCanvas; import com.itextpdf.layout.Canvas; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.property.TextAlignment; import com.itextpdf.layout.property.VerticalAlignment; import java.io.File; import java.io.IOException; public class AddWatermark { public static void main(String[] args) throws IOException { // 加载字体 PdfFont font = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", true); // 创建 PDF 文档对象 PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf")); // 循环遍历每一页 for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) { // 获取当前页 PdfPage page = pdfDoc.getPage(i); // 创建 PDF 画布 PdfCanvas pdfCanvas = new PdfCanvas(page); // 创建画布对象 Canvas canvas = new Canvas(pdfCanvas, page.getPageSize()); // 设置文本对齐方式 canvas.setProperty(Property.TEXT_ALIGNMENT, TextAlignment.CENTER); canvas.setProperty(Property.VERTICAL_ALIGNMENT, VerticalAlignment.MIDDLE); // 创建水印文本 Paragraph p = new Paragraph("Watermark Text").setFont(font).setFontSize(50); // 设置水印文本颜色 p.setFontColor(Color.LIGHT_GRAY); // 绘制水印文本 canvas.add(p); } // 关闭 PDF 文档对象 pdfDoc.close(); } } ``` 在上述代码中,我们使用 iText 库中的 `PdfCanvas` 以及 `Canvas` 对象来绘制水印文本。首先,我们需要加载字体,然后循环遍历每一页,创建画布对象并设置文本对齐方式,接着创建水印文本并设置其颜色和大小,最后将水印文本绘制到画布上。最终,我们可以在输出的 PDF 文件中看到添加水印的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值