Previously I showed you how to add a watermark to Word, but this time I'll show you how to add a watermark of multiple characters to Word. You can easily do this using a library called Free Spire.Doc for Java.
1. From the official website of E-iceblue Free Spire.doc for Download the free version of Java .

2. Start the IDE, create a new project, and then add the appropriate Spire.doc.jar to the reference that was in the installed file.

public class Watermark { public static void main(String[] args) { //Load the document. Document doc = new Document(); doc.loadFromFile("C:\Users\Test1\Desktop\Sample.docx"); //Add WordArt shape and set the size ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text); shape.setWidth(50); shape.setHeight(15); //Place the position and format of the text. shape.setVerticalPosition(20); shape.setHorizontalPosition(20); shape.setRotation(315); shape.getWordArt().setText("Copying prohibited"); shape.setFillColor(Color.red); shape.setLineStyle(ShapeLineStyle.Single); shape.setStrokeColor(new Color(192, 192, 192, 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 text.
                shape = (ShapeObject) shape.deepClone();
                shape.setVerticalPosition(40 + 120 * i);
                shape.setHorizontalPosition(20 + 130 * j);
                paragraph1.getChildObjects().add(shape);
            }
        }
    }
    //you save.
    doc.saveToFile("output/AddMultipleTextWatermark.docx", FileFormat.Docx_2013);
}
}
 <h4> <strong> Execution result </strong> </h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210114/20210114145451.png " alt="f:id:lendoris:20210114145451p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p> </p>
        Recommended Posts