pdf相关依赖:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
加载文件并压缩:
说明:本文PDF打水印后的临时文件不统一在本地存储,直接压缩。也可以统一存同一文件夹,对文件夹再压缩。
public void compressPdfFile(List<File> files, String investigationId, HttpServletResponse response) {
files = files.stream()
.filter(item -> item.getName().endsWith(".pdf"))
.collect(Collectors.toList());
//zip文件名
String compressedFileName = "名字.zip";
try {
response.setHeader("Content-Disposition", "attachment;filename=".concat(URLEncoder.encode(compressedFileName, "UTF-8")));
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
for (File file : files) {
InputStream inputStream = new FileInputStream(file);
File outTempFile = File.createTempFile("tmp", ".pdf");
try {
PdfReader reader = new PdfReader(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(outTempFile);
PdfStamper stamper = new PdfStamper(reader, fileOutputStream);
try {
addWaterMark(reader, stamper);
}catch (Exception e) {
LOGGER.error(file.getName() + "添加水印失败");
throw new Exception("添加水印失败");
} finally {
stamper.close();
reader.close();
fileOutputStream.close();
}
//压缩文件
try(FileInputStream fis = new FileInputStream(outTempFile)) {
ZipEntry ze = new ZipEntry(ecsFileDo.getFileName());
zos.putNextEntry(ze);
byte[] content = new byte[1024];
int len;
while ((len = fis.read(content)) != -1) {
zos.write(content, 0, len);
zos.flush();
}
}catch (Exception e) {
LOGGER.error(file.getName() + "压缩失败");
}
} catch (Exception e) {
LOGGER.error(file.getName() + "压缩失败");
} finally {
if(!outTempFile.delete()) {
LOGGER.error(file.getName() + "临时文件删除失败");
}
}
}
zos.close();
} catch (Exception e) {
throw new Exception("获取pdf文件失败");
}
}
添加水印:
private void addWaterMark(PdfReader reader, PdfStamper stamper){
//水印相关设置
try {
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
StringBuilder waterMarker = new StringBuilder();
waterMarker.append("水印内容");
for (int i = 0; i < reader.getNumberOfPages(); i++) {
float pageHeight = reader.getPageSize(i + 1).getHeight();
float pageWidth = reader.getPageSize(i + 1).getWidth();
int xRepeatTimes = 3;
int yRepeatTimes = 4;
float xGap = pageWidth / xRepeatTimes;
float yGap = pageHeight / yRepeatTimes;
float xOffset = pageWidth / 16;
float yOffset = pageHeight / 16;
PdfContentByte content = stamper.getOverContent(i + 1);
content.beginText();
content.setFontAndSize(baseFont, 16);
content.setRGBColorFill(240, 240, 240); //setColorFill(BaseColor.LIGHT_GRAY)
//设置水印透明度
PdfGState gState = new PdfGState();
gState.setFillOpacity(0.15f);
content.setGState(gState);
for (int lineIndex = 0; lineIndex < xRepeatTimes; lineIndex++) {
for (float colIndex = 0; colIndex < yRepeatTimes; ++colIndex) {
content.showTextAligned(Element.ALIGN_LEFT, waterMarker.toString(), xOffset + lineIndex * xGap, pageHeight - yOffset - colIndex * yGap, -45);
}
}
content.endText();
}
}catch (Exception e){
throw new Exception("添加水印失败");
}
}