Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@ jobs:

# Steps represent a sequence of watermark that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
submodules: true # Fetch Hugo themes
fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod
submodules: true
fetch-depth: 0
- name: Checkout theme repo
uses: actions/checkout@main
uses: actions/checkout@v4
with:
repository: groupdocs/tutorials-theme
token: ${{ secrets.REPO_TOKEN }}
fetch-depth: 0
path: themes/tutorials-theme
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install Theme Dependencies
run: npm install
working-directory: ./themes/tutorials-theme
# Step 2 - Sets up the latest version of Hugo
working-directory: ./themes/tutorials-theme
- name: Setup Hugo
uses: peaceiris/actions-hugo@v3
with:
Expand All @@ -54,7 +56,6 @@ jobs:
run: npm install -D --save postcss-cli
- name: Install Dependicies3
run: npm install -D postcss
# Also specifies the theme we want to use
- name: Build
run: hugo --configDir config/watermark --environment production --minify

Expand Down
13 changes: 10 additions & 3 deletions .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: macos-latest
runs-on: ubuntu-latest

# Steps represent a sequence of watermark that will be executed as part of the job
steps:
Expand All @@ -39,12 +39,19 @@ jobs:
token: ${{ secrets.REPO_TOKEN }}
ref: beta
fetch-depth: 0
path: themes/tutorials-theme
path: themes/tutorials-theme
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install Theme Dependencies
run: npm install
working-directory: ./themes/tutorials-theme
# Step 2 - Sets up the latest version of Hugo
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: '0.97.3'
hugo-version: 'latest'
extended: true
- name: Install Dependicies1
run: npm install -D --save autoprefixer
Expand Down
8 changes: 4 additions & 4 deletions config/watermark/_default/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ footer_about_disable = false
minifyOutput = true
[minify.tdewolff]
[minify.tdewolff.css]
decimals = -1
keepCSS2 = true
precision = 0
version = 0
[minify.tdewolff.html]
keepConditionalComments = true
keepSpecialComments = true
keepDefaultAttrVals = true
keepDocumentTags = true
keepEndTags = true
Expand All @@ -274,7 +274,7 @@ footer_about_disable = false
[minify.tdewolff.js]
[minify.tdewolff.json]
[minify.tdewolff.svg]
decimals = -1
precision = 0
[minify.tdewolff.xml]
keepWhitespace = false

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
---
date: '2026-08-14'
description: 了解如何使用 GroupDocs.Watermark for Java 为 PDF 文件添加水印。只需几个简单步骤,即可保护文档并提升品牌形象。
keywords:
- how to add watermark
- watermark pdf java
- secure pdf watermark
- add text watermark pdf
- pdf branding watermark
lastmod: '2026-08-14'
og_description: 使用 GroupDocs.Watermark for Java 为 PDF 添加水印。本指南逐步演示如何嵌入文字水印、提升安全性,并在
Java 应用程序中强化品牌形象。
og_image_alt: 'Guide: add text watermark to PDF using GroupDocs.Watermark for Java'
og_title: 如何使用 GroupDocs.Watermark Java 为 PDF 添加水印
schemas:
- author: GroupDocs
dateModified: '2026-08-14'
description: Learn how to add watermark to PDF files with GroupDocs.Watermark for
Java. Secure your documents and boost branding in a few simple steps.
headline: How to add a text watermark to PDF using GroupDocs.Watermark for Java
(2023 guide)
type: TechArticle
- questions:
- answer: Yes, GroupDocs.Watermark supports over 50 formats, including DOCX, PPTX,
and image files.
question: Can I watermark non‑PDF files?
- answer: Absolutely – the `TextWatermark` API exposes `setColor()` and `setOpacity()`
methods for fine‑tuned styling.
question: Is it possible to customize text color and opacity?
- answer: Enable memory‑optimized loading and consider processing the file in page‑range
chunks to avoid exhausting heap space.
question: How should I handle PDFs larger than 500 MB?
- answer: Yes, a full license removes trial limitations and grants access to all
premium features.
question: Is a commercial license required for production use?
- answer: The library offers advanced features such as multi‑line watermarks, diagonal
placement, and conditional rendering—refer to the API reference for details.
question: What if I need more complex watermark layouts?
type: FAQPage
tags:
- pdf watermark
- groupdocs watermark
- java pdf security
title: 如何使用 GroupDocs.Watermark for Java 为 PDF 添加文字水印(2023 指南)
type: docs
url: /zh/java/pdf-document-watermarking/add-text-watermark-pdf-java/
weight: 1
---

# 如何使用 GroupDocs.Watermark for Java 为 PDF 添加文本水印(2023 指南)

在 PDF 中添加文本水印是 **how to add watermark** 最有效的方式之一,同时还能强化品牌形象。在本指南中,您将学习如何使用 **GroupDocs.Watermark for Java** 将可自定义的文本水印嵌入任意 PDF 文档,保持文件完整性。

## 快速答案
- **需要哪个库?** GroupDocs.Watermark for Java (v24.11 或更高)。
- **需要哪个 Java 版本?** JDK 8 或更高。
- **是否需要许可证?** 免费试用可用于评估;生产环境需要商业许可证。
- **可以给大 PDF 加水印吗?** 可以——API 能在不将整个文档加载到内存的情况下处理数百页的文件。
- **支持品牌化吗?** 当然——您可以设置字体、颜色、不透明度和旋转角度,以匹配企业风格。

## 什么是 how to add watermark?
**How to add watermark** 指的是以编程方式在 PDF 文件中插入可见文本覆盖层,以表明所有权、机密性或品牌标识的过程。GroupDocs.Watermark for Java 提供了高级 API 来处理繁重的工作,您只需调用少量方法。

## 为什么使用 GroupDocs.Watermark for Java?
GroupDocs.Watermark 支持 **50+** 种输入和输出格式,能够在不完整加载内存的情况下处理 **最高 1 GB** 大小的 PDF,并提供 **线程安全** 的操作,可在多线程环境中扩展。这些量化的能力使其成为企业级 PDF 安全和品牌化的可靠选择。

## 前置条件
- **Java Development Kit (JDK)** 8 或更高。
- **GroupDocs.Watermark 库** v24.11(或更高)。
- 使用 IntelliJ IDEA 或 Eclipse 等带有 Maven 支持的 IDE。
- 具备基本的 Java 知识并熟悉 PDF 结构。

## 设置 GroupDocs.Watermark for Java
首先,将库添加到您的 Maven 项目中:

**Maven 设置**
将以下依赖添加到您的 `pom.xml` 文件中:

```xml
<repositories>
<repository>
<id>repository.groupdocs.com</id>
<name>GroupDocs Repository</name>
<url>https://releases.groupdocs.com/watermark/java/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-watermark</artifactId>
<version>24.11</version>
</dependency>
</dependencies>
```

如果您不想使用 Maven,也可以直接从官方发布页面下载 JAR:

- [GroupDocs.Watermark for Java 发行版](https://releases.groupdocs.com/watermark/java/)

### 许可证获取步骤
- **免费试用** – 生成用于评估的临时许可证密钥。
- **购买** – 提供永久许可证,解锁全部功能。

**基本初始化和设置**
在开始处理 PDF 之前导入所需的类:

```java
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.options.PdfLoadOptions;
```

## 实现指南
下面您将看到一步步的演练,涵盖水印工作流的每个阶段。

### 如何在 Java 中为 PDF 添加文本水印?
加载 PDF,创建文本水印,将其应用到每页,然后保存结果。完整过程可以用 **四个简洁步骤** 表示,您可以将其复制到项目中,快速集成水印功能,代码最少,并确保所有页面外观一致。

### 加载 PDF 文档
**定义锚点** – `PdfLoadOptions` 允许您指定加载参数,例如密码保护或内存使用。
**直接答案** – 实例化 `PdfLoadOptions` 和 `Watermarker` 对象,然后调用 `new Watermarker(inputStream, loadOptions)` 打开 PDF 进行编辑。此步骤确保文档已准备好插入水印,而无需完全加载到 RAM 中。

```java
String pdfPath = "YOUR_DOCUMENT_DIRECTORY/document.pdf";
PdfLoadOptions loadOptions = new PdfLoadOptions();
Watermarker watermarker = new Watermarker(pdfPath, loadOptions);
```
*原因*:配置 `PdfLoadOptions` 可让您细粒度控制 PDF 的解析方式,这对大型或加密文件至关重要。

### 初始化文本水印
**定义锚点** – `TextWatermark` 表示将在每页渲染的可视文本覆盖层。
**直接答案** – 创建 `TextWatermark` 实例,设置其字体、大小、颜色和旋转角度,然后可选地调整不透明度。此对象封装了所有外观设置,您只需将其一次传递给 `Watermarker`。

```java
import com.groupdocs.watermark.common.HorizontalAlignment;
import com.groupdocs.watermark.common.VerticalAlignment;
import com.groupdocs.watermark.watermarks.Font;
import com.groupdocs.watermark.watermarks.SizingType;
import com.groupdocs.watermark.watermarks.TextWatermark;

TextWatermark watermark = new TextWatermark("Protected image", new Font("Arial", 8));
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setVerticalAlignment(VerticalAlignment.Center);
watermark.setRotateAngle(45);
watermark.setSizingType(SizingType.ScaleToParentDimensions);
watermark.setScaleFactor(1);
```
*原因*:适当的样式使水印易读且不突兀,保持用户体验的同时声明所有权。

### 访问 PDF 内容和页面
**定义锚点** – `Watermarker.getPages()` 返回一个集合,允许您操作各个页面。
**直接答案** – 遍历 `watermarker.getPages()`,对每个想要修改的页面调用 `page.addWatermark(textWatermark)`。此方法让您可以针对特定页面或全局应用水印。

```java
import com.groupdocs.watermark.contents.PdfContent;
import com.groupdocs.watermark.contents.PdfPage;

PdfContent pdfContent = watermarker.getContent(PdfContent.class);
for (PdfPage page : pdfContent.getPages()) {
// Process each page as needed.
}
```
*原因*:页面级控制在仅需对特定章节(如封面或机密章节)加水印时非常有用。

### 为图像工件添加水印
**定义锚点** – `ImageArtifact` 对象表示 PDF 页面内嵌的光栅图像。
**直接答案** – 遍历 `page.getImageArtifacts()` 并调用 `artifact.addWatermark(textWatermark)` 将相同的文本水印嵌入每个图像。这可保护可能被提取和重复使用的视觉资产。

```java
import com.groupdocs.watermark.contents.PdfArtifact;

for (PdfPage page : pdfContent.getPages()) {
for (PdfArtifact artifact : page.getArtifacts()) {
if (artifact.getImage() != null) {
artifact.getImage().add(watermark);
}
}
}
```
*原因*:对图像加水印可防止文档中出现的图形、图表或照片被未经授权地重复使用。

### 保存并关闭加水印的 PDF 文档
**定义锚点** – `Watermarker.save(String path)` 将修改后的 PDF 写入文件系统。
**直接答案** – 调用 `watermarker.save("output.pdf")` 然后 `watermarker.close()` 刷新缓冲区并释放文件句柄。此最终步骤确保所有水印更改被持久化,并清理系统资源。

```java
import java.io.File;

String outputPath = "YOUR_OUTPUT_DIRECTORY/output.pdf";
watermarker.save(outputPath);
watermarker.close();
```
*原因*:适当的资源管理可避免文件锁定和内存泄漏,这在高吞吐量服务器环境中尤为重要。

## 实际应用
GroupDocs.Watermark for Java 自然适用于许多实际场景:

- **文档安全** – 在合同、发票或法律简报上嵌入机密声明。
- **品牌化** – 在所有导出的 PDF 中显示公司名称或口号。
- **版权保护** – 通过在每页盖上可见声明来阻止未经授权的分发。

典型的集成点包括自动文档生成流水线、内容管理系统和企业工作流引擎。

## 性能考虑
处理大 PDF 时,请牢记以下最佳实践:

- 使用 `PdfLoadOptions.setLoadMode(LoadMode.MemoryOptimized)` 以保持低内存使用。
- 保存后及时关闭 `Watermarker` 对象。
- 使用线程池批量处理文档,以最大化 CPU 利用率而不导致 I/O 过载。

## 常见问题
**Q: 我可以给非 PDF 文件加水印吗?**
A: 是的,GroupDocs.Watermark 支持超过 50 种格式,包括 DOCX、PPTX 和图像文件。

**Q: 可以自定义文本颜色和不透明度吗?**
A: 完全可以——`TextWatermark` API 提供 `setColor()` 和 `setOpacity()` 方法,以实现精细的样式设置。

**Q: 如何处理大于 500 MB 的 PDF?**
A: 启用内存优化加载,并考虑将文件按页范围分块处理,以避免耗尽堆内存。

**Q: 生产环境是否需要商业许可证?**
A: 是的,完整许可证消除试用限制,并提供所有高级功能的访问权限。

**Q: 如果需要更复杂的水印布局怎么办?**
A: 该库提供高级功能,如多行水印、对角线放置和条件渲染——详情请参阅 API 参考。

## 其他资源
- [文档](https://docs.groupdocs.com/watermark/java/)
- [API 参考](https://reference.groupdocs.com/watermark/java)
- [下载](https://releases.groupdocs.com/watermark/java/)
- [GitHub](https://github.com/groupdocs-watermark/GroupDocs.Watermark-for-Java)
- [免费支持](https://forum.groupdocs.com/c/watermark/10)
- [临时许可证](https://purchase.groupdocs.com/temporary-license/)

通过遵循上述步骤,您现在已经拥有了在 Java 中对 PDF 文件 **how to add watermark** 的坚实基础。将这些模式整合到您自己的服务中,以保护敏感内容、强化品牌并满足合规要求。

---

**最后更新:** 2026-08-14
**测试环境:** GroupDocs.Watermark 24.11 for Java
**作者:** GroupDocs

## 相关教程

- [如何使用 GroupDocs.Watermark for Java 为 PDF 图像批注添加文本水印](/watermark/java/pdf-document-watermarking/add-text-watermark-pdf-annotations-java/)
- [如何使用 GroupDocs.Watermark for Java 为特定 PDF 页面添加文本和图像水印](/watermark/java/pdf-document-watermarking/add-watermarks-pdf-pages-groupdocs-java/)
- [GroupDocs.Watermark for Java:PDF 水印完整指南](/watermark/java/pdf-document-watermarking/groupdocs-watermark-java-pdf-watermark-guide/)
Loading