有些机密或者有版权的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.getLoggerPdfUtils.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 addTestInputStream 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 PdfDocumentnew PdfReaderpdfIS), new PdfWriterpdfOS)); 56 Document doc = null; 57 try { 58 doc = new DocumentpdfDoc); 59 int n = pdfDoc.getNumberOfPages); 60 Rectangle pagesize = pdfDoc.getPagen).getPageSize); 61 62 forint 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.printlntextLeft+":"+textWidth+":"+textBottom); 67 68 PdfFont font = PdfFontFactory.createFont"STSong-Light", "UniGB-UCS2-H", true); 69 Paragraph p = new Paragraphtext).setFontfont).setFontSize50).setFixedPositioni+1, textLeft, textBottom, textWidth); 70 doc.addp); 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 addTestMarkForPDFInputStream 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 PdfDocumentnew PdfReaderpdfIS), new PdfWriterpdfOS)); 97 Document doc = null; 98 try { 99 doc = new DocumentpdfDoc); 100 int n = pdfDoc.getNumberOfPages); 101 Rectangle pagesize = pdfDoc.getPagen).getPageSize); 102 103 forint 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).setFontfont).setFontSize50); 111 DeviceRgb rgbColor = new DeviceRgb192, 192, 192); 112 // para.setFontColorDeviceRgb.makeLighterrgbColor)); // 定制字体颜色偏亮) 113 para.setFontColorDeviceRgb.makeDarkerrgbColor)); // 定制字体颜色偏暗) 114 para.setRotationAngle120f); // 设置文字倾斜度 115 para.setOpacity0.5f); // 设置文字透明化度 116 para.setFixedPositioni+1, textLeft, textBottom, textWidth); // 设置文字的绝对位置 117 para.setTextAlignmentTextAlignment.JUSTIFIED); // 设置文字排序方式 118 para.addtext); 119 120 doc.addpara); 121 } 122 123 LOGGER.info"添加文字结束!"); 124 } finally { 125 if doc != null) { 126 doc.close); 127 } 128 } 129 130 } 131 132 public static void mainString[] 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).addTestMarkForPDFnew FileInputStreamsourceFile), new FileOutputStreampdfFile), text); 137 } 138 }
效果如图: