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(); | |
} | |
} |