Repeating watermarks, also called multi-line watermarks, are a type of watermark that appears multiple times on a page of a Word document at regular intervals. Compared with single watermarks, repeating watermarks are more difficult to remove or obscure, thus offering a better deterrent to unauthorized copying and distribution. This article is going to show how to insert repeating text and image watermarks into Word documents programmatically using Spire.Doc for Java.
- Add Repeating Text Watermarks to Word Documents in Java
- Add Repeating Picture Watermarks to Word Documents in 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 Repeating Text Watermarks to Word Documents in Java
We can insert repeating text watermarks to Word documents by adding repeating WordArt to the headers of a document at specified intervals. The detailed steps are as follows:
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Create an object of ShapeObject class and set the WordArt text using ShapeObject.getWordArt().setText() method.
- Specify the rotation angle and the number of vertical repetitions and horizontal repetitions.
- Set the format of the shape using methods under ShapeObject class.
- Loop through the sections in the document to insert repeating watermarks to each section by adding the WordArt shape to the header of each section multiple times at specified intervals using Paragraph.getChildObjects().add(ShapeObject) method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document; 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 insertRepeatingTextWatermark { public static void main(String[] args) { //Create an object of Document class Document doc = new Document(); //Load a Word document doc.loadFromFile("Sample.docx"); //Create an object of ShapeObject class and set the WordArt text ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text); shape.getWordArt().setText("DRAFT"); //Specify the watermark rotating angle and the number of vertical repetitions and horizontal repetitions double rotation = 315; int ver = 5; int hor = 3; //Set the format of the WordArt shape shape.setWidth(60); shape.setHeight(20); shape.setVerticalPosition(30); shape.setHorizontalPosition(20); shape.setRotation(rotation); shape.setFillColor(Color.BLUE); shape.setLineStyle(ShapeLineStyle.Single); shape.setStrokeColor(Color.CYAN); shape.setStrokeWeight(1); //Loop through the sections in the document for (Section section : (Iterable) doc.getSections() ) { //Get the header of a section HeaderFooter header = section.getHeadersFooters().getHeader(); //Add paragraphs to the header Paragraph paragraph = header.addParagraph(); for (int i = 0; i < ver; i++) { for (int j = 0; j < hor; j++) { //Add the WordArt shape to the header shape = (ShapeObject) shape.deepClone(); shape.setVerticalPosition((float) (section.getPageSetup().getPageSize().getHeight()/ver * i + Math.sin(rotation) * shape.getWidth()/2)); shape.setHorizontalPosition((float) ((section.getPageSetup().getPageSize().getWidth()/hor - shape.getWidth()/2) * j)); paragraph.getChildObjects().add(shape); } } } //Save the document doc.saveToFile("RepeatingTextWatermark.docx"); doc.dispose(); } }
Add Repeating Picture Watermarks to Word Documents in Java
Similarly, we can insert repeating image watermarks into Word documents by adding repeating pictures to headers at regular intervals. The detailed steps are as follows:
- Create an object of Document class.
- Load a Word document using Document.loadFromFile() method.
- Load a picture using DocPicture.loadImage() method.
- Set the text wrapping style of the picture as Behind using DocPicture.setTextWrappingStyle(TextWrappingStyle.Behind) method.
- Specify the number of vertical repetitions and horizontal repetitions.
- Loop through the sections in the document to insert repeating picture watermarks to the document by adding a picture to the header of each section at specified intervals using Paragraph.getChildObjects().add(DocPicture) method.
- Save the document using Document.saveToFile() method.
- Java
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 insertRepeatingPictureWatermark { public static void main(String[] args) { //Create an object of Document class Document doc = new Document(); //Load a Word document doc.loadFromFile("Sample.docx"); //Load a picture DocPicture pic = new DocPicture(doc); pic.loadImage("watermark.png"); //Set the text wrapping style of the picture as Behind pic.setTextWrappingStyle(TextWrappingStyle.Behind); //Specify the number of vertical repetitions and horizontal repetitions int ver = 4; int hor = 3; //Loop through the sections in the document for (Section section : (Iterable) doc.getSections() ) { //Get the header of a section HeaderFooter header = section.getHeadersFooters().getHeader(); //Add a paragraph to the section Paragraph paragraph = header.addParagraph(); for (int i = 0; i < ver; i++) { for (int j = 0; j < hor; j++) { //Add the picture to the header pic = (DocPicture) pic.deepClone(); pic.setVerticalPosition((float) ((section.getPageSetup().getPageSize().getHeight()/ver) * i)); pic.setHorizontalPosition((float) (section.getPageSetup().getPageSize().getWidth()/hor - pic.getWidth()/2) * j); paragraph.getChildObjects().add(pic); } } } //Save the document doc.saveToFile("RepeatingPictureWatermark.docx", FileFormat.Auto); doc.dispose(); } }
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.