|
@@ -1,7 +1,9 @@
|
|
|
package com.qmth.distributed.print.business.util;
|
|
|
|
|
|
+import com.itextpdf.awt.AsianFontMapper;
|
|
|
import com.itextpdf.text.Document;
|
|
|
-import com.itextpdf.text.PageSize;
|
|
|
+import com.itextpdf.text.DocumentException;
|
|
|
+import com.itextpdf.text.Element;
|
|
|
import com.itextpdf.text.Rectangle;
|
|
|
import com.itextpdf.text.pdf.*;
|
|
|
import com.qmth.boot.tools.models.ByteArray;
|
|
@@ -19,8 +21,11 @@ import org.apache.commons.io.IOUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
+import javax.swing.*;
|
|
|
+import java.awt.*;
|
|
|
import java.io.*;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
import java.util.*;
|
|
|
|
|
|
/**
|
|
@@ -281,4 +286,109 @@ public class PdfUtil {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * pdf添加水印
|
|
|
+ *
|
|
|
+ * @param inputFile 需要添加水印的文件
|
|
|
+ * @param waterMarkName 需要添加的水印文字
|
|
|
+ * @param opacity 水印字体透明度
|
|
|
+ * @param fontsize 水印字体大小
|
|
|
+ * @param rotation 水印倾斜角度(0-360)
|
|
|
+ */
|
|
|
+ public static File addWaterMark(File inputFile, String[] waterMarkName, float opacity, int fontsize, int rotation) {
|
|
|
+ if (!inputFile.exists()) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("试卷文件不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (waterMarkName.length == 0) {
|
|
|
+ return inputFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
|
|
|
+ LocalDateTime nowTime = LocalDateTime.now();
|
|
|
+ StringJoiner stringJoiner = new StringJoiner("");
|
|
|
+ stringJoiner.add(dictionaryConfig.fssLocalPdfDomain().getConfig()).add(File.separator)
|
|
|
+ .add(UploadFileEnum.PAPER.getTitle()).add(File.separator)
|
|
|
+ .add(String.valueOf(nowTime.getYear())).add(File.separator)
|
|
|
+ .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
|
|
|
+ .add(String.format("%02d", nowTime.getDayOfMonth()))
|
|
|
+ .add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.PDF_PREFIX);
|
|
|
+ String outputPath = stringJoiner.toString();
|
|
|
+ PdfReader reader = null;
|
|
|
+ PdfStamper stamper = null;
|
|
|
+ File outputFile = null;
|
|
|
+ try {
|
|
|
+ outputFile = new File(outputPath);
|
|
|
+ if (!outputFile.exists()) {
|
|
|
+ outputFile.getParentFile().mkdirs();
|
|
|
+ outputFile.createNewFile();
|
|
|
+ }
|
|
|
+ reader = new PdfReader(new FileInputStream(inputFile));
|
|
|
+ stamper = new PdfStamper(reader, new FileOutputStream(outputPath));
|
|
|
+ BaseFont base = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
|
|
|
+ PdfGState gs = new PdfGState();
|
|
|
+ //这里是透明度设置
|
|
|
+ gs.setFillOpacity(opacity);
|
|
|
+ //这里是条纹不透明度
|
|
|
+ gs.setStrokeOpacity(0.2f);
|
|
|
+ int total = reader.getNumberOfPages() + 1;
|
|
|
+ int textH = 0;
|
|
|
+ int textW = 0;
|
|
|
+ for (String s : waterMarkName) {
|
|
|
+ JLabel label = new JLabel();
|
|
|
+ FontMetrics metrics;
|
|
|
+
|
|
|
+ label.setText(s);
|
|
|
+ metrics = label.getFontMetrics(label.getFont());
|
|
|
+ //字符串的高, 只和字体有关
|
|
|
+ textH = metrics.getHeight();
|
|
|
+ //字符串的宽
|
|
|
+ textW = metrics.stringWidth(label.getText());
|
|
|
+ }
|
|
|
+
|
|
|
+ Rectangle pageRect;
|
|
|
+ PdfContentByte under;
|
|
|
+ //循环PDF,每页添加水印
|
|
|
+ for (int i = 1; i < total; i++) {
|
|
|
+ // 只打奇数页水印
|
|
|
+ if (i % 2 == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ pageRect = reader.getPageSizeWithRotation(i);
|
|
|
+ under = stamper.getOverContent(i); //在内容上方添加水印
|
|
|
+ under.saveState();
|
|
|
+ under.setGState(gs);
|
|
|
+ under.beginText();
|
|
|
+ under.setFontAndSize(base, fontsize); //这里是水印字体大小
|
|
|
+ for (int j = 0; j < waterMarkName.length; j++) {
|
|
|
+ under.showTextAligned(Element.ALIGN_LEFT, waterMarkName[j], 10, pageRect.getHeight() - 30 - (j * textH), rotation);
|
|
|
+ }
|
|
|
+ //添加水印文字
|
|
|
+ under.endText();
|
|
|
+ }
|
|
|
+ return outputFile;
|
|
|
+ } catch (IOException | DocumentException e) {
|
|
|
+ System.out.println("添加水印失败!错误信息为: " + e);
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ //关闭流
|
|
|
+ if (stamper != null) {
|
|
|
+ try {
|
|
|
+ stamper.close();
|
|
|
+ } catch (DocumentException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (reader != null) {
|
|
|
+ reader.close();
|
|
|
+ }
|
|
|
+ if (outputFile.exists()) {
|
|
|
+ outputFile.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|