|
@@ -1,13 +1,9 @@
|
|
|
package com.jpsoft.picc.modules.common.utils;
|
|
|
|
|
|
-import com.itextpdf.text.Document;
|
|
|
-import com.itextpdf.text.Element;
|
|
|
-import com.itextpdf.text.Font;
|
|
|
-import com.itextpdf.text.Phrase;
|
|
|
-import com.itextpdf.text.pdf.ColumnText;
|
|
|
-import com.itextpdf.text.pdf.GrayColor;
|
|
|
-import com.itextpdf.text.pdf.PdfPageEventHelper;
|
|
|
-import com.itextpdf.text.pdf.PdfWriter;
|
|
|
+import com.itextpdf.text.*;
|
|
|
+import com.itextpdf.text.pdf.*;
|
|
|
+
|
|
|
+import java.io.FileOutputStream;
|
|
|
|
|
|
/**
|
|
|
* @author 墨鱼_mo
|
|
@@ -17,6 +13,8 @@ import com.itextpdf.text.pdf.PdfWriter;
|
|
|
public class Watermark extends PdfPageEventHelper {
|
|
|
|
|
|
|
|
|
+
|
|
|
+
|
|
|
Font FONT = new Font(Font.FontFamily.HELVETICA, 30, Font.BOLD, new GrayColor(0.95f));
|
|
|
private String waterCont;//水印内容
|
|
|
|
|
@@ -40,4 +38,59 @@ public class Watermark extends PdfPageEventHelper {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void imageWatermark(String inputPath, String outputPath,String images) throws Exception{
|
|
|
+
|
|
|
+ try{
|
|
|
+ PdfReader reader = new PdfReader(inputPath);
|
|
|
+ PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPath));
|
|
|
+ PdfGState gs1 = new PdfGState();
|
|
|
+ gs1.setFillOpacity(0.1f);
|
|
|
+
|
|
|
+ Image image = Image.getInstance(images);
|
|
|
+ int n = reader.getNumberOfPages();
|
|
|
+ PdfContentByte under;
|
|
|
+ for (int i = 1; i <= n; i++) {
|
|
|
+ PdfContentByte pdfContentByte = stamper.getOverContent(i);
|
|
|
+ // 获得PDF最顶层
|
|
|
+ under = stamper.getOverContent(i);
|
|
|
+ pdfContentByte.setGState(gs1);
|
|
|
+
|
|
|
+ for (int y = 0; y < 8; y++) {
|
|
|
+ for (int x = 0; x < 6; x++) {
|
|
|
+ // 水印文字成45度角倾斜
|
|
|
+ image.setRotation(30);// 旋转 弧度
|
|
|
+ // 设置旋转角度
|
|
|
+ image.setRotationDegrees(-45);// 旋转 角度
|
|
|
+ // 设置等比缩放
|
|
|
+ under.setColorFill(BaseColor.GRAY);
|
|
|
+ image.scaleToFit(80,120);
|
|
|
+ image.setRotation(45);
|
|
|
+ image.setAbsolutePosition(70 + 140 * x, 125 * y);
|
|
|
+ pdfContentByte.addImage(image);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ stamper.close();
|
|
|
+ reader.close();
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ try {
|
|
|
+ Watermark.imageWatermark("C:\\Users\\Administrator\\Desktop\\投保单.pdf","C:\\Users\\Administrator\\Desktop\\test.pdf","C:\\Users\\Administrator\\Desktop\\picc\\logo2.png");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ System.out.println("done");
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|