|
@@ -540,6 +540,220 @@ public class PdfUtil {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * pdf添加水印
|
|
|
+ *
|
|
|
+ * @param inputFile 需要添加水印的文件
|
|
|
+ * @param waterMarkName 需要添加的水印文字
|
|
|
+ * @param opacity 水印字体透明度(0-1)
|
|
|
+ * @param fontsize 水印字体大小
|
|
|
+ * @param rotation 水印倾斜角度(0-360)
|
|
|
+ */
|
|
|
+ public static File addWaterMarkNew(File inputFile, String[] waterMarkName, float opacity, int fontsize, int rotation) {
|
|
|
+ if (!inputFile.exists()) {
|
|
|
+ throw ExceptionResultEnum.ERROR.exception("试卷文件不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (waterMarkName.length == 0) {
|
|
|
+ return inputFile;
|
|
|
+ }
|
|
|
+
|
|
|
+ PdfReader reader = null;
|
|
|
+ PdfStamper stamper = null;
|
|
|
+ File outputFile = null;
|
|
|
+ try {
|
|
|
+ outputFile = new File(inputFile.getPath());
|
|
|
+ if (!outputFile.exists()) {
|
|
|
+ outputFile.getParentFile().mkdirs();
|
|
|
+ outputFile.createNewFile();
|
|
|
+ }
|
|
|
+ reader = new PdfReader(new FileInputStream(inputFile));
|
|
|
+ stamper = new PdfStamper(reader, new FileOutputStream(inputFile));
|
|
|
+ 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;
|
|
|
+ for (int i = 1; i < total; i++) {
|
|
|
+ // 只打奇数页水印
|
|
|
+ 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], 20, pageRect.getHeight() - 20 - (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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * pdf生成水印(全屏)
|
|
|
+ *
|
|
|
+ * @param inputFile 插入前的文件路径
|
|
|
+ * @param waterMarkName 水印文案
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public static void addWaterMarkFullScreen(File inputFile, String[] waterMarkName) throws Exception {
|
|
|
+ PdfReader reader = new PdfReader(new FileInputStream(inputFile));
|
|
|
+ PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(inputFile));
|
|
|
+ PdfGState gs = new PdfGState();
|
|
|
+
|
|
|
+ // 设置字体
|
|
|
+ BaseFont font = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
|
|
|
+
|
|
|
+ // 设置透明度
|
|
|
+ gs.setFillOpacity(0.2f);
|
|
|
+
|
|
|
+ int total = reader.getNumberOfPages() + 1;
|
|
|
+ PdfContentByte content;
|
|
|
+ for (int i = 1; i < total; i++) {
|
|
|
+ content = stamper.getOverContent(i);
|
|
|
+ content.beginText();
|
|
|
+ content.setGState(gs);
|
|
|
+ // 水印颜色
|
|
|
+ content.setColorFill(BaseColor.BLUE);
|
|
|
+ // 水印字体样式和大小
|
|
|
+ content.setFontAndSize(font, 20);
|
|
|
+
|
|
|
+ // 获取页面尺寸
|
|
|
+ Rectangle pageSize = reader.getPageSize(i);
|
|
|
+ float pageWidth = pageSize.getWidth();
|
|
|
+ float pageHeight = pageSize.getHeight();
|
|
|
+
|
|
|
+ // 计算水印位置,覆盖整个页面
|
|
|
+ float x = 0;
|
|
|
+ float y = 0;
|
|
|
+ float rotation = 30; // 水印旋转角度
|
|
|
+
|
|
|
+ // 循环插入水印
|
|
|
+ while (x < pageWidth && y < pageHeight) {
|
|
|
+ for (int j = 0; j < waterMarkName.length; j++) {
|
|
|
+ content.showTextAligned(Element.ALIGN_CENTER, waterMarkName[j], x, y, rotation);
|
|
|
+ }
|
|
|
+ // 水平平移
|
|
|
+ x += 200;
|
|
|
+ // 垂直平移
|
|
|
+ if (x >= pageWidth) {
|
|
|
+ x = 0;
|
|
|
+ y += 200;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ content.endText();
|
|
|
+ }
|
|
|
+ stamper.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void waterMark(File inputFile, String waterMarkName, String password) {
|
|
|
+ try {
|
|
|
+ PdfReader reader = new PdfReader(new FileInputStream(inputFile));
|
|
|
+ PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
|
|
|
+ inputFile));
|
|
|
+
|
|
|
+ byte[] ownerPassword = password.getBytes();
|
|
|
+ stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_ASSEMBLY, false);
|
|
|
+ stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_COPY, false);
|
|
|
+ stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_DEGRADED_PRINTING, false);
|
|
|
+ stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_FILL_IN, false);
|
|
|
+ stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_MODIFY_ANNOTATIONS, false);
|
|
|
+ stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_MODIFY_CONTENTS, false);
|
|
|
+ stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_PRINTING, false);
|
|
|
+ stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_SCREENREADERS, false);
|
|
|
+ stamper.setEncryption(null, ownerPassword, PdfWriter.DO_NOT_ENCRYPT_METADATA, true);
|
|
|
+ stamper.setViewerPreferences(PdfWriter.HideToolbar | PdfWriter.HideMenubar);
|
|
|
+
|
|
|
+ BaseFont font = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
|
|
|
+
|
|
|
+ Rectangle pageRect = null;
|
|
|
+ PdfGState gs = new PdfGState();
|
|
|
+ gs.setFillOpacity(0.3f);
|
|
|
+ gs.setStrokeOpacity(0.4f);
|
|
|
+ int total = reader.getNumberOfPages() + 1;
|
|
|
+
|
|
|
+ JLabel label = new JLabel();
|
|
|
+ int interval = -5;
|
|
|
+ FontMetrics metrics;
|
|
|
+ int textH = 0;
|
|
|
+ int textW = 0;
|
|
|
+ label.setText(waterMarkName);
|
|
|
+ metrics = label.getFontMetrics(label.getFont());
|
|
|
+ textH = metrics.getHeight();//字符串的高, 只和字体有关
|
|
|
+ textW = metrics.stringWidth(label.getText());//字符串的宽
|
|
|
+
|
|
|
+ PdfContentByte under;
|
|
|
+ for (int i = 1; i < total; i++) {
|
|
|
+ pageRect = reader.getPageSizeWithRotation(i);
|
|
|
+ // 计算水印X,Y坐标
|
|
|
+ float x = pageRect.getWidth() / 2;
|
|
|
+ float y = pageRect.getHeight() / 2;
|
|
|
+ under = stamper.getOverContent(i);
|
|
|
+ under.saveState();
|
|
|
+ under.setGState(gs);
|
|
|
+ under.beginText();
|
|
|
+ under.setFontAndSize(font, 15);
|
|
|
+ // 水印文字成45度角倾斜
|
|
|
+ for (int height = interval + textH; height < pageRect.getHeight();
|
|
|
+ height = height + textH * 8) {
|
|
|
+ for (int width = interval + textW; width < pageRect.getWidth() + textW;
|
|
|
+ width = width + textW) {
|
|
|
+ under.showTextAligned(Element.ALIGN_LEFT
|
|
|
+ , waterMarkName, width - textW,
|
|
|
+ height - textH, 45);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 添加水印文字
|
|
|
+ under.endText();
|
|
|
+ }
|
|
|
+ stamper.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* html转pdf文件
|
|
|
*
|