How to add a watermark to an Word document by Java
This article mainly explains the "Java how to add watermarks to Word documents", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java how to add watermarks to Word documents" it!
Generally, you can set a text watermark or load a picture as a watermark. Generally, you can set a text watermark or load a picture as a watermark. The following will share ways to add watermarking effects to Word documents through Java programming, namely
Text watermarking
Image watermark
Using tools: Free Spire.Doc for Java (free version)
Jar Import:
Method 1: download the jar (https://www.e-iceblue.cn/Introduce/Free-Spire-Doc-JAVA.html) package through the official website. 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: import through maven. Refer to Import method (https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html).
Java code example (for reference)
1. Add a text watermark
Import com.spire.doc.*;import com.spire.doc.documents.WatermarkLayout;import java.awt.*; public class Main {public static void main (String [] args) {/ / load test document Document document = new Document (); document.loadFromFile ("sample.docx"); / / insert text watermark InsertTextWatermark (document.getSections () .get (0)) / / Save document document.saveToFile ("textwatermark.docx", FileFormat.Docx);} / / Custom method inserts text watermark and formats watermark 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);}
Text watermarking effect:
two。 Add a picture watermark
Import com.spire.doc.*; public class Main {public static void main (String [] args) {/ / load test document Document document = new Document (); document.loadFromFile ("sample.docx"); / / load picture as watermark 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 watermarking effect:
Thank you for your reading, the above is the content of "Java how to add watermarks to Word documents", after the study of this article, I believe you have a deeper understanding of how Java adds watermarks to Word documents, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!