Java: Add Text Watermarks or Image Watermarks to Word

A watermark is a very light image or text that sits in the background of a document. When you need to send a document to others, it’s common to add a watermark to the document to prevent unwarranted use or remind readers that the document is confidential or is a draft, sample, etc. In this article, you will learn how to programmatically add a text watermark or an image watermark to a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<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>12.11.0</version>
    </dependency>
</dependencies>
    

Add a Text Watermark to Word

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.loadFromFile() method.
  • Get the first section using Document.getSections().get() method.
  • Create a TextWatermark instance.
  • Set the text, font size, color and layout of the text watermark using the methods offered by TextWatermark class.
  • Add the text watermark to sample document using Section.getDocument().setWatermark() method.
  • Save the document to file using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;

public class WordTextWatermark {
    public static void main(String[] args) {
        //Create a Document instance
        Document document = new Document();

        //Load a sample Word document
        document.loadFromFile("Sample.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Create a TextWatermark instance
        TextWatermark txtWatermark = new TextWatermark();

        //Set the format of the text watermark
        txtWatermark.setText("Confidential");
        txtWatermark.setFontSize(40);
        txtWatermark.setColor(Color.red);
        txtWatermark.setLayout(WatermarkLayout.Diagonal);

        //Add the text watermark to document
        section.getDocument().setWatermark(txtWatermark);

        //Save the document to file
        document.saveToFile("out/result.docx", FileFormat.Docx);
    }

}

Java: Add Text Watermarks or Image Watermarks to Word

Add an Image Watermark to Word

The detailed steps are as follows:

  • Create a Document instance.
  • Load a sample Word document using Document.loadFromFile() method.
  • Create a PictureWatermark instance.
  • Load an image as the image watermark using PictureWatermark.setPicture() method, and then set scaling as well as washout property of the image watermark using PictureWatermark.setScaling() method and PictureWatermark.isWashout() method.
  • Add the image watermark to sample document using Document.setWatermark() method.
  • Save the document to file using Document.saveToFile() method.
  • Java
import com.spire.doc.*;


public class WordImageWatermark {
    public static void main(String[] args)  throws Exception{
        //Create a Document instance
        Document document = new Document();

        //Load a sample Word document
        document.loadFromFile("Sample.docx");

        //Create a PictureWatermark instance
        PictureWatermark picture = new PictureWatermark();

        //Set the format of the picture watermark
        picture.setPicture("logo.png");
        picture.setScaling(100);
        picture.isWashout(false);

        //Add the image watermark to document
        document.setWatermark(picture);

        //Save the result file
        document.saveToFile("out/result2.docx",FileFormat.Docx );
    }
}

Java: Add Text Watermarks or Image Watermarks to Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.