In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "Java how to add text watermark to Word documents", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "Java how to add text watermarks to Word documents" this article.
Methods and ideas
Before adding a watermark to each page of the Word document, you first need to insert a "continuous" section break after the last character in the body of each page of the Word document, then add WordArt type shape objects in the header paragraph of each section, and set WordArt coordinates, styles, alignment, and so on. Finally, save the document.
Jar introduction
Introduce the Spire.Doc.jar file in Free Spire.Doc for Java into the program (the file is in the lib folder). If you need to download and import through Maven, you can configure pom.xml as follows:
Com.e-iceblue https://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.doc.free 5.1.0 Java Code
When adding image watermarks to each page, you can refer to the following steps:
Create an object of the Document class and load the Word document through the Document.loadFromFile (String fileName) method.
Gets the specified section through the Document.getSections (). Get (int index) method.
Get the header through the Section.getHeadersFooters (). GetHeader () method, and add a paragraph to the header by the HeaderFooter.addParagraph () method.
Create an object of the ShapeObject class and pass in a parameter to set the shape type to WordArt of type Text_Plain_Text. And call the method to set the WordArt style, such as WordArt height, width, rotation, color, alignment and so on.
Add WordArt to a paragraph through the Paragraph.getChildObjects (). Add (IdocumentObject entity) method.
Finally, save the document through the Document.saveToFile (String fileName, FileFormat fileFormat) method.
Here is an example of the complete Java code:
Import com.spire.doc.*;import com.spire.doc.documents.*;import com.spire.doc.fields.ShapeObject;import java.awt.*;public class DifferentTextWatermark {public static void main (String [] args) {/ / load the Word test document Document doc = new Document (); doc.loadFromFile ("test.docx"); / / get the first section of the document Section section1 = doc.getSections () .get (0) / / define the vertical coordinate position of the watermark text float y = (float) (section1.getPageSetup (). GetPageSize (). GetHeight () / 3); / / add text watermark 1 HeaderFooter header1 = section1.getHeadersFooters (). GetHeader (); / / get header header1.getParagraphs (). Clear (); / / delete paragraph Paragraph para1= header1.addParagraph () in the original header format / / re-add paragraphs / / add WordArt and set the size ShapeObject shape1 = new ShapeObject (doc, ShapeType.Text_Plain_Text); shape1.setWidth (362); shape1.setHeight (118); / / set WordArt text content, location and style (that is, text watermark) shape1.setRotation (315); shape1.getWordArt () .setText ("Internal use") Shape1.setFillColor (new Color (128128128)); shape1.setLineStyle (ShapeLineStyle.Single); shape1.setStrokeColor (new Color (128128128)); shape1.setStrokeWeight (0.5); shape1.setVerticalPosition (y); shape1.setHorizontalAlignment (ShapeHorizontalAlignment.Center); para1.getChildObjects (). Add (shape1) / / similarly set the text watermark 2 Section section2 = doc.getSections (). Get (1); HeaderFooter header2 = section2.getHeadersFooters (). GetHeader (); header2.getParagraphs (). Clear (); Paragraph para2= header2.addParagraph (); ShapeObject shape2 = new ShapeObject (doc, ShapeType.Text_Plain_Text); shape2.setWidth (362); shape2.setHeight (118) Shape2.setRotation (315); shape2.getWordArt (). SetText ("top secret information"); shape2.setFillColor (new Color (221160221)); shape2.setLineStyle (ShapeLineStyle.Single); shape2.setStrokeColor (new Color (221160221)); shape2.setStrokeWeight (221160221); shape2.setVerticalPosition (y); shape2.setHorizontalAlignment (ShapeHorizontalAlignment.Center); para2.getChildObjects (). Add (shape2) / / similarly set the text watermark 3 Section section3 = doc.getSections (). Get (2); HeaderFooter header3 = section3.getHeadersFooters (). GetHeader (); header3.getParagraphs (). Clear (); Paragraph para3= header3.addParagraph (); ShapeObject shape3 = new ShapeObject (doc, ShapeType.Text_Plain_Text); shape3.setWidth (362); shape3.setHeight (118) Shape3.setRotation; shape3.getWordArt (). SetText ("No Circulation"); shape3.setFillColor (new Color (70130180)); shape3.setLineStyle (ShapeLineStyle.Single); shape3.setStrokeColor (new Color (70130180)); shape3.setStrokeWeight (70130180); shape3.setVerticalPosition (y); shape3.setHorizontalAlignment (ShapeHorizontalAlignment.Center); para3.getChildObjects (). Add (shape3) / / Save document doc.saveToFile ("DifferentTextWatermark.docx", FileFormat.Docx_2013); doc.dispose ();}}
As shown in the figure, each page can display a different text watermarking effect:
The above is all the contents of the article "how to add text watermarks to Word files by Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.