PDF文档设置水印工具类
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import org.apache.log4j.Logger; import com.itextpdf.text.Element; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfGState; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; public class WatermarkUtil { private static Logger logger = Logger.getLogger(WatermarkUtil.class); /** * PDF文件设置文字水印 * * @param inputFilePath 输入文件路径 * @param outputFilePath 输出文件路径 * @param StrWatermark 文字水印内容 * @param pageList 水印添加的页码(传空则全部页码添加水印) * @return 返回是否添加成功 */ public static Boolean setStrWatermark2PDFByPage(String inputFilePath, String outputFilePath, String StrWatermark, ArrayList<String> pageList) { try { logger.info("开始添加文字水印,inputFilePath:" + inputFilePath + "; outputFilePath:" + outputFilePath + "; StrWatermark:" + StrWatermark + "; "); PdfReader reader = new PdfReader(inputFilePath); File outputFile = new File(outputFilePath); // 假如目标路径不存在,则新建该路径 if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } // 假如目标文件不存在,则新建该文件 if (!outputFile.exists()) { outputFile.createNewFile(); } BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputFile)); PdfStamper stamper = new PdfStamper(reader, bos); int total = reader.getNumberOfPages() + 1; PdfContentByte content; BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); // BaseFont base = // BaseFont.createFont("/data/tmis/uploads/file/font/simsun.ttc,1", // BaseFont.IDENTITY_H, BaseFont.EMBEDDED); PdfGState gs = new PdfGState(); if (pageList == null || pageList.isEmpty()) {// 水印添加的页码(传空则全部页码添加水印) for (int i = 1; i < total; i++) { // content = stamper.getOverContent(i);// 在内容上方加水印 content = stamper.getUnderContent(i);// 在内容下方加水印 gs.setFillOpacity(0.2f); // content.setGState(gs); content.beginText(); content.setRGBColorFill(222, 222, 222); content.setFontAndSize(base, 30); content.setTextMatrix(100, 250); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 100, 100, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 100, 300, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 100, 500, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 100, 700, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 300, 100, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 300, 300, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 300, 500, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 300, 700, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 500, 100, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 500, 300, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 500, 500, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 500, 700, 45); content.setRGBColorFill(0, 0, 0); content.setFontAndSize(base, 8); content.endText(); } stamper.close(); reader.close(); } else { for (int i = 1; i < total; i++) { if (pageList.contains(String.valueOf(i))) { // content = stamper.getOverContent(i);// 在内容上方加水印 content = stamper.getUnderContent(i);// 在内容下方加水印 gs.setFillOpacity(0.2f); // content.setGState(gs); content.beginText(); content.setRGBColorFill(222, 222, 222); content.setFontAndSize(base, 30); content.setTextMatrix(100, 250); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 100, 100, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 100, 300, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 100, 500, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 100, 700, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 300, 100, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 300, 300, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 300, 500, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 300, 700, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 500, 100, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 500, 300, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 500, 500, 45); content.showTextAligned(Element.ALIGN_CENTER, StrWatermark, 500, 700, 45); content.setRGBColorFill(0, 0, 0); content.setFontAndSize(base, 8); content.endText(); } } stamper.close(); reader.close(); logger.info("添加文字水印完毕"); } return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error("添加文字水印异常" + e.toString()); return false; } } /** * PDF文件设置图片水印 * * @param inputFilePath 输入文件路径 * @param outputFilePath 输出文件路径 * @param ImgWatermarkPath 图片水印路径 * @param pageList 水印添加的页码(传空则全部页码添加水印) * @return 返回是否添加成功 */ public static Boolean setImgWatermark2PDFByPage(String inputFilePath, String outputFilePath, String ImgWatermarkPath, ArrayList<String> pageList) { try { logger.info("开始添加图片水印,inputFilePath:" + inputFilePath + "; outputFilePath:" + outputFilePath + "; ImgWatermarkPath:" + ImgWatermarkPath + "; "); // 待加水印的文件 PdfReader reader = new PdfReader(inputFilePath); // 加完水印的文件 File outputFile = new File(outputFilePath); // 假如目标路径不存在,则新建该路径 if (!outputFile.getParentFile().exists()) { outputFile.getParentFile().mkdirs(); } // 假如目标文件不存在,则新建该文件 if (!outputFile.exists()) { outputFile.createNewFile(); } PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile)); // 设置字体 BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); // PDF总页数 int total = reader.getNumberOfPages() + 1; // 循环对每页插入水印 PdfContentByte content; if (pageList == null || pageList.isEmpty()) {// 水印添加的页码(传空则全部页码添加水印) for (int i = 1; i < total; i++) { PdfGState gs1 = new PdfGState(); gs1.setFillOpacity(0.1f); content = stamper.getUnderContent(i);// 在内容下方加水印 content.setGState(gs1); // 图片水印 if (ImgWatermarkPath != null) { Image image = null; if (ImgWatermarkPath != null) { image = Image.getInstance(ImgWatermarkPath); image.setRotationDegrees(45);//旋转 角度 // 设置图片的显示大小 image.scaleToFit(100, 125); } image.setAbsolutePosition(50, 50);content.addImage(image); image.setAbsolutePosition(50, 250);content.addImage(image); image.setAbsolutePosition(50, 450);content.addImage(image); image.setAbsolutePosition(50, 650);content.addImage(image); image.setAbsolutePosition(250, 50);content.addImage(image); image.setAbsolutePosition(250, 250);content.addImage(image); image.setAbsolutePosition(250, 450);content.addImage(image); image.setAbsolutePosition(250, 650);content.addImage(image); image.setAbsolutePosition(450, 50);content.addImage(image); image.setAbsolutePosition(450, 250);content.addImage(image); image.setAbsolutePosition(450, 450);content.addImage(image); image.setAbsolutePosition(450, 650);content.addImage(image); } } } else { for (int i = 1; i < total; i++) { if (pageList.contains(String.valueOf(i))) { PdfGState gs1 = new PdfGState(); gs1.setFillOpacity(0.1f); content = stamper.getUnderContent(i);// 在内容下方加水印 content.setGState(gs1); // 图片水印 if (ImgWatermarkPath != null) { Image image = null; if (ImgWatermarkPath != null) { image = Image.getInstance(ImgWatermarkPath); image.setRotationDegrees(45);//旋转 角度 // 设置图片的显示大小 image.scaleToFit(100, 125); } image.setAbsolutePosition(50, 50);content.addImage(image); image.setAbsolutePosition(50, 250);content.addImage(image); image.setAbsolutePosition(50, 450);content.addImage(image); image.setAbsolutePosition(50, 650);content.addImage(image); image.setAbsolutePosition(250, 50);content.addImage(image); image.setAbsolutePosition(250, 250);content.addImage(image); image.setAbsolutePosition(250, 450);content.addImage(image); image.setAbsolutePosition(250, 650);content.addImage(image); image.setAbsolutePosition(450, 50);content.addImage(image); image.setAbsolutePosition(450, 250);content.addImage(image); image.setAbsolutePosition(450, 450);content.addImage(image); image.setAbsolutePosition(450, 650);content.addImage(image); } } } } stamper.close(); reader.close(); logger.info("添加图片水印完毕"); return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); logger.error("添加图片水印异常" + e.toString()); return false; } } }
版权声明:本文为RivenLw原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。