itext操作PDF文件添加水印

功能描述:添加图片和文字水印

 1 /**
 2      * 
 3      * 【功能描述:添加图片和文字水印】 【功能详细描述:功能详细描述】
 4      * @param srcFile 待加水印文件
 5      * @param destFile 加水印后存放地址
 6      * @param text 加水印的文本内容
 7      * @param textWidth 文字横坐标
 8      * @param textHeight 文字纵坐标
 9      * @throws Exception
10      */
11     public void addWaterMark(String srcFile, String destFile, String text,
12             int textWidth, int textHeight) throws Exception
13     {
14         // 待加水印的文件
15         PdfReader reader = new PdfReader(srcFile);
16         // 加完水印的文件
17         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
18                 destFile));
19         int total = reader.getNumberOfPages() + 1;
20         PdfContentByte content;
21         // 设置字体
22         BaseFont font = BaseFont.createFont();
23         // 循环对每页插入水印
24         for (int i = 1; i < total; i++)
25         {
26             // 水印的起始
27             content = stamper.getUnderContent(i);
28             // 开始
29             content.beginText();
30             // 设置颜色 默认为蓝色
31             content.setColorFill(BaseColor.BLUE);
32             // content.setColorFill(Color.GRAY);
33             // 设置字体及字号
34             content.setFontAndSize(font, 38);
35             // 设置起始位置
36             // content.setTextMatrix(400, 880);
37             content.setTextMatrix(textWidth, textHeight);
38             // 开始写入水印
39             content.showTextAligned(Element.ALIGN_LEFT, text, textWidth,
40                     textHeight, 45);
41             content.endText();
42         }
43         stamper.close();
44     }
原文地址:https://www.cnblogs.com/tankqiu/p/4412898.html