Skip to content
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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
---
date: '2026-07-30'
description: 了解如何在 Java 中为 GroupDocs.Watermark 设置许可证,有效保护文档并高效管理使用。
keywords:
- how to set license
- GroupDocs Watermark Java
- metered licensing Java
lastmod: '2026-07-30'
og_description: 如何在 Java 中为 GroupDocs.Watermark 设置许可证。本指南将指导您安装 SDK、获取计量密钥并配置许可证,以保护您的文档。
og_image_alt: 'Guide: Set license for GroupDocs Watermark in Java'
og_title: 如何在 Java 中为 GroupDocs Watermark 设置许可证
schemas:
- author: GroupDocs
dateModified: '2026-07-30'
description: Learn how to set license for GroupDocs.Watermark in Java, protect your
documents effectively and manage usage efficiently.
headline: How to Set License for GroupDocs Watermark in Java
type: TechArticle
- description: Learn how to set license for GroupDocs.Watermark in Java, protect your
documents effectively and manage usage efficiently.
name: How to Set License for GroupDocs Watermark in Java
steps:
- name: Define the public and private keys
text: Enter the keys you received after registering for a temporary license. `Metered`
is the GroupDocs.Watermark class that handles metered licensing and usage tracking.
*Place your keys in a secure location (environment variables, encrypted config,
etc.) before using them in code.*
- name: Create an instance of the Metered class
text: Instantiate the `Metered` object with your keys. This object will be passed
to the watermark engine during initialization.
- name: Set the metered license using the provided keys
text: Call the `setLicense` method (or the equivalent API call) with your public
and private keys. Once set, all subsequent watermark operations will be billed
according to your usage. > **Pro tip:** Keep the keys out of source control.
Use a secrets manager or encrypted properties file to avoid accidenta
type: HowTo
- questions:
- answer: A temporary license is time‑limited and ideal for evaluation, while a
perpetual license provides unlimited use without recurring fees.
question: What is the difference between a temporary and a perpetual license?
- answer: Yes—replace the metered key initialization with a call to `engine.setLicense("path/to/license/file")`.
question: Can I switch from a metered license to a perpetual one without code
changes?
- answer: The SDK falls back to offline mode; watermarking continues but usage won’t
be reported until connectivity is restored.
question: What happens if the metered service is unreachable?
- answer: The SDK can handle files up to 1 GB; larger files should be split or processed
in streaming mode.
question: Are there file‑size limits for watermarking?
- answer: It works on any platform that supports Java 8+, including Windows, Linux,
and macOS.
question: Does the metered license work on all operating systems?
type: FAQPage
tags:
- set license
- GroupDocs Watermark
- Java licensing
- metered license
- document security
title: 如何在 Java 中为 GroupDocs Watermark 设置许可证
type: docs
url: /zh/java/licensing-configuration/set-metered-license-groupdocs-watermark-java/
weight: 1
---

# 如何在 Java 中为 GroupDocs Watermark 设置许可证

保护知识产权是现代应用程序的首要任务,水印是阻止未经授权分发的有效手段。如果您正在使用 **GroupDocs.Watermark for Java**,则需要一个能够跟踪使用情况并随需求扩展的许可证。本教程解释了在 Java 中 **如何设置许可证**,从安装 SDK 到配置向服务报告消耗的计量密钥。

## 快速答案
- **什么是计量许可证?** 它是一种基于使用量的许可证,记录每个 API 调用,让您只为实际消耗付费。
- **我需要先获取试用吗?** 是的,您可以从 GroupDocs 网站请求临时许可证以评估产品。
- **需要哪个 Java 版本?** Java 8 或更高;SDK 编译针对 JDK 8+。
- **我可以以后切换到永久许可证吗?** 当然——只需用永久许可证文件替换计量密钥。
- **此设置是否兼容 Maven?** 是的,提供了 Maven 坐标以实现无缝依赖管理。

## 计量许可证是什么?
计量许可证是 GroupDocs 提供的云启用授权,记录 SDK 执行的每一次水印操作。每个 API 调用都会在 GroupDocs 的授权服务器上记录,基于实际使用量进行按需付费计费。该模型为开发者提供实时的消耗洞察,帮助控制成本,同时确保完整功能访问。

## 为什么在 GroupDocs Watermark 中使用计量许可证?
GroupDocs.Watermark 支持超过五十种输入和输出格式——包括 PDF、DOCX、PPTX 以及各种图像类型,并且能够在不将整个文档加载到内存中的情况下处理高达 1 GB 的文件,从而保持性能。使用计量许可证,您只为实际运行的操作付费,使解决方案能够在成本有效的情况下扩展,同时保留对所有功能的完整访问。

## 先决条件
- **GroupDocs.Watermark for Java** 版本 24.11 或更高。
- 已安装并配置的 Java Development Kit (JDK) 8 或更高版本。
- 对 Maven 或手动 JAR 管理有基本了解。
- 来自 GroupDocs 门户的临时或永久许可证密钥。

## 如何在 Java 中为 GroupDocs Watermark 设置计量许可证?

加载您的公钥和私钥,创建 `Metered` 实例并应用许可证——全部在三个简洁的步骤中完成。这种方法确保每个水印请求都计入您的账户,让您对消耗有完整的可视性。

### 步骤 1:定义公钥和私钥
输入您在注册临时许可证后收到的密钥。

`Metered` 是处理计量授权和使用跟踪的 GroupDocs.Watermark 类。
*在代码中使用之前,请将密钥放置在安全位置(环境变量、加密配置等)。*

### 步骤 2:创建 Metered 类的实例
使用您的密钥实例化 `Metered` 对象。该对象将在初始化期间传递给水印引擎。

```text
Metered metered = new Metered(System.getenv("GROUPDOCS_PUBLIC_KEY"),
System.getenv("GROUPDOCS_PRIVATE_KEY"));
```

### 步骤 3:使用提供的密钥设置计量许可证
使用您的公钥和私钥调用 `setLicense` 方法(或等效的 API 调用)。设置后,所有后续的水印操作将根据您的使用量计费。

```text
WatermarkEngine engine = new WatermarkEngine();
engine.setMeteredLicense(metered);
```

> **专业提示:** 将密钥保存在源代码控制之外。使用密钥管理器或加密的属性文件,以避免意外泄露。

## 为 Java 设置 GroupDocs.Watermark

### 安装信息

使用 Maven 或直接下载 JAR,将 GroupDocs.Watermark 集成到您的项目中。

**Maven 设置:**
在您的 `pom.xml` 文件中添加以下配置:

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

**直接下载:**
从 [GroupDocs.Watermark for Java 发布版](https://releases.groupdocs.com/watermark/java/) 下载最新版本。

### 许可证获取

要解锁全部功能,请获取免费试用或临时许可证:

- 在 [GroupDocs 网站](https://purchase.groupdocs.com/temporary-license/) 注册以开始。
- 获取密钥后,按照实现指南将其集成到项目中。

### 基本初始化和设置

将 SDK 添加到项目后,导入必要的命名空间并创建水印引擎实例,如上面的代码片段所示。

## 故障排除技巧
- **无效密钥:** 仔细检查公钥和私钥是否完全匹配;任何一个拼写错误都会导致激活失败。
- **许可证文件路径错误:** 如果您更喜欢基于文件的许可证,请确保文件路径是绝对路径或相对于工作目录正确解析。
- **网络问题:** 计量授权需要向外部发起 HTTPS 调用;请确认防火墙允许访问 `api.groupdocs.com`。

## 实际应用
1. **文档安全:** 为 PDF、Word 文档和图像添加可见或不可见的水印,以保护敏感的企业数据。
2. **使用跟踪:** 生成每日水印文档数量的报告,有助于预算编制和合规性。
3. **CMS 集成:** 在内容发布工作流中自动插入水印,许可证自动强制执行。

## 性能考虑因素

**优化性能:**
- 仅在必要时应用水印;对已受保护的文件跳过处理。
- 对于大批量处理,复用同一个 `WatermarkEngine` 实例,以避免重复的初始化开销。

**最佳实践:**
- 在处理数百页的 PDF 时监控 JVM 堆使用情况;如果内存成为瓶颈,考虑使用流式 API。
- 将日志级别设为 `INFO`,以捕获授权调用而不会使控制台信息过载。

## 结论

在本指南中,我们介绍了在 Java 中为 GroupDocs.Watermark **如何设置许可证**,从 Maven 安装到计量密钥配置。遵循这些步骤,您将获得精确的使用跟踪、灵活的计费以及强大的文档保护——且不影响性能。

**后续步骤:**
- 试验不同的水印样式(文字、图像、对角线)。
- 探索高级功能,例如基于用户角色的条件水印。
- 查看 GroupDocs 分析仪表板,监控消耗趋势。

准备好保护您的文档了吗?立即实现该解决方案,安心地知道您的资产受到保护,许可证费用透明可见。

## 常见问题

**Q: 临时许可证和永久许可证有什么区别?**
A: 临时许可证有时间限制,适合评估使用;永久许可证提供无限使用且无需周期性费用。

**Q: 我可以在不更改代码的情况下从计量许可证切换到永久许可证吗?**
A: 可以——将计量密钥初始化替换为对 `engine.setLicense("path/to/license/file")` 的调用。

**Q: 如果计量服务不可用会怎样?**
A: SDK 将回退到离线模式;水印仍会继续,但使用情况将在恢复连接后才会上报。

**Q: 水印对文件大小有限制吗?**
A: SDK 能处理最高 1 GB 的文件;更大的文件应拆分或使用流式模式处理。

**Q: 计量许可证在所有操作系统上都能工作吗?**
A: 它在任何支持 Java 8+ 的平台上均可运行,包括 Windows、Linux 和 macOS。

---

**Last Updated:** 2026-07-30
**Tested With:** GroupDocs.Watermark 24.11 for Java
**Author:** GroupDocs

**资源**
- [文档](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/)

```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>
```

```java
import com.groupdocs.watermark.License;

public class InitializeWatermark {
public static void main(String[] args) throws Exception {
License license = new License();
// Apply the license using your path to the license file
license.setLicense("path/to/your/license/file.lic");
}
}
```

```java
// Step 1: Define the public and private keys for the metered license.
String publicKey = "*****"; // Replace with your actual public key
String privateKey = "*****"; // Replace with your actual private key
```

```java
// Step 2: Create an instance of Metered class.
Metered metered = new Metered();
```

```java
// Step 3: Set the metered license using the provided keys.
metered.setMeteredKey(publicKey, privateKey);
```

## 相关教程

- [GroupDocs.Watermark for Java 许可证和配置教程](/watermark/java/licensing-configuration/)
- [如何在 Java 中设置 GroupDocs.Watermark 许可证:完整指南](/watermark/java/licensing-configuration/groupdocs-watermark-licensing-java-guide/)
- [Java 水印指南:使用 GroupDocs.Watermark API 保护文档](/watermark/java/getting-started/java-watermark-groupdocs-guide/)
Loading