1、pom.xml中添加依赖
com.itextpdf itext7-core7.1.11 pom
2、表格水印的初始化
// 字体
PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Color blue = new DeviceRgb(0,0,0);
// 设置边框
Border border = new SolidBorder(ColorConstants.BLACK,2);
// 初始化表格列数
Table table = new Table(2);
// 设置字体
table.setFontSize(16);
// 设置表格的验收
table.setFontColor(blue);
table.addCell(new Cell(1,2).add(new Paragraph(String.valueOf("文字")).setFont(sysFont)).setBorder(border));
table.addCell(new Cell().add(new Paragraph(String.valueOf("图片")).setFont(sysFont)).setBorder(border));
// 添加图片
Image cellimg = new Image(ImageDataFactory.create("/Users/leixia/Desktop/图片.png")).setWidth(30).setHeight(30);
table.addCell(new Cell().add(cellimg).setBorder(border));
3、读取pdf文件并对文件的每页添加水印
// 原始PDF地址
String inputFilePath = "/Users/leixia/Desktop/原始版.pdf";
// 加水印后的文件地址
String outputFileUrl = "/Users/leixia/Desktop/测试版.pdf";
File inputFile = new File(inputFilePath);
File outputFile = new File(outputFileUrl);
pdfReader = new PdfReader(inputFile);
File parentFile = outputFile.getParentFile();
// 判断父文件夹是否存在
if (!parentFile.exists()) {
parentFile.mkdirs();
}
if(!outputFile.exists()){
outputFile.createNewFile();
}
pdfWriter = new PdfWriter(outputFile);
Pdfdocument pdf = new Pdfdocument(pdfReader, pdfWriter);
doc = new document(pdf);
// 获取pdf的页数
int numberOfPages = pdf.getNumberOfPages();
// 每页都添加table水印
for (int i = 1; i <= numberOfPages; i++) {
doc.add(table.setBold().setFixedPosition(i,100,250,210));
}
完整版代码如下:
public MapaddWaterMark() { Map returnMap = new HashMap (); returnMap.put("status", ""); returnMap.put("msg", ""); document doc = null; PdfWriter pdfWriter = null; PdfReader pdfReader = null; try { // 字体 PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false); Color blue = new DeviceRgb(0,0,0); // 设置边框 Border border = new SolidBorder(ColorConstants.BLACK,2); // 初始化表格列数 Table table = new Table(2); // 设置字体 table.setFontSize(16); // 设置表格的验收 table.setFontColor(blue); table.addCell(new Cell(1,2).add(new Paragraph(String.valueOf("文字")).setFont(sysFont)).setBorder(border)); table.addCell(new Cell().add(new Paragraph(String.valueOf("图片")).setFont(sysFont)).setBorder(border)); // 添加图片 Image cellimg = new Image(ImageDataFactory.create("/Users/leixia/Desktop/图片.png")).setWidth(30).setHeight(30); table.addCell(new Cell().add(cellimg).setBorder(border)); // 原始PDF地址 String inputFilePath = "/Users/leixia/Desktop/原始版.pdf"; // 加水印后的文件地址 String outputFileUrl = "/Users/leixia/Desktop/测试版.pdf"; File inputFile = new File(inputFilePath); File outputFile = new File(outputFileUrl); pdfReader = new PdfReader(inputFile); File parentFile = outputFile.getParentFile(); // 判断父文件夹是否存在 if (!parentFile.exists()) { parentFile.mkdirs(); } if(!outputFile.exists()){ outputFile.createNewFile(); } pdfWriter = new PdfWriter(outputFile); Pdfdocument pdf = new Pdfdocument(pdfReader, pdfWriter); doc = new document(pdf); // 获取pdf的页数 int numberOfPages = pdf.getNumberOfPages(); // 每页都添加table水印 for (int i = 1; i <= numberOfPages; i++) { doc.add(table.setBold().setFixedPosition(i,100,250,210)); } returnMap.put("status", "success"); returnMap.put("msg", "pdf添加水印完成"); } catch (IOException e) { e.printStackTrace(); returnMap.put("status", "error"); returnMap.put("msg", "pdf添加水印出错"); e.printStackTrace(); returnMap.put("status", "error"); returnMap.put("msg", e.getMessage()); }finally{ if(doc != null){ doc.close(); } try { if(pdfWriter != null){ pdfWriter.close(); } if(pdfReader != null){ pdfReader.close(); } } catch (IOException e) { e.printStackTrace(); } } return returnMap; }
完整的项目代码



