Преглед на файлове

feat(合同):合同制作

liutao преди 8 месеца
родител
ревизия
81fb77aaf0
променени са 1 файла, в които са добавени 98 реда и са изтрити 1 реда
  1. 98 1
      src/main/java/com/sunxung/factoring/component/util/PdfUtil.java

+ 98 - 1
src/main/java/com/sunxung/factoring/component/util/PdfUtil.java

@@ -4,9 +4,10 @@ import com.documents4j.api.DocumentType;
 import com.documents4j.api.IConverter;
 import com.documents4j.job.LocalConverter;
 import com.itextpdf.text.Document;
+import com.itextpdf.text.Element;
 import com.itextpdf.text.Image;
 import com.itextpdf.text.Rectangle;
-import com.itextpdf.text.pdf.PdfWriter;
+import com.itextpdf.text.pdf.*;
 
 import java.io.*;
 public class PdfUtil {
@@ -79,4 +80,100 @@ public class PdfUtil {
             }
         }
     }
+
+    /**
+     * pdf文件添加图片水印
+     * @param srcPath 输入的文件路径
+     * @param destPath 输出的文件路径
+     * @param imagePath 水印图片的路径
+     * @throws Exception
+     */
+    public static void addPDFImageWaterMark(String srcPath, String destPath, String imagePath)
+            throws Exception {
+
+        PdfReader reader = new PdfReader(srcPath);
+        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPath));
+
+        //加载图片
+        Image image = Image.getInstance(imagePath);
+
+        PdfGState gs = new PdfGState();
+        //gs.setFillOpacity(0.2f);//图片水印透明度
+        //gs.setStrokeOpacity(0.4f);//设置笔触字体不透明度
+        PdfContentByte content = null;
+
+        int total = reader.getNumberOfPages();//pdf文件页数
+        for (int i=0; i<total; i++) {
+            float x = reader.getPageSize(i+1).getWidth();//页宽度
+            float y = reader.getPageSize(i+1).getHeight();//页高度
+            content = stamper.getOverContent(i+1);
+            content.setGState(gs);
+            content.beginText();//开始写入
+
+            //每页7行,一行3个
+            for (int j=0; j<3; j++) {
+                for (int k=0; k<7; k++) {
+                    //setAbsolutePosition 方法的参数(输出水印X轴位置,Y轴位置)
+                    image.setAbsolutePosition(x/3*j-30, y/7*k-20);
+                    content.addImage(image);
+                }
+            }
+            content.endText();//结束写入
+        }
+        //关闭流
+        stamper.close();
+        reader.close();
+    }
+
+    public static void main(String[] args) throws Exception {
+        addPDFWaterMark("D:\\保理系统4.0数据库设计-20230627.pdf","D:\\123.pdf","12390819873409810239540723640178623");
+    }
+
+    /**
+     * pdf文件添加文字水印
+     * @param srcPath 输入的文件路径
+     * @param destPath 输出的文件路径
+     * @param word 水印文字
+     * @throws Exception
+     */
+    public static void addPDFWaterMark(String srcPath, String destPath, String word)
+            throws Exception {
+
+        PdfReader reader = new PdfReader(srcPath);
+        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPath));
+
+        //使用系统字体
+        String prefixFont = null;
+        String os = System.getProperties().getProperty("os.name");
+        if (os.startsWith("win") || os.startsWith("Win")) {
+            prefixFont = "C:\\Windows\\Fonts\\SIMSUN.TTC,1";
+        } else {
+            prefixFont = "/usr/share/fonts/chinese/TrueType/uming.ttf";
+        }
+
+        //创建字体,第一个参数是字体路径
+        BaseFont base = BaseFont.createFont(prefixFont, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
+        //BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
+
+        PdfGState gs = new PdfGState();
+        gs.setFillOpacity(0.2f);//图片水印透明度
+        //gs.setStrokeOpacity(0.4f);//设置笔触字体不透明度
+        PdfContentByte content = null;
+
+        int total = reader.getNumberOfPages();//pdf文件页数
+        for (int i=0; i<total; i++) {
+            float x = reader.getPageSize(i+1).getWidth();//页宽度
+            float y = reader.getPageSize(i+1).getHeight();//页高度
+            content = stamper.getOverContent(i+1);
+            content.setGState(gs);
+            content.beginText();//开始写入
+            content.setFontAndSize(base, 20);//字体大小
+            //showTextAligned 方法的参数(文字对齐方式,位置内容,输出水印X轴位置,Y轴位置,旋转角度)
+            content.showTextAligned(Element.ALIGN_CENTER, word, x/3+90, y/15, 0);
+            content.endText();//结束写入
+        }
+        //关闭流
+        stamper.close();
+        reader.close();
+    }
 }