1、在Visual Studio中打开“解决方案资源管理器”- 鼠标右键点击“引用”-“添加引用”:

2、点击“浏览”-“浏览”:

3、找到本地路径下的dll文件,点击“添加”到引用列表:

4、添加到列表后,点击“OK”,完成引用:

5、完成dll引用后,在vs程碌食撞搁序中编辑如下代码内容:using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;namespace ImageWatermark2{ class Program { static void Main(string[] args) { //加载Word测试文档 Document doc = new Document(); doc.LoadFromFile("test.docx"); //获取文档第一节 Section section1 = doc.Sections[0]; //定义水印图片的纵向坐标位置 float y = section1.PageSetup.PageSize.Height/3; //添加图片水印1 HeaderFooter header1 = section1.HeadersFooters.Header;//获取页眉 header1.Paragraphs.Clear();//删除原有页眉格式的段落 Paragraph para1 = header1.AddParagraph();//重新添加段落 DocPicture pic1 = para1.AppendPicture("logo1.png");//添加图片 pic1.TextWrappingStyle = TextWrappingStyle.Behind;//图片置于文字下方 pic1.VerticalPosition = y; pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center;//设置图片对齐方式 //同理设置第二节页眉中的图片水印2 Section section2 = doc.Sections[1]; HeaderFooter header2 = section2.HeadersFooters.Header; header2.Paragraphs.Clear(); Paragraph para2 = header2.AddParagraph(); DocPicture pic2 = para2.AppendPicture("logo2.png"); pic2.TextWrappingStyle = TextWrappingStyle.Behind; pic2.VerticalPosition = y; pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center; //同理设置第三节中的页眉中的图片水印3 Section section3 = doc.Sections[2]; HeaderFooter header3 = section3.HeadersFooters.Header; header3.Paragraphs.Clear(); Paragraph para3 = header3.AddParagraph(); DocPicture pic3 = para3.AppendPicture("logo3.png"); pic3.TextWrappingStyle = TextWrappingStyle.Behind; pic3.VerticalPosition = y; pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center; //保存文档 doc.SaveToFile("DifferentImageWatermark.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("DifferentImageWatermark.docx"); } }}
6、完成代码后,执行程序,生成Word文档。在结果文档中,可查看每页中的图片水印效果:
