DEV Community

CodeSharing
CodeSharing

Posted on

Copy/Remove Watermark from Word Document in Java Application

In my early post, I have introduced how to add text watermark and image watermark to Word document in Java application. Today, I will introduce how to copy and remove watermark from Word document in Java application by using Free Spire.Doc for Java. Free Spire.Doc for Java supports copy and remove both text watermark and image watermark from Word document.

Installation
Method 1: Download the Free Spire.Doc for Java and unzip it. Then add the Spire.Doc.jar file to your Java application as dependency.

Method 2: You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
   <repository>
      <id>com.e-iceblue</id>
      <name>e-iceblue</name>
      <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>e-iceblue</groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>2.7.3</version>
   </dependency>
</dependencies>

Enter fullscreen mode Exit fullscreen mode

The original Word document is shown below:

Remove Text Watermark

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class RemoveWatermark {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load the Word document
        doc.loadFromFile("result.docx");

        //Set the watermark as null to remove the image (or text) watermark
        doc.setWatermark(null);

        //Save the document
        doc.saveToFile("RemoveWatermark.docx", FileFormat.Docx_2013);
    }
}
Enter fullscreen mode Exit fullscreen mode

Copy Image Watermark

import com.spire.doc.*;

public class CopyWatermark {
    public static void main(String[] args) {
        //load document 1
        Document doc1 = new Document();
        doc1.loadFromFile("result2.docx");

        //load document 2
        Document doc2 = new Document();
        doc2.loadFromFile("target.docx");

        //Get the image watermark of document 1, copy it to document 2
        doc2.setWatermark(doc1.getWatermark());

        //Save the document2
        doc2.saveToFile("CopyWatermark.docx",FileFormat.Docx_2013);
        doc2.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)