手抄报 安全手抄报 手抄报内容 生活知识 生活百科 英语手抄报 清明节手抄报 节约用水手抄报 知识问答

C# 给Word每一页添加不同图片水印

时间:2026-04-02 19:30:05

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

C# 给Word每一页添加不同图片水印

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

C# 给Word每一页添加不同图片水印

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

C# 给Word每一页添加不同图片水印

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

C# 给Word每一页添加不同图片水印

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文档。在结果文档中,可查看每页中的图片水印效果:

C# 给Word每一页添加不同图片水印

© 2026 手抄报圈
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com