fix: 更新带水印下载代码逻辑
This commit is contained in:
@@ -4,7 +4,12 @@ import com.itextpdf.text.BaseColor;
|
||||
import com.itextpdf.text.Element;
|
||||
import com.itextpdf.text.Rectangle;
|
||||
import com.itextpdf.text.pdf.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
@@ -15,6 +20,8 @@ import java.lang.reflect.Field;
|
||||
*/
|
||||
public class WaterMarkUtil {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(WaterMarkUtil.class);
|
||||
|
||||
/**
|
||||
* @param inputFile 你的PDF文件地址
|
||||
* @param outputFile 添加水印后生成PDF存放的地址
|
||||
@@ -78,4 +85,83 @@ public class WaterMarkUtil {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean waterMarkNew(String inputFile, String outputFile, String waterMarkName) {
|
||||
try {
|
||||
logger.debug("水印追加文件:"+inputFile+" "+outputFile);
|
||||
FileInputStream is = new FileInputStream(inputFile);
|
||||
PdfReader reader = new PdfReader(is);
|
||||
Field f = PdfReader.class.getDeclaredField("encrypted");
|
||||
f.setAccessible(true);
|
||||
f.set(reader, Boolean.FALSE);
|
||||
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
|
||||
|
||||
// 获取总页数 +1, 下面从1开始遍历
|
||||
int total = reader.getNumberOfPages() + 1;
|
||||
//这里的字体设置比较关键,这个设置是支持中文的写法
|
||||
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
|
||||
|
||||
// 间隔
|
||||
int interval = 5;
|
||||
// 获取水印文字的高度和宽度
|
||||
int textH = 0, textW = 0;
|
||||
JLabel label = new JLabel();
|
||||
label.setText(waterMarkName);
|
||||
FontMetrics metrics = label.getFontMetrics(label.getFont());
|
||||
textH = metrics.getHeight();
|
||||
textW = metrics.stringWidth(label.getText());
|
||||
|
||||
// 设置水印透明度
|
||||
PdfGState gs = new PdfGState();
|
||||
gs.setFillOpacity(0.2f);
|
||||
gs.setStrokeOpacity(0.2f);
|
||||
|
||||
Rectangle pageSizeWithRotation = null;
|
||||
PdfContentByte content = null;
|
||||
for (int i = 1; i < total; i++) {
|
||||
// 在内容上方加水印
|
||||
content = stamper.getOverContent(i);
|
||||
// 在内容下方加水印
|
||||
// content = stamper.getUnderContent(i);
|
||||
content.saveState();
|
||||
content.setGState(gs);
|
||||
|
||||
// 设置字体和字体大小
|
||||
content.beginText();
|
||||
content.setFontAndSize(base, 19);
|
||||
|
||||
// 获取每一页的高度、宽度
|
||||
pageSizeWithRotation = reader.getPageSizeWithRotation(i);
|
||||
float pageHeight = pageSizeWithRotation.getHeight();
|
||||
float pageWidth = pageSizeWithRotation.getWidth();
|
||||
int parityNum = 0;
|
||||
// 根据纸张大小多次添加, 水印文字成30度角倾斜
|
||||
for (int height = interval + textH; height < pageHeight; height = height + textH * 6) {
|
||||
parityNum ++;
|
||||
for (int width = interval + textW; width < pageWidth + textW; width = width + textW * 2) {
|
||||
if(parityNum%2==0){
|
||||
width= width + 50;
|
||||
height = height +50;
|
||||
content.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height - textH, 30);
|
||||
}else{
|
||||
content.showTextAligned(Element.ALIGN_LEFT, waterMarkName, width - textW, height - textH, 30);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
content.endText();
|
||||
}
|
||||
|
||||
// 关流
|
||||
stamper.close();
|
||||
reader.close();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -441,7 +441,7 @@ public class AttFileEOController {
|
||||
String waterContent = userMsg;
|
||||
//2020年9月5日 去掉水印处理
|
||||
//2021年7月25日要求下载生成水印
|
||||
WaterMarkUtil.waterMark(oldFilePath,waterFilePath,waterContent);
|
||||
WaterMarkUtil.waterMarkNew(oldFilePath,waterFilePath,waterContent);
|
||||
response.setHeader("Content-Disposition", "attachment;filename=\""+fileOldName+"\"");
|
||||
response.setContentType("application/octet-stream");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user