Java在Word中添加多行圖片水印
更新時間:2021年02月09日 22:13:13 作者:E-iceblue
這篇文章主要介紹了Java在Word中添加多行圖片,圖文講解的很清晰,有對于這方面不懂得同學可以跟著研究下
Word中設置水印效果時,不論是文本水印或者是圖片水印都只能添加單個文字或者圖片到Word頁面,效果比較單一,本文通過Java代碼示例介紹如何在頁面中添加多行圖片水印效果,即水印效果以多個圖片平鋪到頁面。(添加多行文字水印效果,可以查看這篇文章中的方法)
程序環(huán)境:使用spire.doc.jar,版本:3.9.0
Java代碼:
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.TextWrappingStyle; import com.spire.doc.fields.DocPicture; public class ImageWatermark { public static void main(String[] args) { //加載Word文檔 Document doc=new Document(); doc.loadFromFile("input.docx"); //加載圖片 DocPicture picture = new DocPicture(doc); picture.loadImage("logo.png"); picture.setTextWrappingStyle(TextWrappingStyle.Behind);//設置圖片環(huán)繞方式 //遍歷所有section for (int n = 0; n < doc.getSections().getCount(); n++) { Section section = doc.getSections().get(n); //獲取section的頁眉 HeaderFooter header = section.getHeadersFooters().getHeader(); Paragraph paragrapg1; //獲取或添加段落 if(header.getParagraphs().getCount()>0) { paragrapg1 = header.getParagraphs().get(0); } else { paragrapg1 = header.addParagraph(); } //復制圖片,并添加圖片到段落 for (int p = 0; p < 4; p++) { for (int q = 0; q < 3; q++) { picture = (DocPicture)picture.deepClone(); picture.setVerticalPosition(50 + 150 * p); picture.setHorizontalPosition(10 + 140 * q); paragrapg1.getChildObjects().add(picture); } } } //保存文檔 doc.saveToFile("output.docx", FileFormat.Docx_2013); doc.dispose(); } }
到此這篇關于Java在Word中添加多行圖片水印的文章就介紹到這了,更多相關Java添加圖片水印內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
JAVA中JSONObject對象和Map對象之間的相互轉換
這篇文章主要介紹了JAVA中JSONObject對象和Map對象之間的相互轉換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01