在 Java 中為 Excel 添加文字浮水印 – GroupDocs.Watermark

在當今的數位時代,保護試算表中的敏感資訊至關重要。使用 GroupDocs.Watermark for Java 可輕鬆 Add text watermark excel 檔案,該函式庫允許您直接在 Excel 活頁簿中嵌入自訂文字浮水印。本教學將帶您完成設定、基本使用以及進階樣式設定,讓您在確保文件安全的同時,保持品牌一致性。

快速解答

  • 此函式庫的功能是什麼? 它會在 Excel 檔案中插入可自訂的文字浮水印,且不會更改原始內容。
  • 需要哪個版本? GroupDocs.Watermark for Java 24.11 或更新版本。
  • 需要授權嗎? 免費試用可用於評估;永久授權則會移除所有限制。
  • 可以只針對單一工作表嗎? 可以 — 您可以將浮水印套用於特定工作表。
  • 在大型檔案上速度快嗎? 是的,它使用串流方式處理多百頁的活頁簿,保持低記憶體使用量。

「add text watermark excel」是什麼?

Add text watermark excel 指的是將可見的文字覆蓋層嵌入 Excel 活頁簿,以表示機密性、所有權或品牌。此覆蓋層作為檔案的一部份儲存,於任何相容的檢視器開啟試算表時皆會顯示。

為什麼使用 GroupDocs.Watermark for Java?

GroupDocs.Watermark 支援 30+ 輸入與輸出格式,且可在不將整個活頁簿載入記憶體的情況下處理最高 200 MB 的 Excel 檔案,於一般伺服器硬體上提供次秒級的效能。其 API 完全支援執行緒安全,適合高吞吐量的企業管線。

前置條件

  1. 函式庫與相依性
    • GroupDocs.Watermark for Java(版本 24.11 或更新)
  2. 環境設定
    • 已安裝 Java Development Kit (JDK) 8 或更新版本
  3. 知識需求
    • 基本的 Java 程式設計技能
    • 熟悉 Maven 以管理相依性

如何添加文字浮水印至 Excel – 步驟指南

要在 Excel 活頁簿中嵌入文字浮水印,您首先使用 Watermarker 載入檔案,接著定義浮水印的外觀,最後在儲存前將其套用至目標工作表。此流程簡單明瞭,只需幾行 Java 程式碼即可實作,如下方程式碼片段所示。

步驟 1:載入 Excel 文件

Direct answer: 初始化 Watermarker 類別,傳入 Excel 檔案的路徑與適當的載入選項——此步驟會為浮水印操作做好文件的準備。

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

步驟 2:建立與設定文字浮水印

Direct answer: 實例化 TextWatermark,設定其文字、字型、大小、顏色與對齊方式,然後將其附加至 SpreadsheetWatermarkOptions 物件。

```java
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.contents.SpreadsheetLoadOptions;

// Initialize load options
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();

// Create a watermarker instance for the Excel file
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx", loadOptions);

步驟 3:將浮水印套用至目標工作表

Direct answer: 在 Watermarker 實例上使用 add 方法,傳入選項,並可選擇指定工作表索引以針對單一工作表。

```java
import com.groupdocs.watermark.options.HorizontalAlignment;
import com.groupdocs.watermark.options.VerticalAlignment;
import com.groupdocs.watermark.watermarks.Color;
import com.groupdocs.watermark.watermarks.Font;
import com.groupdocs.watermark.watermarks.TextWatermark;

// Create a text watermark
TextWatermark watermark = new TextWatermark("Confidential", new Font("Segoe UI", 19));

// Set size, alignment and color of the watermark
watermark.setSizingType(TextWatermark.SIZING_TYPE.FIT_TO_PAGE);
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setVerticalAlignment(VerticalAlignment.Center);

步驟 4:儲存已加浮水印的活頁簿

Direct answer: 呼叫 Watermarker 的 save 方法,提供輸出路徑與格式;函式庫會寫入加了浮水印的檔案,同時保留原始資料。

```java
// Save the watermarked document
watermarker.save("YOUR_OUTPUT_DIRECTORY/spreadsheet_watermarked.xlsx");

// Release resources
watermarker.close();

在試算表中套用文字效果於浮水印

透過線條樣式、不透明度與旋轉來提升可見度。

自訂線條格式

Definition anchor: SpreadsheetTextEffects 是一個協助類別,用於定義試算表中文本浮水印的線條粗細、虛線樣式與顏色。

```java
import com.groupdocs.watermark.options.SpreadsheetLineFormat;
import com.groupdocs.watermark.options.SpreadsheetDashStyle;
import com.groupdocs.watermark.options.SpreadsheetLineStyle;

// Set up text effects for the watermark
SpreadsheetTextEffects effects = new SpreadsheetTextEffects();
effects.getLineFormat().setEnabled(true);
effects.getLineFormat().setColor(Color.getRed());
effects.getLineFormat().setDashStyle(SpreadsheetDashStyle.DashDotDot);
effects.getLineFormat().setLineStyle(SpreadsheetLineStyle.Triple);
effects.getLineFormat().setWeight(1);

將效果套用至浮水印選項

將 SpreadsheetTextEffects 整合至 SpreadsheetWatermarkOptions,以控制浮水印在每頁的呈現方式。

```java
import com.groupdocs.watermark.options.SpreadsheetWatermarkShapeOptions;

// Attach effects to the watermark shape options
SpreadsheetWatermarkShapeOptions options = new SpreadsheetWatermarkShapeOptions();
options.setEffects(effects);

實務應用

Text‑watermarked spreadsheets are useful in many scenarios:

  1. 機密報告 – 標記財務報表為「Confidential」以防止洩漏。
  2. Branding – 在面向客戶的試算表上覆蓋公司標誌或口號。
  3. Legal Documentation – 清楚標示合約與協議,以確立真實性。

效能考量

When handling large Excel workbooks, follow these best practices:

  • Stream instead of load: GroupDocs.Watermark 以串流方式處理資料,避免整個文件的記憶體分配。
  • Close resources promptly: 使用 try‑with‑resources 或明確的 close() 呼叫以釋放檔案資源。
  • Tune the JVM: 為超過 100 MB 的檔案增加堆積大小(-Xmx2g),但需監控 GC 暫停。

結論

現在您已了解如何使用 GroupDocs.Watermark for Java 為 add text watermark excel 檔案添加文字浮水印,從基本插入到進階樣式設定。可嘗試不同的字型、顏色與旋轉角度,以符合企業形象,並將此工作流程整合至更大的文件處理管線,以實現自動化保護。

常見問題

Q: GroupDocs.Watermark 支援的最大檔案大小是多少?
A: 該函式庫可在不降低效能的情況下處理最高 200 MB 的 Excel 活頁簿,這歸功於其串流架構。

Q: 我可以自訂字型樣式與大小嗎?
A: 可以 — Font 類別允許您設定字型族、大小、樣式(粗體、斜體)以及任何文字浮水印的顏色。

Q: 能否僅在特定工作表上添加浮水印?
A: 完全可以。使用接受工作表索引或名稱的 add 方法重載,即可針對單獨的工作表。

Q: 在應用浮水印時應如何處理錯誤?
A: 將程式碼包在 try‑catch 區塊中,捕捉 IOException 與 WatermarkException,以優雅地處理檔案存取問題與 API 錯誤。

Q: 之後需要時可以移除浮水印嗎?
A: 可以 — GroupDocs.Watermark 提供 remove API,可在保留原始內容的同時移除先前添加的浮水印。


最後更新: 2026-07-15
測試環境: GroupDocs.Watermark for Java 24.11
作者: GroupDocs

資源

相關教學