GroupDocs.Watermark Java SDK を使用して Excel スプレッドシートにウォーターマークを追加する方法
Excel ファイルを不正配布から保護したり、ブランド アイデンティティを強化したりするには、ファイルの表示方法に関係なく常に見えるビジュアル マークを追加するだけで実現できます。このチュートリアルでは、ウォーターマークの追加方法 — 特に画像ウォーターマーク — を GroupDocs.Watermark Java SDK を使って Excel スプレッドシートのヘッダーまたはフッターに追加する方法を学びます。ガイドの最後までに、セル データを変更せずにロゴ、機密バッジ、または任意のカスタム画像をブックに埋め込めるようになります。
Quick Answers
- 「how to add watermark」とは何を指しますか? すべての印刷ページまたはヘッダー/フッターなど特定のセクションに表示される画像(またはテキスト)オーバーレイの追加を指します。
- Java でこれをサポートしているライブラリはどれですか?
GroupDocs.WatermarkJava SDK(最新リリース)。 - ライセンスは必要ですか? 開発用には無料トライアルで動作しますが、本番環境では商用ライセンスが必要です。
- ヘッダーにロゴを追加できますか? はい – 画像ウォーターマークを使用し、ヘッダーオプションを設定します(
add logo to header)。 - 複数シートを一度に処理できますか? ワークシート インデックスをループし、各シートに同じウォーターマークを適用します。
Prerequisites
以下を事前に用意してください:
- GroupDocs.Watermark for Java SDK(バージョン 24.11 以上)。
- Java Development Kit (JDK) 8 以上。
- Java と Excel ファイル構造に関する基本的な知識。
Setting Up GroupDocs.Watermark for Java SDK
まず、Maven 依存関係を追加して SDK をクラスパスに含めます。
Maven Configuration
<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>
Direct Download
Maven を使用したくない場合は、公式リリースページから最新の JAR を取得してください: GroupDocs リリース。
License Acquisition
- すべての機能を評価できる無料トライアルまたは一時ライセンスキーを取得します。
- 商用展開にはフルライセンスを購入してください。
Initialization and Setup
Excel ファイルを読み込み、すべてのウォーターマーク操作のエントリーポイントとなる Watermarker インスタンスを作成します。
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.options.SpreadsheetLoadOptions;
// Initialize Load Options
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
// Create Watermarker instance with your spreadsheet file path
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx", loadOptions);
How to Add Watermark to a Spreadsheet Header/Footer
以下は java add image watermark 機能を実演し、さらに add logo to header の方法も示すステップバイステップのプロセスです。
Step 1: Configure Loading Options
ロード オプションを定義し、Watermarker を再インスタンス化します。これにより SDK がブックを正しく読み取ります。
import com.groupdocs.watermark.common.HorizontalAlignment;
import com.groupdocs.watermark.common.VerticalAlignment;
import com.groupdocs.watermark.options.SpreadsheetLoadOptions;
// Load the document with specified load options
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
Watermarker watermarker = new Watermarker("YOUR_DOCUMENT_DIRECTORY/spreadsheet.xlsx", loadOptions);
Step 2: Create and Configure Image Watermark
埋め込みたい画像(例: ロゴや「CONFIDENTIAL」バッジ)を指す ImageWatermark オブジェクトを作成します。ヘッダー/フッター領域にきれいに収まるよう、配置とスケーリングを調整します。
import com.groupdocs.watermark.watermarks.ImageWatermark;
import com.groupdocs.watermark.watermarks.SizingType;
// Initialize the image watermark with your logo or desired image
ImageWatermark watermark = new ImageWatermark("YOUR_DOCUMENT_DIRECTORY/logo.png");
// Set alignment and scaling for a proper fit within the header/footer
watermark.setVerticalAlignment(VerticalAlignment.Top);
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setSizingType(SizingType.ScaleToParentDimensions); // Scales with parent dimensions
watermark.setScaleFactor(1); // Adjust scale factor as needed
Step 3: Configure Header/Footer Options
どのワークシートのどの部分(ヘッダーまたはフッター)にウォーターマークを適用するかを SDK に指示します。ここで add logo to header を行うか、フッターを選択するかを決めます。
import com.groupdocs.watermark.options.SpreadsheetWatermarkHeaderFooterOptions;
// Set options for applying the watermark
SpreadsheetWatermarkHeaderFooterOptions headerFooterOptions = new SpreadsheetWatermarkHeaderFooterOptions();
headerFooterOptions.setWorksheetIndex(0); // Apply to first worksheet (index 0)
Step 4: Add the Watermark
準備した画像ウォーターマークを選択したヘッダー/フッター位置に付加します。
// Add the configured watermark
watermarker.add(watermark, headerFooterOptions);
Step 5: Save and Close
元のブックを変更しないように新しいファイルに保存し、リソースを解放します。
// Save the watermarked document
watermarker.save("YOUR_OUTPUT_DIRECTORY/out_spreadsheet.xlsx");
// Release resources by closing the Watermarker instance
watermarker.close();
Troubleshooting Tips
- 画像パスを再確認してください。パスが間違っていると
FileNotFoundExceptionがスローされます。 - ワークシート インデックスは 0 から始まります。指定したインデックスが実際に存在することを確認してください。
- ウォーターマークが歪んで見える場合は、
setScaleFactorを調整するか、SizingTypeをStretchToParentDimensionsに変更してください。
Why Use GroupDocs.Watermark Java SDK?
- Cross‑format support – Excel、Word、PowerPoint、PDF、画像に対応。
- No‑loss editing – 元のセル データはそのまま保持されます。
- Performance‑optimized – 大規模ブックやバッチ処理に適しています。
Practical Use Cases
| シナリオ | ウォーターマークの効果 |
|---|---|
| 機密レポート | すべての印刷ページに「CONFIDENTIAL」画像を追加。 |
| ブランド強化 | 請求書のヘッダーに会社ロゴを配置。 |
| バージョン管理 | バージョン番号バッジを埋め込み、変更時に自動更新。 |
| 法令遵守 | 規制対象のスプレッドシートにコンプライアンスシールを付与。 |
Performance Considerations
- Memory usage – 非常に大きなスプレッドシートを処理する際は JVM ヒープを監視してください。
- Batch processing – メモリ フットプリントを抑えるためにファイルをグループで処理します。
- Reuse objects – 複数ファイルで単一の
Watermarkerインスタンスを再利用するとオーバーヘッドが削減されます。
Frequently Asked Questions
Q: Can I add multiple watermarks to a single document?
A: Yes, by creating additional ImageWatermark instances and configuring each before calling watermarker.add().
Q: How do I remove an existing watermark from a spreadsheet?
A: Currently, GroupDocs.Watermark focuses on adding watermarks. To remove one, you’ll need to recreate the workbook without the watermark or use a tool that supports watermark extraction.
Q: What image formats are supported for watermarks?
A: Common formats like PNG and JPEG are supported, but check GroupDocs documentation for a full list of compatible formats.
Q: Is it possible to watermark password‑protected spreadsheets?
A: Yes, as long you provide the necessary password when opening the file.
Q: How do I apply watermarks across all worksheets in a document?
A: Loop through each worksheet index and repeat the watermarking steps, setting headerFooterOptions.setWorksheetIndex(i) for each iteration.
Conclusion
You now have a complete, production‑ready method for java add excel watermark—specifically an image watermark—using the GroupDocs.Watermark Java SDK. This approach adds branding, confidentiality notices, or any visual cue directly into the header or footer of your Excel files while keeping the underlying data untouched. Feel free to explore other watermark types (text, shape) and combine them for richer document protection.
Last Updated: 2026-03-20
Tested With: GroupDocs.Watermark 24.11 for Java
Author: GroupDocs