Java实现对PDF、纵向、横向页面添加自定义水印
- 效果图 -- 纵向 页面PDF
- 使用到JAR Maven依赖版本
- 效果图 -- 横向页面PDF
效果图 – 纵向 页面PDF
代码如下:
使用到JAR Maven依赖版本
<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.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.util.Matrix;import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;/*** Java 对PDF 纵向 页面天添加自定义水印*/public class PDFWatermark {public static void main(String[] args) {try {// 输入PDF文件InputStream inputStream = new FileInputStream("W:\\pdf\\77777.pdf"); //需要更换文件地址// 读取PDF文档PDDocument document = PDDocument.load(inputStream);// 自定义水印内容String watermarkText = "lemenpan 2024-05-28 14:23:35 88888888888";// 遍历每一页for (PDPage page : document.getPages()) {// 获取页面宽高float pageWidth = page.getMediaBox().getWidth();float pageHeight = page.getMediaBox().getHeight();// 检查页面是否为纵向if (pageHeight > pageWidth) {// 开始添加水印PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);// 设置水印透明度PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();graphicsState.setNonStrokingAlphaConstant(0.5f);contentStream.setGraphicsStateParameters(graphicsState);// 设置水印字体和大小float fontSize = 41.0f;contentStream.setFont(PDType1Font.HELVETICA_BOLD, fontSize);// 设置水印颜色contentStream.setNonStrokingColor(200, 200, 200);// 设置旋转角度,以页面中心为原点contentStream.transform(Matrix.getRotateInstance(Math.toRadians(55), pageWidth / 2, pageHeight / 2));// 计算水印位置(居中)float x = -fontSize * watermarkText.length() / 4; // 基于字符长度和字体大小的估计float y = -fontSize / 4; // 基于字体大小的估计// 添加水印文本contentStream.beginText();contentStream.newLineAtOffset(-400, -30);contentStream.showText(watermarkText);contentStream.endText();// 关闭内容流contentStream.close();}}UUID uuid = UUID.randomUUID();// 保存带有水印的PDFdocument.save("W:\\pdf\\" + uuid.toString() + ".pdf"); //需要更换文件地址// 关闭文档document.close();} catch (Exception e) {e.printStackTrace();}}
}
效果图 – 横向页面PDF
代码如下:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.util.Matrix;import java.io.FileInputStream;
import java.io.InputStream;
import java.util.UUID;/*** Java 对PDF 横向 页面天添加自定义水印*/public class PDFWatermark11HangXiang {public static void main(String[] args) {try {// 输入PDF文件InputStream inputStream = new FileInputStream("W:\\pdf\\88888.pdf"); //需要更换文件地址// 读取PDF文档PDDocument document = PDDocument.load(inputStream);// 自定义水印内容String watermarkText = "lemenpan 2024-05-28 14:23:35 88888888888";// 遍历每一页for (PDPage page : document.getPages()) {// 获取页面宽高float pageWidth = page.getMediaBox().getWidth();float pageHeight = page.getMediaBox().getHeight();// 检查页面是否为横向if (pageWidth > pageHeight) {// 开始添加水印PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);// 设置水印透明度PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();graphicsState.setNonStrokingAlphaConstant(0.5f);contentStream.setGraphicsStateParameters(graphicsState);// 设置水印字体和大小contentStream.setFont(PDType1Font.HELVETICA_BOLD, 50);// 设置水印颜色contentStream.setNonStrokingColor(200, 200, 200);// 计算水印位置(居中)float fontSize = 50.0f;float stringWidth = PDType1Font.HELVETICA_BOLD.getStringWidth(watermarkText) / 1000 * fontSize;float x = (pageWidth - stringWidth) / 2;float y = (pageHeight - fontSize) / 2;// 设置旋转角度contentStream.transform(Matrix.getRotateInstance(Math.toRadians(35), pageWidth / 2, pageHeight / 2));// 添加水印文本contentStream.beginText();contentStream.newLineAtOffset(-420,-40 ); //调节 水印内容位置 ,为 X 、Y 坐标。contentStream.showText(watermarkText);contentStream.endText();// 关闭内容流contentStream.close();}}UUID uuid = UUID.randomUUID();// 保存带有水印的PDFdocument.save("W:\\pdf\\" + uuid.toString() + ".pdf"); //需要更换文件地址// 关闭文档document.close();} catch (Exception e) {e.printStackTrace();}}
}