package com.junfun.pms.report.util;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDate;
public class WaterMarkUtils {
public static void main(String[] args) throws DocumentException, IOException {
FileInputStream fileInputStream = new FileInputStream("D:\\tu\\修船测试.pdf");
File imageFile = new File("D:\\tu\\chuanzhang.jpg");
FileInputStream imageInputStream = new FileInputStream(imageFile);
// 将图片流转成字节数组
byte[] imgByte = IOUtils.toByteArray(imageInputStream);
FileOutputStream fileOutputStream = new FileOutputStream("D:\\tu\\修船测试加章.pdf");
WaterMarkUtils.addMultiWatermarkToPdf(fileInputStream, imgByte, fileOutputStream);
}
// public static void pdfs() throws DocumentException, IOException {
// File file = ResourceUtils.getFile("D:\\tu\\修船测试.pdf");
// Image img = Image.getInstance("D:\\tu\\chaunzhang.jpg");
//
// InputStream inputStream = Files.newInputStream(file.toPath());
// // 读取原始 PDF 文件
// PdfReader reader = new PdfReader(inputStream);
// PdfStamper stamper = new PdfStamper(reader, Files.newOutputStream(Paths.get("D:\\tu\\修船测试加章.pdf")));
// try {
// // 获取 PDF 中的页数
// int pageCount = reader.getNumberOfPages();
// // 获取第几页pdf
// PdfContentByte contentByte = stamper.getOverContent(pageCount);
// contentByte.beginText();
// //给图片设置透明度
// PdfGState pdfGState = new PdfGState();
// pdfGState.setFillOpacity(1.0F);
// contentByte.setGState(pdfGState);
// //图片的位置
// img.setAbsolutePosition(330, 220);
// //这是图片的尺寸
// img.scaleAbsolute(100, 100);
// img.setCompressionLevel(100);
// //添加图品结束等等
// contentByte.addImage(img);
// contentByte.endText();
// }catch (Exception e){
// e.printStackTrace();
// }finally {
// // 保存修改后的 PDF 文件并关闭文件流
// stamper.close();
// reader.close();
// }
//
//
// }
public static void addWatermarkToPdf(InputStream pdfInputStream, byte[] imageBytes, OutputStream outputStream) throws DocumentException, IOException {
// 读取原始 PDF 文件
PdfReader reader = new PdfReader(pdfInputStream);
PdfStamper stamper = new PdfStamper(reader, outputStream);
try {
// 获取 PDF 中的页数
int pageCount = reader.getNumberOfPages();
// 获取第几页pdf
PdfContentByte contentByte = stamper.getOverContent(pageCount);
contentByte.beginText();
//给图片设置透明度
PdfGState pdfGState = new PdfGState();
pdfGState.setFillOpacity(1.0F);
contentByte.setGState(pdfGState);
//创建图片对象
Image img = Image.getInstance(imageBytes);
//图片的位置
img.setAbsolutePosition(330, 220);
//这是图片的尺寸
img.scaleAbsolute(100, 100);
img.setCompressionLevel(100);
//添加图品结束等等
contentByte.addImage(img);
contentByte.endText();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 保存修改后的 PDF 文件并关闭文件流
stamper.close();
reader.close();
}
}
public static void addMultiWatermarkToPdf(InputStream pdfInputStream, byte[] imageBytes, OutputStream outputStream) throws DocumentException, IOException {
PdfReader reader = new PdfReader(pdfInputStream);
PdfStamper stamper = new PdfStamper(reader, outputStream);
try {
int pageCount = reader.getNumberOfPages();
for (int i = 1; i <= pageCount; i++) {
PdfContentByte contentByte = stamper.getOverContent(i);
Image img = Image.getInstance(imageBytes);
img.setAbsolutePosition(30, 750); // 设置图片在左上角的位置
img.scaleAbsolute(100, 100); // 设置图片的尺寸
contentByte.addImage(img);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
stamper.close();
reader.close();
}
}
}
最后一个可以加多个水印