Add watermark to excel using GroupDocs.Watermark Java
Watermarking your Excel files can be a game‑changer—whether you need to protect sensitive data, brand your reports, or simply add a professional touch. In this tutorial you’ll learn how to add watermark to excel spreadsheets using GroupDocs.Watermark for Java, with clear explanations, real‑world use cases, and tips to avoid common pitfalls.
Quick Answers
- What library do I need? GroupDocs.Watermark for Java (latest version).
- Which Excel formats are supported? .xlsx and .xls files (unencrypted).
- Can I add image watermarks? Yes – the SDK supports both text and image watermarks.
- Do I need a license for production? A commercial license is required for non‑trial deployments.
- How long does implementation take? About 10‑15 minutes for a basic text watermark.
What is add watermark to excel?
Adding a watermark to an Excel workbook means stamping a visible (or semi‑transparent) text or image onto every worksheet or embedded document. This helps you assert ownership, indicate confidentiality, or reinforce branding across the entire file.
Why use GroupDocs.Watermark for Java?
GroupDocs.Watermark offers a high‑level API that abstracts the complexity of dealing with Office Open XML structures. It lets you:
- Apply watermarks to multiple worksheets in a single call.
- Handle embedded attachments (e.g., embedded PDFs) automatically.
- Preserve original formatting and formulas.
- Work with both text and image watermarks (e.g., add image watermark java).
Prerequisites
- Java Development Environment – Java 8 or higher (JDK).
- GroupDocs.Watermark for Java SDK – download the latest release from here.
- IDE – IntelliJ IDEA, Eclipse, or any Java‑compatible editor.
- Sample spreadsheet – an .xlsx file you want to protect.
You can add the SDK to your project via Maven, Gradle, or by manually placing the JAR files on the classpath.
Import Packages
These imports give you access to the core watermarking classes, spreadsheet handling, and font utilities.
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.examples.Constants;
import com.groupdocs.watermark.domain.DocumentInfo;
import com.groupdocs.watermark.domain.FileType;
import com.groupdocs.watermark.domain.content.SpreadsheetContent;
import com.groupdocs.watermark.domain.content.SpreadsheetWorksheet;
import com.groupdocs.watermark.domain.content.SpreadsheetAttachment;
import com.groupdocs.watermark.domain.document.IDocumentInfo;
import com.groupdocs.watermark.domain.loadoptions.SpreadsheetLoadOptions;
import com.groupdocs.watermark.domain.wrappers.TextWatermark;
import java.awt.Font;
How to watermark spreadsheet – Step‑by‑Step Guide
Below is a practical, numbered walkthrough that shows exactly how to watermark spreadsheet files with Java. Each step includes a short explanation followed by the original code block (unchanged).
Step 1: Set Up Your Watermark Object
Why? This object defines the visual appearance of the stamp you’ll apply.
TextWatermark watermark = new TextWatermark("Confidential", new Font("Arial", 19));
Step 2: Load the Spreadsheet
Why? Opening the workbook gives the SDK a handle to edit.
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
Watermarker watermarker = new Watermarker("your-file-path.xlsx", loadOptions);
Step 3: Access Spreadsheet Content and Worksheets
Why? Excel files can contain many sheets; you need to iterate through each one.
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);
for (SpreadsheetWorksheet worksheet : content.getWorksheets()) {
// Process each worksheet
}
Step 4: Loop Through Attachments in Each Worksheet
Why? Some worksheets embed other documents (e.g., PDFs). Handling them ensures a consistent watermark across the whole file.
for (SpreadsheetAttachment attachment : worksheet.getAttachments()) {
IDocumentInfo info = attachment.getDocumentInfo();
if (info.getFileType() != FileType.Unknown && !info.isEncrypted()) {
// Process the supported attachment
}
}
Step 5: Watermark Each Attached Document
Why? You want the same “Confidential” stamp on every embedded file.
Watermarker attachedWatermarker = attachment.createWatermarker();
attachedWatermarker.add(watermark);
attachment.updateContent(attachedWatermarker);
attachedWatermarker.close();
Step 6: Save All Changes
Why? Persist the modifications to a new workbook.
watermarker.save("your-output-file.xlsx");
Step 7: Close Resources
Why? Freeing resources prevents memory leaks, especially when processing large workbooks.
watermarker.close();
Putting It All Together: Complete Example
The following class combines every step into a single, runnable program. Do not modify the code block – it is identical to the original example.
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.domain.DocumentInfo;
import com.groupdocs.watermark.domain.FileType;
import com.groupdocs.watermark.domain.content.SpreadsheetContent;
import com.groupdocs.watermark.domain.content.SpreadsheetWorksheet;
import com.groupdocs.watermark.domain.content.SpreadsheetAttachment;
import com.groupdocs.watermark.domain.loadoptions.SpreadsheetLoadOptions;
import com.groupdocs.watermark.domain.wrappers.TextWatermark;
import java.awt.Font;
public class WatermarkSpreadsheet {
public static void main(String[] args) {
try {
// Step 1: Create watermark
TextWatermark watermark = new TextWatermark("Confidential", new Font("Arial", 19));
// Step 2: Load spreadsheet
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions();
Watermarker watermarker = new Watermarker("path/to/your/spreadsheet.xlsx", loadOptions);
// Step 3: Access content and worksheets
SpreadsheetContent content = watermarker.getContent(SpreadsheetContent.class);
for (SpreadsheetWorksheet worksheet : content.getWorksheets()) {
// Step 4: Loop attachments
for (SpreadsheetAttachment attachment : worksheet.getAttachments()) {
DocumentInfo info = attachment.getDocumentInfo();
if (info.getFileType() != FileType.Unknown && !info.isEncrypted()) {
// Step 5: Watermark attached documents
Watermarker attachedWatermarker = attachment.createWatermarker();
attachedWatermarker.add(watermark);
attachment.updateContent(attachedWatermarker);
attachedWatermarker.close(); // Clean up
}
}
}
// Step 6: Save modifications
watermarker.save("path/to/output/spreadsheet_watermarked.xlsx");
} catch (Exception e) {
e.printStackTrace();
} finally {
// Step 7: Close resources
// (Ensure resources are closed)
}
}
}
Common Issues and Solutions
| Issue | Why it Happens | Fix |
|---|---|---|
| Encrypted workbook is skipped | The SDK cannot read encrypted files without a password. | Decrypt the file first or provide the password via SpreadsheetLoadOptions.setPassword("pwd"). |
| Watermark not visible on some sheets | The sheet may have a custom view or protection that hides drawing objects. | Disable sheet protection before adding the watermark, then re‑apply it if needed. |
| Performance slowdown on large files | Loading the entire workbook into memory can be heavy. | Process worksheets in batches or increase JVM heap size (-Xmx2g). |
| Image watermark appears distorted | Incorrect scaling settings. | Use ImageWatermark with explicit width/height parameters to maintain aspect ratio. |
Frequently Asked Questions
Q: Can I add an image watermark instead of text?
A: Yes. Use ImageWatermark from the SDK and pass a java.awt.Image instance. This covers the add image watermark java scenario.
Q: How do I control the position of the watermark?
A: The TextWatermark (or ImageWatermark) class provides properties such as setHorizontalAlignment, setVerticalAlignment, and setOpacity to fine‑tune placement and transparency.
Q: Is it possible to watermark multiple Excel files in one run?
A: Absolutely. Wrap the entire workflow in a for loop that iterates over a directory of files, re‑using the same TextWatermark instance.
Q: What happens if the spreadsheet contains charts?
A: Charts are treated as separate drawing objects; the watermark is applied on the worksheet canvas, so charts remain unaffected but are still covered by the translucent stamp.
Q: Can I remove a watermark that was added earlier?
A: The SDK includes watermarker.remove(watermark) methods, but you need to keep a reference to the original watermark object or search by text/content to identify it.
Last Updated: 2026-03-27
Tested With: GroupDocs.Watermark 23.12 (Java)
Author: GroupDocs