POM
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency>
代码:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.util.Matrix;
import java.awt.*;
import java.io.File;
public class Test1 {
public static void main(String[] args) {
// 原始文件路径
String sourcePath = "D:\\poi\\11.pdf";
// 去c://windows/font下赋值去
String fontFilePath = "D:\\poi\\simhei.ttf";
String waterMarkText = "水印文字";
// log图片的位置
String logoFilePath = "D:\\poi\\logo.jpeg";
// 输出的PDF文件路径
String pdfFile = "D:\\poi\\11-new" + System.currentTimeMillis() + ".pdf";
addWaterRemark(sourcePath, pdfFile, fontFilePath, waterMarkText, logoFilePath);
}
public static void addWaterRemark(String sourcePath, String pdfFile, String fontFilePath, String waterMarkText, String logoFilePath) {
try {
// 读取原始 PDF 文件
PDDocument doc = PDDocument.load(new File(sourcePath));
doc.setAllSecurityToBeRemoved(true);
//必须是ttf字体
PDFont font = PDType0Font.load(doc, new File(fontFilePath));
PDImageXObject logoImage = PDImageXObject.createFromFile(logoFilePath, doc);
float logoImageWidth = logoImage.getWidth();
float logoImageHeight = logoImage.getHeight();
float maxX;
float maxY;
float tempX;
float tempY;
float xStepLength = 300f;
float yStepLength = 200f;
float startX = 100f;
float startY = 100f;
PDPageContentStream cs;
// 透明度
PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();
r0.setNonStrokingAlphaConstant(0.2f);
r0.setAlphaSourceFlag(true);
for (PDPage page : doc.getPages()) {
maxX = page.getBBox().getUpperRightX();
maxY = page.getBBox().getUpperRightX();
cs = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true);
cs.setGraphicsStateParameters(r0);
cs.setNonStrokingColor(new Color(200, 200, 200));
cs.setFont(font, 36f);
// 添加图片,不能在beginText和endText之间加
cs.drawImage(logoImage, (maxX - logoImageWidth) / 2, maxY / 2, logoImageWidth * 1.5f, logoImageHeight * 1.5f);
cs.beginText();
tempX = startX;
while (tempX < maxX) {
tempY = startY;
while (tempY < maxY) {
cs.setTextMatrix(Matrix.getRotateInstance(45, tempX, tempY));
cs.showText(waterMarkText);
tempY += yStepLength;
}
tempX += xStepLength;
}
cs.endText();
// 关闭流
cs.close();
}
// 保存修改后的 PDF 文件
doc.save(new File(pdfFile));
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}