Java實現(xiàn)為Word每一頁設(shè)置不同圖片水印的效果
Word中設(shè)置水印時,可加載圖片設(shè)置為水印效果,但通常添加水印效果時,會對所有頁面都設(shè)置成統(tǒng)一效果,如果需要對每一頁或者某個頁面設(shè)置不同的水印效果,則可以參考本文中的方法。下面,將以Java代碼為例,對Word每一頁設(shè)置不同的圖片水印效果作詳細(xì)介紹。
方法思路
在給Word每一頁添加水印前,首先需要在Word文檔每一頁正文的最后一個字符后面插入“連續(xù)”分節(jié)符,然后在每一節(jié)的頁眉段落里添加水印圖片,并設(shè)置圖片的坐標(biāo)位置、對齊方式、襯與文字下方等。最后保存文檔。
Jar引入
在程序中引入 Free Spire.Doc for Java 中的Spire.Doc.jar文件(該文件在lib文件夾下);如果需要通過 Maven下載導(dǎo)入,可進(jìn)行如下配置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()方法添加段落到頁眉。
- 通過Paragraph.appendPicture(String filePath)方法添加圖片到段落,DocPicture.setVerticalPosition(float value)方法設(shè)置水印圖片位置,DocPicture.setHorizontalAlignment(ShapeHorizontalAlignment value)方法設(shè)置圖片對齊方式。
- 最后,通過Document.saveToFile(String fileName, FileFormat fileFormat)方法保存文檔。
不同頁面中設(shè)置不一樣的圖片水印效果,只需要獲取該頁面對應(yīng)的節(jié),然后參考上述用到的方法來添加即可。
示例代碼
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;
public class DifferentImageWatermark {
public static void main(String[] args) {
//加載Word測試文檔
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();//獲取頁眉
header1.getParagraphs().clear();//刪除原有頁眉格式的段落
Paragraph para1= header1.addParagraph();//重新添加段落
DocPicture pic1 = para1.appendPicture("logo1.png");//加載圖片
pic1.setTextWrappingStyle(TextWrappingStyle.Behind);//圖片置于文字下方
pic1.setVerticalPosition(y);
pic1.setHorizontalAlignment(ShapeHorizontalAlignment.Center);//設(shè)置圖片對齊方式
//同理設(shè)置第二節(jié)頁眉中的圖片水印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é)中的頁眉中的圖片水印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();
}
}如圖,每一頁均可顯示不同的圖片水印效果:

補(bǔ)充
當(dāng)然Java還可以實現(xiàn)為Word添加文字或圖片水印,下面的實現(xiàn)代碼
1.文字水印
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;
public class Main {
public static void main(String[] args) {
//加載測試文檔
Document document = new Document();
document.loadFromFile("sample.docx");
//插入文本水印
InsertTextWatermark(document.getSections().get(0));
//保存文檔
document.saveToFile("textwatermark.docx",FileFormat.Docx );
}
//自定義方法指定文本水印字樣,并設(shè)置成水印
private static void InsertTextWatermark(Section section){
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.setText("內(nèi)部使用");
txtWatermark.setFontSize(40);
txtWatermark.setColor(Color.red);
txtWatermark.setLayout(WatermarkLayout.Diagonal);
section.getDocument().setWatermark(txtWatermark);
}
}2.圖片水印
import com.spire.doc.*;
public class Main {
public static void main(String[] args) {
//加載測試文檔
Document document = new Document();
document.loadFromFile("sample.docx");
//加載需要設(shè)置成水印的圖片
PictureWatermark picture = new PictureWatermark();
picture.setPicture("wx.png");
picture.setScaling(5);
picture.isWashout(false);
//將圖片設(shè)置成水印
document.setWatermark(picture);
//保存文檔
document.saveToFile("imagewatermark.docx",FileFormat.Docx );
}
}到此這篇關(guān)于Java實現(xiàn)為Word每一頁設(shè)置不同圖片水印的效果的文章就介紹到這了,更多相關(guān)Java Word水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java8與Runtime.getRuntime().availableProcessors()
這篇文章主要介紹了詳解Java8與Runtime.getRuntime().availableProcessors(),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java數(shù)據(jù)結(jié)構(gòu)之稀疏矩陣定義與用法示例
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之稀疏矩陣定義與用法,結(jié)合實例形式分析了java稀疏矩陣的定義、運(yùn)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
詳解Maven settings.xml配置(指定本地倉庫、阿里云鏡像設(shè)置)
這篇文章主要介紹了詳解Maven settings.xml配置(指定本地倉庫、阿里云鏡像設(shè)置),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
java Socket編程實現(xiàn)I/O多路復(fù)用的示例
本文主要介紹了java Socket編程實現(xiàn)I/O多路復(fù)用的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09
SpringBoot2 參數(shù)管理實踐之入?yún)⒊鰠⑴c校驗的方式
這篇文章主要介紹了SpringBoot2 參數(shù)管理實踐,入?yún)⒊鰠⑴c校驗,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-06-06

