效果如图
demo代码
package test;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
/**
* 图片加水印,设置透明度
*/
public class WatermarkMainTest {
public static void main(String[] args) throws DocumentException, IOException {
List<String> watermarkNames = new ArrayList<>();
watermarkNames.add("xx公司专用");
watermarkNames.add("xxx|业务部");
watermarkNames.add("2022-12-11 22:33");
try {
setWatermark2("C:\\Users\\demo\\Desktop\\out.pdf", "C:\\Users\\demo\\Desktop\\read.pdf",watermarkNames);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param out 输出文件的位置
* @param input 原PDF位置
* @param waterMarkNames 水印内容
* @throws DocumentException
* @throws IOException
*/
public static void setWatermark2(String out, String input, List<String> waterMarkNames)
throws Exception {
PdfReader reader = null;
PdfStamper stamper = null;
try {
File file2 = new File(out);
if (file2.exists()) {
file2.delete();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
reader = new PdfReader(input);
stamper = new PdfStamper(reader, bos);
// 获取总页数 +1, 下面从1开始遍历
int total = reader.getNumberOfPages() + 1;
// 使用classpath下面的字体库
com.itextpdf.text.pdf.BaseFont base = null;
// 区分Linux系统与windows系统
String fontsPath = "C:/Windows/Fonts/simsun.ttc";//中文字体路径
base = com.itextpdf.text.pdf.BaseFont.createFont(fontsPath+",1", com.itextpdf.text.pdf.BaseFont.IDENTITY_H, com.itextpdf.text.pdf.BaseFont.NOT_EMBEDDED);
// 间隔
int interval = -10;
// 获取水印文字的高度和宽度
int textH = 0, textW = 0;
JLabel label = new JLabel();
label.setText(waterMarkNames.get(0));
FontMetrics metrics = label.getFontMetrics(label.getFont());
//textH = metrics.getHeight();
//textW = metrics.stringWidth(label.getText());
// 根据自己需要可以调整水印的间隔
textH = 16;
textW = 182;
System.out.println("textH: " + textH);
System.out.println("textW: " + textW);
// 设置水印透明度
PdfGState gs = new PdfGState();
gs.setFillOpacity(0.3f);
gs.setStrokeOpacity(0.3f);
Rectangle pageSizeWithRotation = null;
PdfContentByte content = null;
for (int i = 1; i < total; i++) {
// 在内容上方加水印
content = stamper.getOverContent(i);
// 在内容下方加水印
// content = stamper.getUnderContent(i);
content.saveState();
content.setGState(gs);
// 设置字体和字体大小
content.beginText();
content.setFontAndSize(base, 9);
// 获取每一页的高度、宽度
pageSizeWithRotation = reader.getPageSizeWithRotation(i);
float pageHeight = pageSizeWithRotation.getHeight();
float pageWidth = pageSizeWithRotation.getWidth();
// 根据纸张大小多次添加, 水印文字成30度角倾斜,根据自己的需要,对应的值调整
for (int height = interval + textH; height < pageHeight; height = height + textH * 6) {
for (int width = interval + textW; width < pageWidth + textW; width = width + textW * 1) {
int len=0;
for (String waterMarkName : waterMarkNames) {
content.showTextAligned(Element.ALIGN_MIDDLE, waterMarkName, width - textW-90, height - textH-len*12, 30);
len++;
}
}
}
content.endText();
}
} catch (Exception e) {
// 日志处理
// log.error("水印异常--->waterName="+waterMarkNames.toString(),e);
throw new Exception("水印异常");
}finally {
// 关流
if(stamper != null){
stamper.close();
}
if(reader != null){
reader.close();
}
}
}
}
?
|