Watermarks are added to Word documents to prevent illegal use, define ownership, display document status and other purposes. In this article, you’ll learn how to use Java to add watermarks to Word documents. This article will demonstrate how to insert text and image watermarks into Word documents, respectively.

  • Add text watermarks to Word documents
  • Add an image watermark to a Word document

To process the contents in Word documents, we will use Aspose.Words for Java, a powerful Word processing API that lets you create, modify, or transform Word documents. To test this feature, download the latest version of Aspose.Words for Java at ****.

Adds a text watermark to a Word document in Java

Here are the steps to insert a text watermark in a Word document using Java.

  • First, load the Word Document using the Document class.
  • Create an instance of the TextWatermarkOptions class to set watermark options, such as font family, font size, color, layout, and so on.
  • Add a text watermark to the Document using the document.getwatermark ().settext (String, TextWatermarkOptions) method.
  • Finally, use the document.save (String) method to save the updated Word Document.

The following code example shows how to add a text watermark to a Word document.

// Load the Word document
Document doc = new Document("Word.docx");

// Set watermark options
TextWatermarkOptions watermarkOptions = new TextWatermarkOptions();
watermarkOptions.setFontSize(36);
watermarkOptions.setFontFamily("Arial");
watermarkOptions.setColor(Color.RED);
watermarkOptions.setLayout(WatermarkLayout.DIAGONAL);
watermarkOptions.isSemitrasparent(true);

// Insert watermark
doc.getWatermark().setText("CONFIDENTIAL", watermarkOptions);

// Save the updated document
doc.save("text-watermark.docx");
Copy the code

The following is a screenshot of the Word document after adding the text watermark.

Add an image watermark to a Word document

Here are the steps to add an image watermark to a Word document using Java.

  • First, load the Word Document using the Document class.
  • Create an instance of the ImageWatermarkOptions class to set the watermark options.
  • Loads the watermark image into the BufferedImage object.
  • Use the document.getwatermark ().setimage (BufferedImage, ImageWatermarkOptions) method to add an image watermark to the Document.
  • Finally, use the document.save (String) method to save the updated Word Document.

The following code example shows how to add an image watermark to a Word document.

// Load the Word document
Document doc = new Document("Word.docx");

// Set watermark options
ImageWatermarkOptions watermarkOptions = new ImageWatermarkOptions();
watermarkOptions.isWashout(false);

// Load watermark image
BufferedImage image = ImageIO.read(new File("logo.png")); 

// Insert watermark
doc.getWatermark().setImage(image, watermarkOptions);

// Save the updated document
doc.save("image-watermark.docx");
Copy the code

The following is a screenshot of the Word document after adding the image watermark.

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.