1:提供一个aspose.pdf的jar包下载路径:
链接:https://pan.baidu.com/s/118BvmZMwt0k0CjiJgdOEFg
提取码:tflv
2:代码如下:直接使用
// 添加水印
//filepath:文件路径
//data:水印文字内容
public static void addWatermark(String filepath,String data) {
Document pdfDocument = new Document(filepath);
TextStamp textStamp = new TextStamp(data);
textStamp.setBackground(true);
textStamp.setXIndent(100);
textStamp.setYIndent(100);
textStamp.setRotateAngle(50);
textStamp.getTextState().setFont(FontRepository.findFont("Arial"));
textStamp.getTextState().setFontSize(34.0F);
textStamp.getTextState().setFontStyle(FontStyles.Italic);
textStamp.getTextState().setForegroundColor(Color.getLightGray());
TextStamp textStamp1 = new TextStamp(data);
textStamp1.setBackground(true);
textStamp1.setXIndent(300);//设置位置
textStamp1.setYIndent(300);
textStamp1.setRotateAngle(50);//设置旋转角度
textStamp1.getTextState().setFont(FontRepository.findFont("Arial"));
textStamp1.getTextState().setFontSize(34.0F);//设置字体大小
textStamp1.getTextState().setFontStyle(FontStyles.Italic);
textStamp1.getTextState().setForegroundColor(Color.getLightGray());//设置字体颜色
TextStamp textStamp2 = new TextStamp(data);
textStamp2.setBackground(true);
textStamp2.setXIndent(500);
textStamp2.setYIndent(500);
textStamp2.setRotateAngle(50);
textStamp2.getTextState().setFont(FontRepository.findFont("Arial"));
textStamp2.getTextState().setFontSize(34.0F);
textStamp2.getTextState().setFontStyle(FontStyles.Italic);
textStamp2.getTextState().setForegroundColor(Color.getLightGray());
PageCollection pages = pdfDocument.getPages();
int size = pages.size();
for (int i = 1; i <= size; i++) {
pages.get_Item(i).addStamp(textStamp);
pages.get_Item(i).addStamp(textStamp1);
pages.get_Item(i).addStamp(textStamp2);
}
pdfDocument.save(filepath);
}
3:效果如下: