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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM