DEV Community

Leon Davis
Leon Davis

Posted on

Remove Watermarks from Word Documents in Java

Watermarks are often used in Word documents to indicate their status, such as "Confidential," "Draft," or "Sample." However, when preparing a document for final distribution or sharing, you may need to remove these watermarks to present a cleaner version of the file.

In this guide, we’ll show you how to remove watermarks from Word documents using Java. Whether you're automating the process or working with a single file, this approach will help you remove unwanted watermarks with minimal effort.

Why Remove a Watermark?

Watermarks can serve many purposes during the drafting process, but once the document is finalized or ready for sharing, they may no longer be necessary. In such cases, removing the watermark will make the document look more polished and professional. Some common reasons for removing watermarks include:

  • Finalizing a draft document for publication.
  • Preparing documents for professional presentation or distribution.
  • Automating document processing for batch file workflows.

Now, let’s walk through the steps required to remove a watermark from a Word document using Java.

Step-by-Step Guide to Remove Watermarks from Word Documents in Java

Step 1: Set Up Your Java Project

Before you can manipulate Word documents in Java, you'll need to add a library that supports document manipulation. One such library is Spire.Doc for Java, which provides a simple API to work with Word documents. You can include this library in your project using Maven by adding the following dependency to your pom.xml:

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.11.2</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Once the library is added to your project, you can begin working with Word documents.

Step 2: Load the Document

Now, let's load the Word document from which you want to remove the watermark. Here's how you can do that:

import com.spire.doc.*;

public class RemoveWatermark {

    public static void main(String[] args) {
        // Load the document
        Document document = new Document();
        document.loadFromFile("sample.docx");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we load a Word document (sample.docx) into the Document object. This is the document containing the watermark that you want to remove.

Step 3: Remove the Watermark

To remove the watermark, you simply need to use the setWatermark() method. By passing null to this method, any watermark present in the document will be removed.

// Remove the watermark
document.setWatermark(null);
Enter fullscreen mode Exit fullscreen mode

This line of code will clear any watermark from the document, whether it’s a text watermark or an image-based watermark.

Step 4: Save the Document Without the Watermark

Once the watermark is removed, you can save the document to a new file. You can specify the desired file format when saving the document. Here, we’ll save it as a DOCX file:

// Save the document to a new file
String output = "remove_watermark.docx";
document.saveToFile(output, FileFormat.Docx_2013);
Enter fullscreen mode Exit fullscreen mode

This will save the modified document without the watermark as remove_watermark.docx.

Complete Code Example

Here’s the full code to remove a watermark from a Word document and save it without the watermark:

import com.spire.doc.*;

public class RemoveWatermark {

    public static void main(String[] args) {
        // Load the document
        Document document = new Document();
        document.loadFromFile("sample.docx");

        // Remove the watermark
        document.setWatermark(null);

        // Save the document to a new file
        String output = "remove_watermark.docx";
        document.saveToFile(output, FileFormat.Docx_2013);

        System.out.println("Watermark removed and document saved as " + output);
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Removing watermarks from Word documents is a simple process in Java. By following the steps outlined in this guide, you can easily automate watermark removal for individual documents or even batch-process multiple files. Whether you're finalizing drafts or preparing documents for professional use, this approach allows you to clean up your files quickly and efficiently.

Top comments (0)