package com.***.dtx.dc.***.demo.util;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.UUID;
public class Watermark {
public static boolean addwaterMark(String originaBase64lFile, String outputFile, String waterMarkName,
float opacity, int fontsize, int angle, int heightdensity, int widthdensity, boolean cover) {
if (!cover) {
File outfile = new File(outputFile);
System.out.println("文件输出路径:"+outputFile);
if (outfile.exists()) {
return true;
}
}
if(StringUtils.isEmpty(originaBase64lFile)) {
System.out.println("原始文件不存在");
return false;
}
PdfReader reader = null;
PdfStamper stamper = null;
try {
int interval = -5;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(originaBase64lFile));
reader = new PdfReader(byteArrayInputStream);
stamper = new PdfStamper(reader, new ByteArrayOutputStream());
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
Rectangle pageRect = null;
PdfGState gs = new PdfGState();
gs.setFillOpacity(opacity);
gs.setStrokeOpacity(0.2f);
int total = reader.getNumberOfPages() + 1;
System.out.println("Pdf页数:" + reader.getNumberOfPages());
JLabel label = new JLabel();
FontMetrics metrics;
int textH = 0;
int textW =