如何使用 GroupDocs.Watermark for Java 為 Excel 工作表添加文字浮水印
當你需要 how to watermark Excel 工作簿——尤其是包含敏感資料的時候——加入清晰、專業的文字浮水印是保護內容及加強品牌形象的最有效方法之一。本教學將逐步說明如何使用 GroupDocs.Watermark Java 程式庫 add text watermark Excel 檔案,涵蓋從專案設定到儲存最終安全工作簿的全部步驟。
快速解答
- What library should I use? 應該使用哪個程式庫? GroupDocs.Watermark for Java.
- Can I add a text watermark to every sheet? 我可以在每個工作表加入文字浮水印嗎? Yes – iterate over each worksheet and apply the same watermark.
- Do I need a license? 我需要授權嗎? A free trial works for evaluation; a permanent license is required for production.
- Which Java version is supported? 支援哪個 Java 版本? JDK 8 or later.
- Will the watermark affect cell data? 浮水印會影響儲存格資料嗎? No, it only overlays images within the worksheet.
什麼是 Excel 浮水印?
Excel 浮水印是指將可見的標記(文字或圖像)直接嵌入工作表的視覺元素(例如圖像)之上,讓任何開啟檔案的人都能看到所有權或機密性聲明。此技術有助於防止未授權的散佈,並為報告增添專業外觀。
為什麼要使用 Java 為 Excel 加入文字浮水印?
- Security – 明確標示機密或專有資訊。
- Brand consistency – 加強公司品牌於所有共享試算表的一致性。
- Automation – Java 可程式化嵌入浮水印,節省手動編輯時間。
- Cross‑format support – 同時支援
.xlsx與舊版.xls檔案。
前置條件
- Java Development Kit (JDK) 8 or newer.
- Maven for dependency management.
- 基本熟悉 Java 語法與物件導向概念。
設定 GroupDocs.Watermark for Java
要開始使用,先將 GroupDocs.Watermark 相依性加入 Maven 專案。
使用 Maven
<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>
直接下載
Alternatively, download the latest JAR from GroupDocs.Watermark for Java releases.
License Acquisition
- Free Trial – Explore all features without cost.
- Temporary License – Use for short‑term testing.
- Purchase – Unlock full production capabilities.
基本初始化
import com.groupdocs.watermark.Watermarker;
// Initialize watermarker instance for your document
Watermarker watermarker = new Watermarker("path/to/your/spreadsheet.xlsx");
實作指南
功能 1:建立文字浮水印並設定其屬性
Creating and customizing the watermark involves setting its text, font, alignment, rotation angle, and scaling.
步驟概覽
Define Your Watermark
import com.groupdocs.watermark.common.HorizontalAlignment;
import com.groupdocs.watermark.common.VerticalAlignment;
import com.groupdocs.watermark.options.SpreadsheetLoadOptions;
import com.groupdocs.watermark.watermarks.Font;
import com.groupdocs.watermark.watermarks.SizingType;
import com.groupdocs.watermark.watermarks.TextWatermark;
// Create a new TextWatermark object
text watermark = new TextWatermark("Protected image", new Font("Arial", 8));
// Configure the watermark properties
text watermark.setHorizontalAlignment(HorizontalAlignment.Center);
text watermark.setVerticalAlignment(VerticalAlignment.Center);
text watermark.setRotateAngle(45); // Set rotation angle
text watermark.setSizingType(SizingType.ScaleToParentDimensions);
text watermark.setScaleFactor(1); // Maintain original size scale factor
- Text and Font – Choose a clear message and a readable font.
- Alignment – Centering works well for most images.
- Rotation & Scaling – A 45° tilt makes the watermark noticeable without obscuring the image.
功能 2:載入試算表文件以進行浮水印
import com.groupdocs.watermark.options.SpreadsheetLoadOptions;
// Load your Excel spreadsheet
documentLoadOptions = new SpreadsheetLoadOptions();
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx", documentLoadOptions);
功能 3:在工作表的圖像上加入文字浮水印
import com.groupdocs.watermark.contents.SpreadsheetContent;
import com.groupdocs.watermark.contents.WatermarkableImageCollection;
// Access content from your loaded document
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);
WatermarkableImageCollection images = content.getWorksheets().get_Item(0).findImages();
for (com.groupdocs.watermark.contents.WatermarkableImage image : images) {
// Add the configured watermark to each image in the worksheet
image.add(watermark);
}
功能 4:儲存並關閉已加浮水印的試算表文件
// Save the changes to a new file
watermarker.save("YOUR_OUTPUT_DIRECTORY/spreadsheet-out.xlsx");
// Close the watermarker instance to free resources
watermarker.close();
實務應用
- Document Security – Guard confidential financial models or HR data.
- Branding – Insert company slogans or legal notices across all shared reports.
- Copyright Protection – Deter plagiarism by marking every exported image.
效能考量
- Keep GroupDocs.Watermark up‑to‑date to benefit from the latest performance tweaks.
- Release the
Watermarkerinstance promptly (close()) to free memory. - For very large workbooks, process worksheets in batches to avoid high memory consumption.
常見問題與解決方案
| Issue | Solution |
|---|---|
| Watermark not visible | Verify that the worksheet actually contains images; the API only watermarks images, not cells. |
| Misaligned watermark | Adjust HorizontalAlignment / VerticalAlignment or change RotateAngle. |
| Out‑of‑memory errors on big files | Increase JVM heap (-Xmx) or process each worksheet separately. |
| License errors | Ensure the trial or permanent license file is correctly referenced in your project. |
常見問答
Q: Can I use this on all Excel versions?
A: Yes, GroupDocs supports both .xlsx and .xls formats.
Q: What if my watermark doesn’t appear correctly?
A: Double‑check alignment settings and make sure the image dimensions are suitable for the chosen scale factor.
Q: How can I customize the font style further?
A: Use the Font class to set bold, italic, color, or other typographic attributes.
Q: Is it possible to add watermarks to all sheets in a workbook?
A: Absolutely—loop through content.getWorksheets() and apply the same image.add(watermark) logic to each sheet.
Q: How do I handle large Excel files efficiently?
A: Process worksheets incrementally, close each Watermarker after saving, and consider increasing the JVM heap size.
資源
- GroupDocs.Watermark Documentation
- API Reference
- Download GroupDocs.Watermark for Java
- GitHub Repository
- Free Support Forum
- Temporary License Application
透過將這些步驟整合至你的 Java 專案,你可以 java add watermark excel 檔案快速完成,確保安全性與品牌一致性。
最後更新: 2026-03-22
測試環境: GroupDocs.Watermark 24.11 for Java
作者: GroupDocs