需要參考我的上一篇博客,定位PDF中的關鍵字,找出需要打印水印的坐標位置。
先說測試結果(PDF原件也是上一篇中的圖片所示):
新生成的帶有水印的PDF文件如下所示:
junit測試代碼及輸出:
maven配置文件
<!-- 引入pdf --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency>
打印水印java文件PDFDocHelper.java
package com.alphajuns.util; import com.itextpdf.text.BaseColor; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Map; /** * @Author AlphaJunS * @Date 19:25 2020/3/7 * @Description 文檔幫助類 */ public class PDFDocHelper { private static BaseFont base = null; // 獲取基礎文字 public static BaseFont getBaseFont() throws DocumentException, IOException { if (base == null) { try { base = BaseFont.createFont("/u01/config/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); } catch (Throwable th) { base = BaseFont.createFont("C:\\WINDOWS\\Fonts\\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); } } return base; } //psw文件簽名 public static String signPsw(String oldPswFilePath, List<Map<String, ?>> reviews) throws IOException, DocumentException { int pos = oldPswFilePath.lastIndexOf('.'); // 獲取文件后綴 String suffix = oldPswFilePath.substring(pos + 1); // 判斷是否為pdf文件 if (!"pdf".equals(suffix.toLowerCase())) { throw new RuntimeException("Not supported PSW file"); } return signSinglePsw(oldPswFilePath, reviews); } //單個psw文件簽名 private static String signSinglePsw(String oldPswFilePath,List<Map<String, ?>> reviews) throws IOException, DocumentException { String newPswPath = oldPswFilePath; int pos = oldPswFilePath.lastIndexOf('.'); // 獲取文件后綴名 String suffix = oldPswFilePath.substring(pos + 1); // 生成新的文件路徑 newPswPath = oldPswFilePath.substring(0, pos) + ".PSW." + suffix; System.out.println("單個psw文件簽名生成的新路徑:" + newPswPath); PdfReader reader = new PdfReader(oldPswFilePath); FileOutputStream fout = new FileOutputStream(newPswPath); PdfStamper stp = new PdfStamper(reader, fout); // 總頁數 System.out.println("PDF總頁數:" + reader.getNumberOfPages()); for (int i = 0; i < reader.getNumberOfPages(); ) { // 需要從第一頁開始,i放在循環中會報空指針異常 i++; PdfContentByte content = stp.getOverContent(i); content.beginText(); // 設置字體及字號 content.setFontAndSize(getBaseFont(), 10); Map<String, Object> review = (Map<String, Object>) reviews.get(reviews.size() - 1); addDeptReview(content, review); content.endText(); } stp.close(); // 將輸出流關閉 fout.close(); reader.close(); // 文件讀寫結束 System.out.println("PSW文件讀寫完畢"); return newPswPath; } /** * @Author AlphaJunS * @Date 18:48 2020/3/7 * @Description 添加水印 * @param content * @param review * @return void */ private static void addDeptReview(PdfContentByte content, Map<String, Object> review) { if (Integer.parseInt(String.valueOf(review.get("type"))) == 1) { content.setColorFill(BaseColor.BLUE); } else { content.setColorFill(BaseColor.RED); } // 設置水印位置和內容 String result = (String) review.get("result"); System.out.println("水印內容:" + result); System.out.println("打印位置坐標:" + pswX[0] + "," + pswY[0]); content.showTextAligned(Element.ALIGN_LEFT, result, pswX[0], pswY[0], 0); } // 打印水印坐標 private static float[] pswY = {128}; private static float[] pswX = {337}; }