Spire.doc for Java is a professional Java Word component that allows developers to easily integrate Word document creation, reading, editing, conversion, and printing into their Java applications.

This article shows you how to add multiple image watermarks to a Word document with spire.doc for Java. You can download the latest version of the test.

Example code is as follows:

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 WordImageWatermark { public static void main(String[] args) throws Exception { //Load the sample file Document doc=new Document(); doc.loadFromFile("Sample.docx"); //Load the image DocPicture picture = new DocPicture(doc); picture.loadImage("Logo.png"); //Set the text wrapping style picture.setTextWrappingStyle(TextWrappingStyle.Behind); for (int n = 0; n < doc.getSections().getCount(); n++) { Section section = doc.getSections().get(n); //Get the head of section HeaderFooter header = section.getHeadersFooters().getHeader(); Paragraph paragrapg1; if(header.getParagraphs().getCount()>0){ paragrapg1=header.getParagraphs().get(0); }else { //Add the header to the paragraph paragrapg1 = header.addParagraph(); } for (int p = 0; p < 3; p++) { for (int q = 0; q < 2; q++) { //copy the image and add it to many places picture = (DocPicture)picture.deepClone(); picture.setVerticalPosition(100 + 200 * p); picture.setHorizontalPosition(50 + 210 * q); paragrapg1.getChildObjects().add(picture); } } } //Save the document to file doc.saveToFile("Result.docx", FileFormat.Docx_2013); }}Copy the code

Output: