|
@@ -0,0 +1,179 @@
|
|
|
+package com.qmth.teachcloud.common.util;
|
|
|
+
|
|
|
+import com.itextpdf.text.Element;
|
|
|
+import com.itextpdf.text.Rectangle;
|
|
|
+import com.itextpdf.text.pdf.*;
|
|
|
+
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+
|
|
|
+
|
|
|
+ * @Description: PDF增加水印工具类
|
|
|
+ */
|
|
|
+public class PDFAddWatermarkUtil {
|
|
|
+
|
|
|
+
|
|
|
+ * 给PDF添加水印
|
|
|
+ *
|
|
|
+ * @param inputFilePath 原文件路径+名称,例如D:\\pdf\\test.pdf
|
|
|
+ * @param outputFilePath 添加水印后输出文件保存的路径+名称
|
|
|
+ * @param waterMarkContent 添加水印的内容
|
|
|
+ */
|
|
|
+ public static void pdfAddWaterMark(String inputFilePath, String outputFilePath, String waterMarkContent) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ int waterMarkHeight = 30;
|
|
|
+ int watermarkWeight = 60;
|
|
|
+
|
|
|
+
|
|
|
+ int waterMarkInterval = 200;
|
|
|
+
|
|
|
+
|
|
|
+ PdfReader pdfReader = new PdfReader(inputFilePath);
|
|
|
+
|
|
|
+
|
|
|
+ PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(outputFilePath));
|
|
|
+
|
|
|
+
|
|
|
+ BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
|
|
|
+
|
|
|
+
|
|
|
+ PdfGState pdfGraPhicState = new PdfGState();
|
|
|
+
|
|
|
+ pdfGraPhicState.setFillOpacity(0.2f);
|
|
|
+
|
|
|
+ pdfGraPhicState.setStrokeOpacity(0.4f);
|
|
|
+
|
|
|
+
|
|
|
+ int pdfPageNum = pdfReader.getNumberOfPages() + 1;
|
|
|
+
|
|
|
+
|
|
|
+ PdfContentByte pdfContent;
|
|
|
+
|
|
|
+
|
|
|
+ Rectangle pageRectangle;
|
|
|
+
|
|
|
+ for (int i = 1; i < pdfPageNum; i++) {
|
|
|
+
|
|
|
+ pageRectangle = pdfReader.getPageSizeWithRotation(i);
|
|
|
+
|
|
|
+ pdfContent = pdfStamper.getOverContent(i);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ pdfContent.saveState();
|
|
|
+
|
|
|
+ pdfContent.setGState(pdfGraPhicState);
|
|
|
+
|
|
|
+
|
|
|
+ pdfContent.beginText();
|
|
|
+
|
|
|
+ pdfContent.setFontAndSize(baseFont, 20);
|
|
|
+
|
|
|
+
|
|
|
+ for (int height = waterMarkHeight; height < pageRectangle.getHeight(); height = height + waterMarkInterval) {
|
|
|
+ for (int width = watermarkWeight; width < pageRectangle.getWidth() + watermarkWeight;
|
|
|
+ width = width + waterMarkInterval) {
|
|
|
+
|
|
|
+ pdfContent.showTextAligned(Element.ALIGN_LEFT, waterMarkContent, (width - watermarkWeight) * 1.1f,
|
|
|
+ height - waterMarkHeight, 35);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pdfContent.endText();
|
|
|
+ }
|
|
|
+ pdfStamper.close();
|
|
|
+ pdfReader.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 给PDF添加水印
|
|
|
+ *
|
|
|
+ * @param inputStream 原文件路径+名称,例如D:\\pdf\\test.pdf
|
|
|
+ * @param outputStream 添加水印后输出文件保存的路径+名称
|
|
|
+ * @param waterMarkContent 添加水印的内容
|
|
|
+ */
|
|
|
+ public static void pdfAddWaterMark(InputStream inputStream, OutputStream outputStream, String waterMarkContent) {
|
|
|
+ try {
|
|
|
+
|
|
|
+ int waterMarkHeight = 30;
|
|
|
+ int watermarkWeight = 60;
|
|
|
+
|
|
|
+
|
|
|
+ int waterMarkInterval = 200;
|
|
|
+
|
|
|
+
|
|
|
+ PdfReader pdfReader = new PdfReader(inputStream);
|
|
|
+
|
|
|
+
|
|
|
+ PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
|
|
|
+
|
|
|
+
|
|
|
+ BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
|
|
|
+
|
|
|
+
|
|
|
+ PdfGState pdfGraPhicState = new PdfGState();
|
|
|
+
|
|
|
+ pdfGraPhicState.setFillOpacity(0.1f);
|
|
|
+
|
|
|
+ pdfGraPhicState.setStrokeOpacity(0.4f);
|
|
|
+
|
|
|
+
|
|
|
+ int pdfPageNum = pdfReader.getNumberOfPages() + 1;
|
|
|
+
|
|
|
+
|
|
|
+ PdfContentByte pdfContent;
|
|
|
+
|
|
|
+
|
|
|
+ Rectangle pageRectangle;
|
|
|
+
|
|
|
+ for (int i = 1; i < pdfPageNum; i++) {
|
|
|
+
|
|
|
+ pageRectangle = pdfReader.getPageSizeWithRotation(i);
|
|
|
+
|
|
|
+ pdfContent = pdfStamper.getOverContent(i);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ pdfContent.saveState();
|
|
|
+
|
|
|
+ pdfContent.setGState(pdfGraPhicState);
|
|
|
+
|
|
|
+
|
|
|
+ pdfContent.beginText();
|
|
|
+
|
|
|
+ pdfContent.setFontAndSize(baseFont, 20);
|
|
|
+
|
|
|
+
|
|
|
+ for (int height = waterMarkHeight; height < pageRectangle.getHeight(); height = height + waterMarkInterval) {
|
|
|
+ for (int width = watermarkWeight; width < pageRectangle.getWidth() + watermarkWeight;
|
|
|
+ width = width + waterMarkInterval) {
|
|
|
+
|
|
|
+ pdfContent.showTextAligned(Element.ALIGN_LEFT, waterMarkContent, (width - watermarkWeight) * 1.1f,
|
|
|
+ height - waterMarkHeight, 35);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ pdfContent.endText();
|
|
|
+ }
|
|
|
+ pdfStamper.close();
|
|
|
+ pdfReader.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ pdfAddWaterMark("/Users/xiaofei/qmth/test/pdf/测试.pdf", "/Users/xiaofei/qmth/test/pdf/测试-watermark.pdf", "zhangsan(张三)");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|