/** * 给pdf文件添加水印 * @param InPdfFile 要加水印的原pdf文件路径 * @param outPdfFile 加了水印后要输出的路径 * @param markImagePath 水印图片路径 * @param pageSize 原pdf文件的总页数 * @throws Exception */ public static void addPdfMark(String InPdfFile, String outPdfFile, String markImagePath, int pageSize) throws Exception { PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes()); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile)); Image img = Image.getInstance(markImagePath);// 插入水印img.setAbsolutePosition(150, 100); for(int i = 1; i <= pageSize; i++) { PdfContentByte under = stamp.getUnderContent(i); under.addImage(img); } stamp.close();// 关闭 File tempfile = new File(InPdfFile); if(tempfile.exists()) { tempfile.delete(); } } [java] view plaincopyprint?/** * 给pdf文件添加水印 * @param InPdfFile 要加水印的原pdf文件路径 * @param outPdfFile 加了水印后要输出的路径 * @param markImagePath 水印图片路径 * @param pageSize 原pdf文件的总页数 * @throws Exception */ public static void addPdfMark(String InPdfFile, String outPdfFile, String markImagePath, int pageSize) throws Exception { PdfReader reader = new PdfReader(InPdfFile, "PDF".getBytes()); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outPdfFile)); Image img = Image.getInstance(markImagePath);// 插入水印img.setAbsolutePosition(150, 100); for(int i = 1; i <= pageSize; i++) { PdfContentByte under = stamp.getUnderContent(i); under.addImage(img); } stamp.close();// 关闭 File tempfile = new File(InPdfFile); if(tempfile.exists()) { tempfile.delete(); }
转自:/huiwenjie168/article/details/7616983