Java 后端PDF添加水印

package com.common.util.file;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * @author zzj
 * @date 2022-03-20
 */
@Slf4j
@Component
@RefreshScope
public class PdfUtils {
    /**
     * 添加paf水印
     * @param inputStream pdf流对象
     * @param tarFilePath 添加水印输出的文件地址
     * @param waterMarkContent 水印内容
     * @throws Exception
     */
    public static void addWaterMark(InputStream inputStream, String tarFilePath, String waterMarkContent)throws Exception {
        addWaterMark(inputStream, tarFilePath, waterMarkContent, 2, 250,45);
    }

    /**
     * 添加paf水印
     * @param inputStream pdf流对象
     * @param tarFilePath 添加水印输出的文件地址
     * @param waterMarkContent 水印内容
     * @param type 水印添加方式 1:单个  2:平铺
     * @param space 当水印为跑平铺时每个水印的间距
     * @param angle 水印倾斜角度
     * @throws Exception
     */
    public static void addWaterMark(InputStream inputStream, String tarFilePath, String waterMarkContent, int type, int space,int angle)throws Exception {
        PdfReader reader = new PdfReader(inputStream);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(tarFilePath));
        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体

        int total = reader.getNumberOfPages() + 1;
        Rectangle pageRect = null;
        PdfContentByte under;
        for (int i = 1; i < total; i++) {
            pageRect = stamper.getReader().getPageSizeWithRotation(i);

            // 获得PDF最顶层
            under = stamper.getOverContent(i);
            under.saveState();
            // set Transparency
            PdfGState gs = new PdfGState();
            // 设置透明度为0.8
            gs.setFillOpacity(0.8f);
            under.setGState(gs);
            under.beginText();
            under.setFontAndSize(base, 20);
            under.setColorFill(BaseColor.GRAY);

            // 单个水印
            if (type == 1) {
                // 计算水印X,Y坐标
                float x = pageRect.getWidth() / 2;
                float y = pageRect.getHeight() / 2;
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkContent, x, y, angle);
            } else {
                // 水印初始高度为水印间距,并且设置水印的高度不能超过页面高度
                for (int height = 10; height < pageRect.getHeight(); height = height + space) {
                    // 水印初始宽度水印间距, 并且设置的水印宽度不能超过页面宽度
                    for (int width = 10; width < pageRect.getWidth(); width = width + space) {
                        under.showTextAligned(Element.ALIGN_LEFT, waterMarkContent, width, height, angle);
                    }
                }
            }

            // 添加水印文字
            under.endText();
            under.setLineWidth(1f);
            under.stroke();
            under.restoreState();
        }
        stamper.close();
        reader.close();
        log.info("PDF水印添加完成!");
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值