iText: 对pdf文件添加水印

有些机密或者有版权的pdf文件需要添加”内部使用,禁止外传”的文字提示。要实现这个功能,需要使用到iText插件具体工具类代码如下:

  1 import com.google.zxing.WriterException;
  2 import com.itextpdf.kernel.colors.DeviceRgb;
  3 import com.itextpdf.kernel.font.PdfFont;
  4 import com.itextpdf.kernel.font.PdfFontFactory;
  5 import com.itextpdf.kernel.geom.Rectangle;
  6 import com.itextpdf.kernel.pdf.PdfDocument;
  7 import com.itextpdf.kernel.pdf.PdfReader;
  8 import com.itextpdf.kernel.pdf.PdfWriter;
  9 import com.itextpdf.layout.Document;
 10 import com.itextpdf.layout.element.Paragraph;
 11 import com.itextpdf.layout.property.TextAlignment;
 12 import org.slf4j.Logger;
 13 import org.slf4j.LoggerFactory;
 14 
 15 import java.io.*;
 16 
 17 /**
 18  * pdf工具
 19  * @author limingcheng
 20  * @Date 2019/11/25
 21  */
 22 public class PdfUtils {
 23 
 24    private final static Logger LOGGER = LoggerFactory.getLogger(PdfUtils.class);
 25 
 26    /** 单例 */
 27    private static PdfUtils instance = new PdfUtils();
 28    
 29    /**
 30     * 获取实例(此处禁止自行实例化对象)
 31     * @return
 32     */
 33    public static PdfUtils getInstance() {
 34       return instance;
 35    }
 36    /**
 37     * 实例化对象
 38     */
 39    private PdfUtils() {}
 40 
 41    /**
 42     * 给pdf的每一页添加注释文字
 43     * @param pdfIS 需要增加文字前的pdf输入流
 44     * @param pdfOS 增加文字后的pdf输出流
 45     * @param text 需要增加的说明文字
 46     * @throws WriterException
 47     * @throws IOException
 48     */
 49    public void addTest(InputStream pdfIS, OutputStream pdfOS, String text ) throws WriterException, IOException {
 50 
 51       LOGGER.info("添加文字开始!");
 52       int textLeft = 0;
 53       int textWidth = 0;
 54       int textBottom = 0;
 55       PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfIS), new PdfWriter(pdfOS));
 56       Document doc = null;
 57       try {
 58          doc = new Document(pdfDoc);
 59          int n = pdfDoc.getNumberOfPages();
 60          Rectangle pagesize = pdfDoc.getPage(n).getPageSize();
 61          
 62          for(int i=0; i<n; i++) {
 63             textLeft = (int) (pagesize.getWidth()/3);
 64             textWidth = (int) (pagesize.getWidth()/3);
 65             textBottom = (int) (pagesize.getHeight()/3);
 66             System.out.println(textLeft+":"+textWidth+":"+textBottom);
 67             
 68             PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", true);
 69             Paragraph p = new Paragraph(text).setFont(font).setFontSize(50).setFixedPosition(i+1, textLeft, textBottom, textWidth);
 70             doc.add(p);
 71          }
 72          
 73          LOGGER.info("添加文字结束!");
 74       } finally {
 75          if (doc != null) {
 76             doc.close();
 77          }
 78       }
 79    }
 80    
 81    /**
 82     * 给pdf的每一页添加注释文字
 83     * @param pdfIS 需要增加文字前的pdf输入流
 84     * @param pdfOS 增加文字后的pdf输出流
 85     * @param text 需要增加的说明文字
 86     * @throws WriterException
 87     * @throws IOException
 88     */
 89    public void addTestMarkForPDF(InputStream pdfIS, OutputStream pdfOS, String text ) throws WriterException, IOException {
 90 
 91       LOGGER.info("添加文字开始!");
 92       
 93       int textLeft = 0;
 94       int textWidth = 0;
 95       int textBottom = 0;
 96       PdfDocument pdfDoc = new PdfDocument(new PdfReader(pdfIS), new PdfWriter(pdfOS));
 97       Document doc = null;
 98       try {
 99          doc = new Document(pdfDoc);
100          int n = pdfDoc.getNumberOfPages();
101          Rectangle pagesize = pdfDoc.getPage(n).getPageSize();
102          
103          for(int i=0; i<n; i++) {
104             
105             textLeft = (int) (pagesize.getWidth()/5)*4;
106             textWidth = 50;
107             textBottom = (int) (pagesize.getHeight()/10);
108             PdfFont font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", true);
109             
110             Paragraph para = new Paragraph().setFont(font).setFontSize(50);
111             DeviceRgb rgbColor = new DeviceRgb(192, 192, 192);
112 //          para.setFontColor(DeviceRgb.makeLighter(rgbColor));    // 定制字体颜色(偏亮)
113             para.setFontColor(DeviceRgb.makeDarker(rgbColor)); // 定制字体颜色(偏暗)
114             para.setRotationAngle(120f);   // 设置文字倾斜度
115             para.setOpacity(0.5f); // 设置文字透明化度
116             para.setFixedPosition(i+1, textLeft, textBottom, textWidth);   // 设置文字的绝对位置
117             para.setTextAlignment(TextAlignment.JUSTIFIED);    // 设置文字排序方式
118             para.add(text);
119             
120             doc.add(para);
121          }
122          
123          LOGGER.info("添加文字结束!");
124       } finally {
125          if (doc != null) {
126             doc.close();
127          }
128       }
129       
130    }
131    
132    public static void main(String[] args) throws FileNotFoundException, WriterException, IOException {
133       String text = "内部使用,仅供查看";
134       File sourceFile = new File("E:\图片\测试文档\1111.pdf");
135       File pdfFile = new File("E:\图片\测试文档\test009.pdf");
136       PdfUtils.getInstance().addTestMarkForPDF(new FileInputStream(sourceFile), new FileOutputStream(pdfFile), text);
137    }
138 }

效果如图:

 

原文地址:https://www.cnblogs.com/bestlmc/p/12012885.html