DEV Community

Alexis
Alexis

Posted on

Java Add Watermark to Word document

Watermarks are added to the Word documents to specify the ownership or to display the document's status. This article will demonstrate how to add text and image watermarks to word documents in Java using Spire.Doc for Java library.

Getting Spire.Doc.Jar

Method 1: If you are using 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>5.4.2</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Method 2: If you are not using maven, you can download the JAR file from this link, extract the zip file and then import the Spire.Doc.jar file under the lib folder into your project as a dependency.

Add a Text Watermark to Word

The most common text watermarks are “confidential” and “draft”, making it difficult to duplicate the original document.

  • Create a Document instance and 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 another file using Document.saveToFile() method.
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;

public class Textwatermark {
    public static void main(String[] args) throws Exception {

        //Create a Document instance
        Document document = new Document();

        //Load a sample Word document
        document.loadFromFile("Test.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("output/SingleWatermark.docx", FileFormat.Docx);

    }
}

Enter fullscreen mode Exit fullscreen mode

Add text watermark

Add multiple text watermarks to Word

If you want to add multiple text watermarks to a single word document, here are the steps.

  • Create a Document instance and load a sample Word document using Document.loadFromFile() method.
  • Define a WordArt shape using ShapeObject and set the size and the style for the shape.
  • Get the header of the section by HeaderFooter class and then add the header to the paragraph using header.addParagraph() method.
  • Copy the WordArt shape and add it to many places by using paragraph.getChildObjects().add(shape) method.
  • Save the document to another file using Document.saveToFile() method.
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;

public class MultipleTextwatermark {
    public static void main(String[] args) throws Exception {

        //Load the sample document
        Document document = new Document();
        document.loadFromFile("Test.docx");

        //Add WordArt shape and set the size
        ShapeObject shape = new ShapeObject(document, ShapeType.Text_Plain_Text);
        shape.setWidth(60);
        shape.setHeight(20);

        //Set the text, position and style for the shape
        shape.setVerticalPosition(40);
        shape.setHorizontalPosition(20);
        shape.setRotation(315);
        shape.getWordArt().setText("Confidential");
        shape.setFillColor(Color.red);
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(new Color(245, 192, 192, 255));
        shape.setStrokeWeight(1);

        Section section;
        HeaderFooter header;
        for (int n = 0; n < document.getSections().getCount(); n++) {
            section = document.getSections().get(n);
            //Get the header of section
            header = section.getHeadersFooters().getHeader();
            Paragraph paragraph;
            for (int i = 0; i < 5; i++) {
                //Add the header to the paragraph
                paragraph = header.addParagraph();
                for (int j = 0; j < 4; j++) {
                    //copy the wordart shape and add it to many places
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(50 + 150 * i);
                    shape.setHorizontalPosition(20 + 160 * j);
                    paragraph.getChildObjects().add(shape);
                }
            }
        }
        //Save the document to file
        document.saveToFile("Output/Multiplewatermarks.docx", FileFormat.Docx_2013);

    }
}
Enter fullscreen mode Exit fullscreen mode

Multiple text watermarks

Add image watermark

Image watermark usually defines the ownership of the document.

  • Create a Document instance and 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 the image style.
  • Add the image watermark to sample document using Document.setWatermark() method.
  • Save the document to another file using Document.saveToFile() method.

import com.spire.doc.*;

public class ImageWatermark {
       public static void main(String[] args){


           //Create a Document instance
           Document document = new Document();

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

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

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

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

           //Save the result file
           document.saveToFile("output/SingleImagewatermark.docx",FileFormat.Docx );
       }
}
Enter fullscreen mode Exit fullscreen mode

Add image watermark

Add multiple image watermarks to word

The steps of adding multiple image watermarks to the word document.

  • Create a Document instance and load a sample Word document using Document.loadFromFile() method.
  • Load an image by using DocPicture.loadImage() method.
  • Set the text wrapping style using picture.setTextWrappingStyle() method.
  • Get the header of the section by HeaderFooter class and then add the header to the paragraph using header.addParagraph() method.
  • Copy the image and add it to many places by using paragraph.getChildObjects().add(image) method.
  • Save the document to another file using Document.saveToFile() method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

public class MultipleImageWatermark {
       public static void main(String[] args){

           //Create a Document instance
           Document document = new Document();

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

           //Load the image
           DocPicture picture = new DocPicture(document);
           picture.loadImage("logo.png");

           //Set the text wrapping style
           picture.setTextWrappingStyle(TextWrappingStyle.Behind);

           for (int n = 0; n < document.getSections().getCount(); n++) {
               Section section = document.getSections().get(n);

               //Get the header of section
               HeaderFooter header = section.getHeadersFooters().getHeader();
               Paragraph paragraph;
               if(header.getParagraphs().getCount()>0){
                   paragraph=header.getParagraphs().get(0);

               }else {
                   //Add the header to the paragraph
                   paragraph = header.addParagraph();
               }

               for (int p = 0; p < 5; p++) {

                   for (int q = 0; q < 4; q++) {
                       //copy the image and add it to many places
                       picture = (DocPicture)picture.deepClone();
                       picture.setVerticalPosition(100 + 200 * p);
                       picture.setHorizontalPosition(50 + 210 * q);
                       paragraph.getChildObjects().add(picture);
                   }
               }
           }
           //Save the result file
           document.saveToFile("output/MultipleImagewatermarks.docx",FileFormat.Docx );
            }
        }
Enter fullscreen mode Exit fullscreen mode

Multiple image watermarks

Conclusion

In this article, you have learned how to add text and image watermark to Word documents using Java with the help of Spire.Doc for Java. If you have any other questions, please feel free to check the Spire.Doc forums.

Top comments (0)