- maven
<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>
/**
* 水印
* @param inputFilePath
* @param outputFilePath
* @param waterMarkContent
*/
public static void pdfAddWaterMark(String inputFilePath, String outputFilePath, String waterMarkContent) {
try {
// 水印的高和宽
int waterMarkHeight = 30;
int watermarkWeight = 60;
// 水印间隔距离
int waterMarkInterval = 240;
// 读取PDF文件流
PdfReader pdfReader = new PdfReader(inputFilePath);
// 创建PDF文件的模板,可以对模板的内容修改,重新生成新PDF文件
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(outputFilePath));
// 设置水印字体
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
// 设置PDF内容的Graphic State 图形状态
PdfGState pdfGraPhicState = new PdfGState();
// 填充透明度
pdfGraPhicState.setFillOpacity(0.1f);
// 轮廓不透明度
pdfGraPhicState.setStrokeOpacity(0.2f);
// PDF页数
int pdfPageNum = pdfReader.getNumberOfPages() + 1;
// PDF文件内容字节
PdfContentByte pdfContent;
// PDF页面矩形区域
Rectangle pageRectangle;
for (int i = 1; i < pdfPageNum; i++) {
// 获取当前页面矩形区域
pageRectangle = pdfReader.getPageSizeWithRotation(i);
// 获取当前页内容,getOverContent表示之后会在页面内容的上方加水印
pdfContent = pdfStamper.getOverContent(i);
// 获取当前页内容,getOverContent表示之后会在页面内容的下方加水印
// pdfContent = pdfStamper.getUnderContent(i);
pdfContent.saveState();
// 设置水印透明度
pdfContent.setGState(pdfGraPhicState);
// 开启写入文本
pdfContent.beginText();
// 设置字体
pdfContent.setFontAndSize(baseFont, 20);
// 在高度和宽度维度每隔waterMarkInterval距离添加一个水印
for (int height = waterMarkHeight; height < pageRectangle.getHeight(); height = height + waterMarkInterval) {
for (int width = watermarkWeight; width < pageRectangle.getWidth() + watermarkWeight;
width = width + waterMarkInterval) {
// 添加水印文字并旋转30度角
pdfContent.showTextAligned(Element.ALIGN_LEFT, waterMarkContent, width - watermarkWeight,
height - waterMarkHeight, 30);
}
}
// 停止写入文本
pdfContent.endText();
}
pdfStamper.close();
pdfReader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void mergePDF(String[] inputFilePathList, String outputFilePath){
try {
Document document = new Document();
PdfCopy copy = new PdfCopy(document,new FileOutputStream(outputFilePath));
document.open();
for (String s : inputFilePathList){
PdfReader pdfReader = new PdfReader(s);
for (int i =1 ; i<=pdfReader.getNumberOfPages();i++){
copy.addPage(copy.getImportedPage(pdfReader,i));
}
pdfReader.close();
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
标签:String,int,合并,水印,pdfContent,加水,new,PDF
From: https://www.cnblogs.com/jingzihao/p/18051137