DEV Community

CodeSharing
CodeSharing

Posted on

Java/ Add Multiple Text Watermarks to Word Document

In my previous articles, I have demonstrated how to add text watermark and image watermark to word document by using Free Spire.Doc for Java. In this article, I will introduce how to add WordArt to the Word header to get the multiple watermarks added to 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>3.9.0</version>
   </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

Add Multiple Text Watermarks:

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 WordWatermark {

    public static void main(String[] args) {

        //Load the sample document
        Document doc = new Document();
        doc.loadFromFile("Input.docx");
        //Add WordArt shape and set its size
        ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
        shape.setWidth(60);
        shape.setHeight(20);
        //Set the text, position and sytle for the WordArt
        shape.setVerticalPosition(30);
        shape.setHorizontalPosition(20);
        shape.setRotation(315);
        shape.getWordArt().setText("Confidential");
        shape.setFillColor(Color.red);
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(new Color(196, 0, 0, 255));
        shape.setStrokeWeight(1);

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

    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)