maven依赖:
xml
复制代码
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
网络地址添加水印代码:
java
复制代码
public static boolean waterMarkNet(String inputFile, String outputFile, String waterMarkName) {
try
{
URL url = new URL(inputFile);
PdfReader reader = new PdfReader(url);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
//这里的字体设置比较关键,这个设置是支持中文的写法
BaseFont base = BaseFont.createFont();
//BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);// 使用系统字体
int total = reader.getNumberOfPages() + 1;
PdfContentByte under;
Rectangle pageRect = null;
PdfGState gs = new PdfGState();
//透明度
gs.setFillOpacity(0.2f);
gs.setStrokeOpacity(0.1f);
for (int i = 1; i < total; i++)
{
pageRect = stamper.getReader().
getPageSizeWithRotation(i);
under = stamper.getOverContent(i);
under.saveState();
under.setGState(gs);
under.beginText();
under.setFontAndSize(base, 50);
under.setTextMatrix(30, 30);
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 100, 20);
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 300, 20);
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 500, 20);
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 700, 20);
// 添加水印文字
under.endText();
under.setLineWidth(10f);
under.stroke();
}
stamper.close();
return true;
} catch (Exception e)
{
e.printStackTrace();
return false;
本地地址添加水印代码
java
复制代码
/**
* 功能描述: 本地文件添加水印
*
* @param inputFile
* @param outputFile
* @param waterMarkName
* @date 10:52 2024/11/20
*/
public static boolean waterMark(String inputFile, String outputFile, String waterMarkName) {
try
{
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
//这里的字体设置比较关键,这个设置是支持中文的写法
BaseFont base = BaseFont.createFont();
//BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);// 使用系统字体
int total = reader.getNumberOfPages() + 1;
PdfContentByte under;
Rectangle pageRect = null;
PdfGState gs = new PdfGState();
//透明度
gs.setFillOpacity(0.2f);
gs.setStrokeOpacity(0.1f);
for (int i = 1; i < total; i++)
{
pageRect = stamper.getReader().
getPageSizeWithRotation(i);
under = stamper.getOverContent(i);
under.saveState();
under.setGState(gs);
under.beginText();
under.setFontAndSize(base, 50);
under.setTextMatrix(30, 30);
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 100, 20);
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 300, 20);
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 500, 20);
under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, 300, 700, 20);
// 添加水印文字
under.endText();
under.setLineWidth(10f);
under.stroke();
}
stamper.close();
return true;
} catch (Exception e)
{
e.printStackTrace();
return false;
}
}
测试代码
java
复制代码
public static void main(String[] args) {
System.out.println(TestwaterMark.waterMark("https://XXXXXX/%E6%AD%A3%E6%96%87.pdf", "D:\\XXX\\XXX.pdf", "zymz"+DateUtils.dateTimeNow()));
}