?Word中設(shè)置水印時(shí),可加載圖片設(shè)置為水印效果关噪,但通常添加水印效果時(shí),會(huì)對(duì)所有頁(yè)面都設(shè)置成統(tǒng)一效果乌妙,如果需要對(duì)每一頁(yè)或者某個(gè)頁(yè)面設(shè)置不同的水印效果使兔,則可以參考本文中的方法。下面藤韵,將以Java代碼為例虐沥,對(duì)Word每一頁(yè)設(shè)置不同的圖片水印效果作詳細(xì)介紹。
方法思路
在給Word每一頁(yè)添加水印前泽艘,首先需要在Word文檔每一頁(yè)正文的最后一個(gè)字符后面插入“連續(xù)”分節(jié)符欲险,然后在每一節(jié)的頁(yè)眉段落里添加水印圖片,并設(shè)置圖片的坐標(biāo)位置匹涮、對(duì)齊方式天试、襯與文字下方等。最后保存文檔然低。
Jar引入
在程序中引入?Free Spire.Doc for Java?中的Spire.Doc.jar文件(該文件在lib文件夾下)喜每;如果需要通過(guò)?Maven下載導(dǎo)入务唐,
配置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代碼
給每頁(yè)添加圖片水印時(shí),可參考如下步驟:
創(chuàng)建Document類(lèi)的對(duì)象灼卢,并通過(guò)Document.loadFromFile(String fileName)方法加載Word文檔绍哎。
通過(guò)Document.getSections().get(int index)方法獲取指定節(jié)来农。
通過(guò)Section.getHeadersFooters().getHeader()方法獲取頁(yè)眉鞋真,HeaderFooter.addParagraph()方法添加段落到頁(yè)眉。
通過(guò)Paragraph.appendPicture(String filePath)方法添加圖片到段落沃于,DocPicture.setVerticalPosition(float value)方法設(shè)置水印圖片位置涩咖,DocPicture.setHorizontalAlignment(ShapeHorizontalAlignment value)方法設(shè)置圖片對(duì)齊方式。
最后繁莹,通過(guò)Document.saveToFile(String fileName, FileFormat fileFormat)方法保存文檔檩互。
不同頁(yè)面中設(shè)置不一樣的圖片水印效果,只需要獲取該頁(yè)面對(duì)應(yīng)的節(jié)咨演,然后參考上述用到的方法來(lái)添加即可闸昨。
下面是完整的Java代碼示例:
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;
/**
* 說(shuō)明:word 添加水印
* 作者:FH Admin
* from fhadmin.cn
*/
public class DifferentImageWatermark {
? ? public static void main(String[] args) {
? ? ? ? //加載Word測(cè)試文檔
? ? ? ? Document doc = new Document();
? ? ? ? doc.loadFromFile("test.docx");
? ? ? ? //獲取文檔第一節(jié)
? ? ? ? Section section1 = doc.getSections().get(0);
? ? ? ? //定義水印圖片的縱向坐標(biāo)位置
? ? ? ? float y = (float) (section1.getPageSetup().getPageSize().getHeight()/3);
? ? ? ? //添加圖片水印1
? ? ? ? HeaderFooter header1 = section1.getHeadersFooters().getHeader();//獲取頁(yè)眉
? ? ? ? header1.getParagraphs().clear();//刪除原有頁(yè)眉格式的段落
? ? ? ? Paragraph para1= header1.addParagraph();//重新添加段落
? ? ? ? DocPicture pic1 = para1.appendPicture("logo1.png");//加載圖片
? ? ? ? pic1.setTextWrappingStyle(TextWrappingStyle.Behind);//圖片置于文字下方
? ? ? ? pic1.setVerticalPosition(y);
? ? ? ? pic1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);//設(shè)置圖片對(duì)齊方式
? ? ? ? //同理設(shè)置第二節(jié)頁(yè)眉中的圖片水印2
? ? ? ? Section section2 = doc.getSections().get(1);
? ? ? ? HeaderFooter header2 = section2.getHeadersFooters().getHeader();
? ? ? ? header2.getParagraphs().clear();
? ? ? ? Paragraph para2= header2.addParagraph();
? ? ? ? DocPicture pic2 = para2.appendPicture("logo2.png");
? ? ? ? pic2.setTextWrappingStyle(TextWrappingStyle.Behind);
? ? ? ? pic2.setVerticalPosition(y);
? ? ? ? pic2.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
? ? ? ? //同理設(shè)置第三節(jié)中的頁(yè)眉中的圖片水印3
? ? ? ? Section section3 = doc.getSections().get(2);
? ? ? ? HeaderFooter header3 = section3.getHeadersFooters().getHeader();
? ? ? ? header3.getParagraphs().clear();
? ? ? ? Paragraph para3= header3.addParagraph();
? ? ? ? DocPicture pic3 = para3.appendPicture("logo3.png");
? ? ? ? pic3.setTextWrappingStyle(TextWrappingStyle.Behind);
? ? ? ? pic3.setVerticalPosition(y);
? ? ? ? pic3.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
? ? ? ? //保存文檔
? ? ? ? doc.saveToFile("DifferentImageWatermark.docx",FileFormat.Docx_2013);
? ? ? ? doc.dispose();
? ? }
}