Java Adding Watermarks to Word Documents

Keywords: Java Programming Maven

Watermarking is a kind of declaration and anti-counterfeiting means commonly used in various documents. Generally, text watermarking can be set or images can be loaded as watermarking. The following will share how to add watermarking effects to Word documents through Java programming, namely

  • Text watermarking
  • Picture watermark
    Use Tools: Free Spire.Doc for Java (Free Edition)
    Jar import:
    Method 1: Through the official website Download jar File package. After downloading, extract the file and import the Spire.Doc.jar file under the lib folder into the java program. Refer to the following import effect:

    Method 2: Through maven Import. Reference resources Import method.

Java code example (for reference)

[Example 1] Add text watermarking

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

public class Main {

    public static void main(String[] args) {
        //Loading test documents
Document document = new Document();
        document.loadFromFile("sample.docx");

        //Insert text watermarking
InsertTextWatermark(document.getSections().get(0));

        //Save document
        document.saveToFile("textwatermark.docx",FileFormat.Docx );
    }
     //Self-defined method to insert text watermarking and format watermarking effect
     private static void InsertTextWatermark(Section section){
         TextWatermark txtWatermark = new TextWatermark();
         txtWatermark.setText("Internal use");
         txtWatermark.setFontSize(40);
         txtWatermark.setColor(Color.red);
         txtWatermark.setLayout(WatermarkLayout.Diagonal);
         section.getDocument().setWatermark(txtWatermark);
     }
}

[Example 2] Add image watermarking

import com.spire.doc.*;

public class Main {

    public static void main(String[] args) {
        //Loading test documents
        Document document = new Document();
        document.loadFromFile("sample.docx");

        //Loading Pictures as Watermarks
        PictureWatermark picture = new PictureWatermark();
        picture.setPicture("wx.png");
        picture.setScaling(5);
        picture.isWashout(false);
        document.setWatermark(picture);

        //Save document
        document.saveToFile("imagewatermark.docx",FileFormat.Docx );
    }
}

Image watermark effect:

(End of this article)

Reprinted please indicate the source!!

Posted by habanero25 on Fri, 04 Oct 2019 20:49:34 -0700