Java利用Apache PDFBox库为PDF添加文本水印:附详细代码示例
要在Java中为PDF添加水印,您可以使用Apache PDFBox库。以下是一个简单的示例代码,用于向PDF文件添加文本水印: ```java import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.fontsize.PDFontSizeData; import org.apache.pdfbox.pdmodel.fontsize.PDType1Font; public class AddWatermarkToPDF { public static void main(String[] args) { try { // Load PDF document PDDocument document = PDDocument.load(new File("input.pdf")); // Create a watermark image with text and font size BufferedImage watermarkImage = createWatermarkImage("Watermark", 20); // Get the first page of the document PDPage page = document.getPage(0); // Create a content stream for the watermark to be added to the page PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true); contentStream.beginText(); contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12); // Set the font and font size for the watermark text contentStream.newLineAtOffset(50, 50); // Position the watermark at the top-left corner of the page contentStream.showText(watermarkImage); // Add the watermark text to the content stream contentStream.endText(); contentStream.close(); // Close the content stream after adding the watermark // Save the modified PDF document document.save("output.pdf"); document.close(); } catch (IOException e) { e.printStackTrace(); } } private static BufferedImage createWatermarkImage(String text, int fontSize) { BufferedImage watermarkImage = new BufferedImage(100, 50, BufferedImage.TYPE_INT_RGB); // Create a watermark image with a placeholder text string and font size Graphics2D graphics = watermarkImage.createGraphics(); // Create a graphics context for the watermark image graphics.setColor(Color.BLACK); // Set the color of the watermark text to black graphics.setFont(new Font("Arial", Font.PLAIN, fontSize)); // Set the font for the watermark text to Arial with a given font size graphics.drawString(text, 50, 50); // Draw the placeholder text string at the top-left corner of the watermark image with a given position and size graphics.dispose(); // Dispose of the graphics context after drawing the watermark text onto the image return watermarkImage; } } ``` 在上面的代码中,我们首先加载了一个PDF文件,然后使用`createWatermarkImage()`方法创建一个带有文本的水印图像。该方法将使用给定的文本字符串、字体和字体大小来创建图像。接下来,我们获取PDF文档中的第一页,并使用`PDPageContentStream`类创建一个内容流,以便将水印添加到页面上。在内容流中,我们使用字体和字体大小设置水印文本的样式,并将水印文本添加到页面上。我们将修改后的PDF文档保存到磁盘上。您需要将代码中的文件路径和文件名替换为您自己的文件路径和文件名。 ######[AI写代码神器 | 875点数解答 | 2024-02-22 18:17:43]
- Java 实现复制粘贴功能:打造简易文本复制粘贴程序(GPT | 1497点数解答 | 2024-08-25 09:40:33)422
- PDF.js 动态导入模块失败:原因剖析与解决方案全揭秘(字节豆包 | 573点数解答 | 2025-07-30 16:04:03)140
- 解决 Java 中 HTTP 请求方法名含非法字符异常:示例与注意要点(阿里通义 | 321点数解答 | 2024-12-03 17:34:09)371
- Java实现链表反转:迭代与递归双解法详解及开发实战指南(DeepSeek | 1409点数解答 | 2026-03-15 15:09:29)49
- "Resolving 'Incompatible Types' Error with org.apache.pdfbox.pdmodel.pddocument"( | 372点数解答 | 2024-02-18 18:01:15)222
- 解决 Apache PDFBox 中 “incompatible types. found: 'org.apache.pdfbox.pdmodel.pddocument'” 报错的方法 ( | 274点数解答 | 2024-02-18 18:01:41)212
- 解决数据库“dm.jdbc.driver.dmexception: 数据溢出”问题的全步骤指南(阿里通义 | 598点数解答 | 2024-10-15 15:48:38)324
- 达梦数据库数据溢出问题排查及多维度解决方法(阿里通义 | 403点数解答 | 2024-10-15 15:48:41)1201
- jQuery 实现动态操作 DOM:打造可增删任务的待办事项列表应用(百度文心 | 1015点数解答 | 2024-10-26 23:41:22)444
- jQuery 打造实用待办事项列表:输入添加、点击删除一步到位!(阿里通义 | 1139点数解答 | 2024-10-29 08:44:48)360
- jQuery 实现动态操作 DOM:打造带添加与删除功能的待办事项列表应用(阿里通义 | 687点数解答 | 2024-10-29 09:25:06)375
- jQuery 实战:打造可添加与删除任务的待办事项列表应用(字节豆包 | 499点数解答 | 2024-10-29 20:40:48)285