需要参考我的上一篇博客,定位PDF中的关键字,找出需要打印水印的坐标位置。
先说测试结果(PDF原件也是上一篇中的图片所示):
新生成的带有水印的PDF文件如下所示:
junit测试代码及输出:
maven配置文件
<!--引入pdf --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency>
打印水印java文件PDFDocHelper.java
packagecom.alphajuns.util; importcom.itextpdf.text.BaseColor; importcom.itextpdf.text.DocumentException; importcom.itextpdf.text.Element; importcom.itextpdf.text.pdf.BaseFont; importcom.itextpdf.text.pdf.PdfContentByte; importcom.itextpdf.text.pdf.PdfReader; importcom.itextpdf.text.pdf.PdfStamper; importjava.io.FileOutputStream; importjava.io.IOException; importjava.util.List; importjava.util.Map; /*** @Author AlphaJunS * @Date 19:25 2020/3/7 * @Description 文档帮助类 */ public classPDFDocHelper { private static BaseFont base = null; //获取基础文字 public static BaseFont getBaseFont() throwsDocumentException, 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); } } returnbase; } //psw文件签名 public static String signPsw(String oldPswFilePath, List<Map<String, ?>> reviews) throwsIOException, DocumentException { int pos = oldPswFilePath.lastIndexOf('.'); //获取文件后缀 String suffix = oldPswFilePath.substring(pos + 1); //判断是否为pdf文件 if (!"pdf".equals(suffix.toLowerCase())) { throw new RuntimeException("Not supported PSW file"); } returnsignSinglePsw(oldPswFilePath, reviews); } //单个psw文件签名 private static String signSinglePsw(String oldPswFilePath,List<Map<String, ?>> reviews) throwsIOException, 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 = newPdfReader(oldPswFilePath); FileOutputStream fout = newFileOutputStream(newPswPath); PdfStamper stp = newPdfStamper(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文件读写完毕"); returnnewPswPath; } /*** @Author AlphaJunS * @Date 18:48 2020/3/7 * @Description 添加水印 * @paramcontent * @paramreview * @returnvoid */ 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}; }