Word中設定水印時,可加載圖片設定為水印效果,但通常添加水印效果時,會對所有頁面都設定成統一效果,如果需要對每一頁或者某個頁面設定不同的水印效果,則可以參考本文中的方法,下面,將以Java代碼為例,對Word每一頁設定不同的圖片水印效果作詳細介紹,
方法思路
在給Word每一頁添加水印前,首先需要在Word檔案每一頁正文的最后一個字符后面插入“連續”分節符,然后在每一節的頁眉段落里添加水印圖片,并設定圖片的坐標位置、對齊方式、襯與文字下方等,最后保存檔案,
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代碼
給每頁添加圖片水印時,可參考如下步驟:
- 創建Document類的物件,并通過Document.loadFromFile(String fileName)方法加載Word檔案,
- 通過Document.getSections().get(int index)方法獲取指定節,
- 通過Section.getHeadersFooters().getHeader()方法獲取頁眉,HeaderFooter.addParagraph()方法添加段落到頁眉,
- 通過Paragraph.appendPicture(String filePath)方法添加圖片到段落,DocPicture.setVerticalPosition(float value)方法設定水印圖片位置,DocPicture.setHorizontalAlignment(ShapeHorizontalAlignment value)方法設定圖片對齊方式,
- 最后,通過Document.saveToFile(String fileName, FileFormat fileFormat)方法保存檔案,
不同頁面中設定不一樣的圖片水印效果,只需要獲取該頁面對應的節,然后參考上述用到的方法來添加即可,
下面是完整的Java代碼示例:
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;
/**
* 說明:word 添加水印
* 作者:FH Admin
* from fhadmin.cn
*/
public class DifferentImageWatermark {
public static void main(String[] args) {
//加載Word測驗檔案
Document doc = new Document();
doc.loadFromFile("test.docx");
//獲取檔案第一節
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();//重新添加段落
DocPicture pic1 = para1.appendPicture("logo1.png");//加載圖片
pic1.setTextWrappingStyle(TextWrappingStyle.Behind);//圖片置于文字下方
pic1.setVerticalPosition(y);
pic1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);//設定圖片對齊方式
//同理設定第二節頁眉中的圖片水印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);
//同理設定第三節中的頁眉中的圖片水印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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/423673.html
標籤:架構設計
上一篇:Mysql資料庫 | 基于Docker搭建Mysql-8.0以上版本主從實體實戰
下一篇:戲說領域驅動設計(一)——開場
