当前位置: 首页 > news >正文

实现PDF文件添加水印的功能

通过Java代码实现PDF文件添加水印的功能,主要依赖iText库(用于PDF操作)和OSS SDK(可选,用于文件上传)。以下是实现的核心步骤:

首先添加依赖

<!-- 添加 PDF 水印 --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.3</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

添加PDF水印的核心方法

@SpringBootTest
public class FileWatermarkUploader {// OSS 配置信息:需根据你的账号信息进行修改private static final String ENDPOINT = "your-endpoint";private static final String ACCESS_KEY_ID = "your-access-key";private static final String ACCESS_KEY_SECRET = "your-secret-key";private static final String BUCKET_NAME = "your-bucket-name";@Testpublic void T() throws Exception {uploadWithWatermark(new File("D:\\test.pdf"), "oss/path/output.pdf", "测试水印 - 保密 Confidential","D:/output/output.pdf");}/*** 上传带水印的文件至 OSS,并保存到本地指定路径。** @param inputFile 原始 PDF 文件* @param objectName OSS 上保存的文件名* @param watermarkText 自定义水印文字* @param outputPath 本地保存路径,如 "C:/output/watermarked.pdf"* @throws Exception 处理异常*/public void uploadWithWatermark(File inputFile, String objectName, String watermarkText, String outputPath)throws Exception {// 判断是否是 PDF 文件if (!inputFile.getName().toLowerCase().endsWith(".pdf")) {throw new IllegalArgumentException("只支持 PDF 文件水印处理");}// 添加水印,返回本地水印文件File watermarkedFile = addPdfWatermark(inputFile, watermarkText);// 将临时文件复制到指定输出路径copyFile(watermarkedFile, new File(outputPath));// 上传到 OSS// uploadToOss(watermarkedFile, objectName);// 删除临时文件if (watermarkedFile.exists()) {watermarkedFile.delete();}}/*** 给 PDF 添加铺满整页的文字水印** @param inputPdf 原始 PDF 文件* @param watermarkText 水印文字* @return 添加水印后的临时文件* @throws IOException IO 异常* @throws DocumentException PDF 处理异常*/private File addPdfWatermark(File inputPdf, String watermarkText) throws IOException, DocumentException {File outputPdf = File.createTempFile("watermarked_", ".doc");PdfReader reader = new PdfReader(new FileInputStream(inputPdf));PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPdf));int totalPages = reader.getNumberOfPages();BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);PdfGState gs = new PdfGState();gs.setFillOpacity(0.5f);  // 半透明效果for (int i = 1; i <= totalPages; i++) {PdfContentByte canvas = stamper.getUnderContent(i);canvas.setGState(gs);canvas.setFontAndSize(font, 10);canvas.setColorFill(BaseColor.LIGHT_GRAY);float xSpacing = 150f; // 横向间隔float ySpacing = 100f; // 纵向间隔for (float x = 0; x < 595; x += xSpacing) { // 页面宽度 A4 约 595ptfor (float y = 0; y < 842; y += ySpacing) { // 页面高度 A4 约 842ptcanvas.beginText();canvas.showTextAligned(Element.ALIGN_CENTER, watermarkText, x, y, 45);canvas.endText();}}}stamper.close();reader.close();return outputPdf;}/*** 上传文件至 OSS** @param file 文件对象* @param objectName OSS 保存的文件名* @throws FileNotFoundException 文件找不到异常*/private void uploadToOss(File file, String objectName) throws FileNotFoundException {OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);ossClient.putObject(BUCKET_NAME, objectName, new FileInputStream(file));ossClient.shutdown();}/*** 将文件从源路径复制到目标路径** @param source 原始文件* @param destination 目标文件* @throws IOException IO 异常*/private void copyFile(File source, File destination) throws IOException {try (InputStream in = new FileInputStream(source); OutputStream out = new FileOutputStream(destination)) {byte[] buffer = new byte[1024];int length;while ((length = in.read(buffer)) > 0) {out.write(buffer, 0, length);}}}
}

关键点说明

  • iText依赖:需在项目中引入iText库(如com.itextpdf:itextpdf:5.2.0)。
  • 水印样式:通过PdfGState设置透明度,通过BaseFont设置字体,通过showTextAligned控制水印位置和旋转角度。
  • 水印密度:通过xSpacing和ySpacing调整水印排列密度。
  • 临时文件处理:生成水印文件后建议删除临时文件,避免存储浪费。

相关文章:

  • 机器学习×第十二卷:回归树与剪枝策略——她剪去多余的分支,只保留想靠近你的那一层
  • Ubuntu最新版本(Ubuntu22.04LTS)安装nfs服务器
  • DeserializationViewer使用说明
  • 煤矿井下Modbus转Profibus网关的传感器与PLC互联解决方案
  • ceph 解决 pg一直处于 active+undersized+degraded 状态问题
  • 《Whisper :说明书 》
  • Postman 的 Jenkins 管理 - 自动构建
  • [论文阅读] 人工智能 + 软件工程 | USEagent:迈向统一的AI软件工程师
  • apisix-使用hmac-auth插件进行接口签名身份验证\apisix consumer
  • stm32之使用中断控制led灯
  • C++ 友元
  • 【沉浸式解决问题】baseMapper can not be null
  • 【世纪龙科技】智能网联汽车自动驾驶虚拟实训软件
  • JS红宝书笔记 8.2 创建对象
  • Mybatis之Integer类型字段为0,入库为null
  • Spring-创建第一个SpringBoot项目
  • html实现登录与注册功能案例(不写死且只使用js)
  • Ubuntu编译ffmpeg解决错误:ERROR: avisynth/avisynth_c.h not found
  • Kafka性能压测报告撰写
  • Vue3中使用 Vue Flow 流程图方法
  • 长沙网站制作哪/携程: 2023年旅行搜索上涨超900%
  • 建一个个人网站多少钱/seo外包优化公司
  • 个人主页网站应该怎样做/网络营销的产品策略
  • 网站建设公司软件开/互联网广告营销是什么
  • 万网建网站教程/app开发费用标准
  • 做网站主页效果图/优化大师优化项目有