Add Watermark to Excel Sheet in Java

Watermarks can be added to the documents either to protect the document from piracy or to show any symbol or message. In other posts, we discussed ways to watermark different documents, images, and presentations. In this article, you will learn how to add watermark to Excel workbooks in different ways in Java. We will be applying watermarks separately using each approach.

The following topics are covered below:

Java API to Watermark Excel Sheets

GroupDocs.Watermark for Java is the API to automate the watermarks for documents, presentations, images, and many other file formats. The complete list of supported document formats is available in the documentation.

You can download the JAR file from the downloads section or use the latest repository and dependency Maven configurations within your Java applications.

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>https://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-watermark</artifactId>
        <version>21.3</version> 
</dependency>

Watermark Excel Sheets using Java

The watermarking API provides customization while inserting the watermark to the spreadsheets as a text. The following are the steps to add watermarks to Excel workbooks in Java.

  • Load the source spreadsheet using Watermarker and the SpreadsheetLoadOptions.
  • Define the watermark text and appearance properties using TextWatermark.
  • Add the defined watermark to the Excel worksheet using add() mehtod.
  • Save the resultant spreadsheet with watermark using the save() method.

The following Java code sample adds the text watermark to all the sheets of the Excel workbook with rotation and opacity and the set alignment.

Watermark Specific Excel Sheet using Java

Likewise, you can also insert watermarks into any single sheet of the workbook. The following steps guide on how to apply text watermark to the specific sheet of the Excel workbook in Java.

  • Load the spreadsheet using the Watermarker.
  • Set the watermark appearance and text using the TextWatermark.
  • Set the worksheet index so that watermark is applied to the mentioned sheet only.
  • Add the text watermark to the Excel worksheet using add() mehtod with watermarking options.
  • Save the output spreadsheet having the watermark using the save() method.

The following Java code snippet applies the text watermark to only the mentioned sheet of the Excel workbook.

Watermark Excel Sheets as Background using Java

Likewise, we can also add watermarks as the background of the spreadsheet. There will be some modification to the above approach to applying watermarks. The following are the steps that insert background text watermark to Excel spreadsheet in Java.

  • Load the spreadsheet using Watermarker.
  • Prepare the watermark text and its appearance using TextWatermark.
  • Set the watermark settings to make it as background using watermarking options by getting content and setting dimensions.
  • Add the watermark to the workbook sheets using the add() mehtod.
  • Lastly, save the watermarked spreadsheet using the save() method.

The following code sample can be used to add a background text watermark to an Excel spreadsheet in Java.

Watermark Excel Sheets Programmatically

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

In this article, we discussed that how we can add watermarks to the excel sheets in different ways within the Java application. We learned to insert text watermark to all the sheets of the Excel workbook, and then we applied the watermark to the specific sheet only. Later, we applied the watermark as a background. You can now use this feature and build your own application to watermark spreadsheets.

Learn more about the API from the documentation. For queries, contact us via the forum.

See Also