Java add text watermark and image watermark to word document

Watermarks are lightly shaded words or pictures on a piece of paper or the digital pages of Word documents. They are often used to indicate the importance of a document and protect it from copying. This article will demonstrate how to use Free Spire.Doc for Java to add text watermark and image watermark to Word document in Java applications.

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>2.7.3</version>
   </dependency>
</dependencies>

Add text watermark

import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;

public class AddTextWatermark {
    public static void main(String[] args) {

        Document document = new Document();
        document.loadFromFile("Input.docx");

        insertTextWatermark(document.getSections().get(0));

        document.saveToFile("result.docx",FileFormat.Docx );
    }
    private static void insertTextWatermark(Section section) {
        TextWatermark txtWatermark = new TextWatermark();
        txtWatermark.setText("Private");
        txtWatermark.setFontSize(60);
        txtWatermark.setColor(Color.red);
        txtWatermark.setLayout(WatermarkLayout.Diagonal);
        section.getDocument().setWatermark(txtWatermark);
    }

}

textwatermark.jpg

Add image watermark

import com.spire.doc.*;


public class AddImageWatermark {
    public static void main(String[] args)  throws Exception{

        Document document = new Document();
        document.loadFromFile("Input.docx");

        PictureWatermark picture = new PictureWatermark();
        picture.setPicture("pic.jpg");
        picture.setScaling(50);
        picture.isWashout(false);
        document.setWatermark(picture);

        document.saveToFile("result2.docx",FileFormat.Docx );
    }
}

imagewatermark.jpg

No Comments Yet