Embedding a watermark in a spreadsheet helps protect sensitive data and signals document ownership. Conholdate.Total for Java provides a straightforward API that lets you insert Watermark in Excel in Java with just a few lines of code. This guide walks you through the required setup, the complete implementation, and key configuration options so you can secure your Excel workbooks efficiently.
How to Insert Watermark in Excel in Java - Step by Step
Load the Workbook with ExcelLoadOptions
- Load the Excel workbook: Use
Watermarkertogether withExcelLoadOptionsto open the file.
Watermarker watermarker = new Watermarker(inputPath, new ExcelLoadOptions());
The Watermarker class is documented in the API reference. Loading with ExcelLoadOptions gives you control over memory usage, which is essential for large workbooks.
Create a Text Watermark with Styling
- Create a text watermark: Define font, color, opacity, rotation, and alignment.
Font font = new Font("Arial", 36);
TextWatermark watermark = new TextWatermark("CONFIDENTIAL", font);
watermark.setColor(Color.RED);
watermark.setOpacity(0.3);
watermark.setRotationAngle(45);
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setVerticalAlignment(VerticalAlignment.Center);
Setting opacity to 0.3 makes the watermark semi‑transparent, while a 45‑degree rotation places it diagonally across the sheet. You can adjust these values to meet branding guidelines or compliance requirements.
Apply the Watermark to All Worksheets
- Add the watermark to the workbook: The
addmethod applies it to every worksheet in a single call.
watermarker.add(watermark);
If you need per‑worksheet control, you can retrieve individual worksheets via watermarker.getWorksheets() and call add on each one separately.
Save the Watermarked Workbook
- Save the watermarked workbook: Choose
ExcelSaveOptionsto control the output format and preserve original metadata.
watermarker.save(outputPath, new ExcelSaveOptions());
The ExcelSaveOptions object lets you specify whether to keep the original file version or upgrade to the latest XLSX standard.
Release Resources to Avoid Memory Leaks
- Close the Watermarker: Release resources to avoid memory leaks, especially when processing many files in a batch.
watermarker.close();
Closing the Watermarker also flushes any pending write operations, ensuring the output file is not corrupted.
These steps illustrate how to insert Watermark in Excel in Java while giving you control over appearance and performance.
Complete Code Example: Insert Watermark in Excel in Java - Full Java Watermark
The following code demonstrates the entire process from loading the workbook to saving the result.
import com.groupdocs.watermark.Watermarker;
import com.groupdocs.watermark.contents.TextWatermark;
import com.groupdocs.watermark.common.Font;
import com.groupdocs.watermark.common.Color;
import com.groupdocs.watermark.contents.HorizontalAlignment;
import com.groupdocs.watermark.contents.VerticalAlignment;
import com.groupdocs.watermark.options.load.ExcelLoadOptions;
import com.groupdocs.watermark.options.save.ExcelSaveOptions;
public class ExcelWatermarkExample {
public static void main(String[] args) {
String inputPath = "input.xlsx";
String outputPath = "output_watermarked.xlsx";
Watermarker watermarker = null;
try {
// Load the Excel workbook (works for both XLS and XLSX)
ExcelLoadOptions loadOptions = new ExcelLoadOptions();
// For very large workbooks you can enable streaming mode:
// loadOptions.setLoadAllWorksheets(false);
watermarker = new Watermarker(inputPath, loadOptions);
// Create a text watermark with desired appearance
Font font = new Font("Arial", 36);
TextWatermark watermark = new TextWatermark("CONFIDENTIAL", font);
watermark.setColor(Color.RED);
watermark.setOpacity(0.3); // 30% opacity
watermark.setRotationAngle(45); // diagonal
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setVerticalAlignment(VerticalAlignment.Center);
// Apply the watermark to all worksheets
watermarker.add(watermark);
// Save the watermarked workbook
ExcelSaveOptions saveOptions = new ExcelSaveOptions();
watermarker.save(outputPath, saveOptions);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (watermarker != null) {
watermarker.close();
}
}
}
}
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update the file paths (
input.xlsx,output_watermarked.xlsx) to match your actual file locations, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Getting the Environment Ready
Add Maven Repository and Dependency
Add the Conholdate Maven repository and the SDK dependency to your pom.xml:
<repositories>
<repository>
<id>conholdate-repo</id>
<name>Conholdate Maven Repository</name>
<url>https://repository.conholdate.com/repo/</url>
</repository>
</repositories>
<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>24.9</version>
<type>pom</type>
</dependency>
You can also download the latest JARs directly from the download page. The SDK requires Java 8 or higher, and a compatible IDE such as IntelliJ IDEA or Eclipse will make debugging easier.
Configure Licensing (Production Use)
Before running the watermark code in a production environment, apply your license file:
import com.groupdocs.watermark.License;
License license = new License();
license.setLicense("path/to/Conholdate.Total.Java.lic");
A temporary license is available for evaluation at the temporary license page. For long‑term projects, review the pricing page to choose the right plan.
What Makes Conholdate.Total for Java Suitable for Excel Watermarking
Comprehensive XLS/XLSX Support
The library handles both legacy XLS and modern XLSX formats without requiring conversion, preserving formulas, charts, and macros.
Efficient Streaming Mode for Large Workbooks
When working with workbooks that contain thousands of rows, enable streaming by setting loadOptions.setLoadAllWorksheets(false). This reduces memory consumption dramatically.
Rich Watermark Styling Options
You can control font family, size, color, opacity, rotation angle, and alignment. The table below summarizes the most commonly tweaked properties:
| Property | Example Value | Impact |
|---|---|---|
| Font | new Font("Arial", 36) | Determines the visual style of the watermark text. |
| Color | Color.RED | Sets the watermark color; any RGB value is accepted. |
| Opacity | 0.3 (30%) | Controls transparency; lower values make the watermark less intrusive. |
| Rotation Angle | 45 degrees | Positions the watermark diagonally for better coverage. |
| Horizontal Alignment | HorizontalAlignment.Center | Places the watermark horizontally. |
| Vertical Alignment | VerticalAlignment.Center | Places the watermark vertically. |
Automatic Worksheet Coverage
A single call to watermarker.add(watermark) propagates the watermark to every sheet, saving you from writing repetitive loops.
Simple Save Options
ExcelSaveOptions lets you keep the original file format or upgrade to the latest XLSX version, and you can also specify whether to preserve macros.
All these capabilities are described in detail in the official documentation.
Conclusion
In this tutorial you learned how to insert Watermark in Excel in Java using the powerful features of Conholdate.Total for Java. By following the step‑by‑step guide, you can protect your spreadsheets with custom text, control appearance, and handle large files efficiently. For production deployments you’ll need a licensed version; explore pricing on the pricing page and obtain a temporary license from the temporary license page to evaluate the SDK.
FAQs
How do I add an image watermark instead of text?
Use theImageWatermarkclass, set its opacity and alignment, and add it to theWatermarkerinstance just like the text example. The image can be a PNG, JPEG, or BMP, and you can also scale it to fit the worksheet. See the API reference for details.Will the watermark be visible after converting the workbook to PDF?
Yes. The watermark is embedded in the workbook and will appear in any format you later convert, such as PDF, when using Conholdate.Total for Java. The conversion process retains all visual elements, including watermarks.Can I control the watermark on a per‑worksheet basis?
The SDK applies the watermark to all worksheets by default, but you can load individual worksheets usingwatermarker.getWorksheets().get_Item(index)and calladdon each one selectively. This gives you fine‑grained control for reports that require different branding per sheet.Do I need a license for development and testing?
A temporary license is available for evaluation at the temporary license page. For commercial use, purchase a full license via the pricing page. The license file must be loaded at runtime as shown in the Setup section.
