Java實現(xiàn)給Word文件添加文字水印
Word中設置水印時,可預設的文字或自定義文字設置為水印效果,但通常添加水印效果時,會對所有頁面都設置成統(tǒng)一效果,如果需要對每一頁或者某個頁面設置不同的水印效果,則可以參考本文中的方法。下面,將以Java代碼為例,對Word每一頁設置不同的文字水印效果作詳細介紹。
方法思路
在給Word每一頁添加水印前,首先需要在Word文檔每一頁正文的最后一個字符后面插入“連續(xù)”分節(jié)符,然后在每一節(jié)的頁眉段落里添加藝術字類型的形狀對象,并設置藝術字的坐標位置、樣式、對齊方式等。最后保存文檔。
Jar引入
在程序中引入 Free Spire.Doc for Java 中的Spire.Doc.jar文件(該文件在lib文件夾下);如果需要通過Maven下載導入,可進行如下配置pom.xml:
<repositories> <repository> <id>com.e-iceblue</id> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>5.1.0</version> </dependency> </dependencies>
Java代碼
給每頁添加圖片水印時,可參考如下步驟:
- 創(chuàng)建Document類的對象,并通過Document.loadFromFile(String fileName)方法加載Word文檔。
- 通過Document.getSections().get(int index)方法獲取指定節(jié)。
- 通過Section.getHeadersFooters().getHeader()方法獲取頁眉,HeaderFooter.addParagraph()方法添加段落到頁眉。
- 創(chuàng)建ShapeObject類的對象,并傳入?yún)?shù)設置形狀類型為Text_Plain_Text類型的藝術字。并調用方法設置藝術字樣式,如藝術字高度、寬度、旋轉、顏色、對齊方式等。
- 通過Paragraph.getChildObjects().add(IdocumentObject entity)方法添加藝術字到段落。
- 最后,通過Document.saveToFile(String fileName, FileFormat fileFormat)方法保存文檔。
不同頁面中設置不一樣的文字水印效果,只需要獲取該頁面對應節(jié)的頁眉段落,然后參考上述用到的方法步驟逐一添加即可。
下面是完整的Java代碼示例:
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) { //加載Word測試文檔 Document doc = new Document(); doc.loadFromFile("test.docx"); //獲取文檔第一節(jié) Section section1 = doc.getSections().get(0); //定義水印文字的縱向坐標位置 float y = (float) (section1.getPageSetup().getPageSize().getHeight()/3); //添加文字水印1 HeaderFooter header1 = section1.getHeadersFooters().getHeader();//獲取頁眉 header1.getParagraphs().clear();//刪除原有頁眉格式的段落 Paragraph para1= header1.addParagraph();//重新添加段落 //添加藝術字并設置大小 ShapeObject shape1 = new ShapeObject(doc, ShapeType.Text_Plain_Text); shape1.setWidth(362); shape1.setHeight(118); //設置藝術字文本內容、位置及樣式(即文本水印字樣) shape1.setRotation(315); shape1.getWordArt().setText("內部使用"); shape1.setFillColor(new Color(128,128,128)); shape1.setLineStyle(ShapeLineStyle.Single); shape1.setStrokeColor(new Color(128,128,128)); shape1.setStrokeWeight(0.5); shape1.setVerticalPosition(y); shape1.setHorizontalAlignment(ShapeHorizontalAlignment.Center); para1.getChildObjects().add(shape1); //同理設置第二節(jié)頁眉中的文字水印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("絕密資料"); shape2.setFillColor(new Color(221,160,221)); shape2.setLineStyle(ShapeLineStyle.Single); shape2.setStrokeColor(new Color(221,160,221)); shape2.setStrokeWeight(0.5); shape2.setVerticalPosition(y); shape2.setHorizontalAlignment(ShapeHorizontalAlignment.Center); para2.getChildObjects().add(shape2); //同理設置第三節(jié)中的頁眉中的文字水印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(315); shape3.getWordArt().setText("禁止傳閱"); shape3.setFillColor(new Color(70,130,180)); shape3.setLineStyle(ShapeLineStyle.Single); shape3.setStrokeColor(new Color(70,130,180)); shape3.setStrokeWeight(0.5); shape3.setVerticalPosition(y); shape3.setHorizontalAlignment(ShapeHorizontalAlignment.Center); para3.getChildObjects().add(shape3); //保存文檔 doc.saveToFile("DifferentTextWatermark.docx",FileFormat.Docx_2013); doc.dispose(); } }
如圖,每一頁均可顯示不同的文字水印效果:
到此這篇關于Java實現(xiàn)給Word文件添加文字水印的文章就介紹到這了,更多相關Java Word文字水印內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot Mybatis Plus自動生成工具類詳解代碼
mybatis-plus 是一個 Mybatis 的增強工具,在 Mybatis 的基礎上只做增強不做改變,為簡化開發(fā)、提高效率而生,這篇文章帶你使用Springboot Mybatis Plus自動生成工具類2021-11-11SpringBoot實現(xiàn)短信驗證碼校驗方法思路詳解
最近做項目遇到這樣的需求,前端是基于BootStrap,html代碼中有BootStrap樣式實現(xiàn)的,具體后臺實現(xiàn)代碼大家通過本文一起學習吧2017-08-08Java的MyBatis框架中MyBatis Generator代碼生成器的用法
這篇文章主要介紹了Java的MyBatis框架中Mybatis Generator代碼生成器的用法,Mybatis Generator主要被用來生成繁瑣的配置文件來提高效率,需要的朋友可以參考下2016-04-04Map按單個或多個Value排序當Value相同時按Key排序
Map可以先按照value進行排序,然后按照key進行排序。 或者先按照key進行排序,然后按照value進行排序,這樣操作都行,這篇文章主要介紹了Map按單個或多個Value排序,當Value相同時按Key排序,需要的朋友可以參考下2023-02-02