C#怎么实现给Word每一页设置不同图片水印
本篇内容介绍了“C#怎么实现给Word每一页设置不同图片水印”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
方法思路在给Word每一页添加水印前,首先需要在Word文档每一页正文的最后一个字符后面插入“连续”分节符,然后在每一节的页眉段落里添加水印图片,并设置图片的坐标位置、对齐方式、衬于文字下方等。最后保存文档。
dll引入方法1在程序中引入Spire.Doc.dll文件;将Free Spire.Doc for .NET下载到本地,解压,找到BIN文件夹下的Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。
方法2通过NuGet安装。可通过以下2种方法安装:
1. 可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成。
2. 将以下内容复制到PM控制台安装。
Install-Package FreeSpire.Doc -Version 10.2.0
给每页添加图片水印时,可参考如下步骤:
创建Document类的对象,并通过LoadFromFile(string fileName)方法加载Word文档。
通过Document.Sections[]属性获取指定节。
通过HeadersFooters.Header属性获取页眉,HeaderFooter.AddParagraph()方法添加段落到页眉。
通过Paragraph.AppendPicture(string imgFile)方法添加图片到段落,DocPicture.VerticalPosition属性设置水印图片位置,DocPicture.HorizontalAlignment属性设置图片对齐方式。
最后,通过Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存文档。
不同页面中设置不一样的图片水印效果,只需要获取该页面对应的节,然后参考上述用到的方法来添加即可。
C#usingSpire.Doc;usingSpire.Doc.Documents;usingSpire.Doc.Fields;namespaceImageWatermark2{classProgram{staticvoidMain(string[]args){//加载Word测试文档Documentdoc=newDocument();doc.LoadFromFile("test.docx");//获取文档第一节Sectionsection1=doc.Sections[0];//定义水印图片的纵向坐标位置floaty=section1.PageSetup.PageSize.Height/3;//添加图片水印1HeaderFooterheader1=section1.HeadersFooters.Header;//获取页眉header1.Paragraphs.Clear();//删除原有页眉格式的段落Paragraphpara1=header1.AddParagraph();//重新添加段落DocPicturepic1=para1.AppendPicture("logo1.png");//添加图片pic1.TextWrappingStyle=TextWrappingStyle.Behind;//图片置于文字下方pic1.VerticalPosition=y;pic1.HorizontalAlignment=ShapeHorizontalAlignment.Center;//设置图片对齐方式//同理设置第二节页眉中的图片水印2Sectionsection2=doc.Sections[1];HeaderFooterheader2=section2.HeadersFooters.Header;header2.Paragraphs.Clear();Paragraphpara2=header2.AddParagraph();DocPicturepic2=para2.AppendPicture("logo2.png");pic2.TextWrappingStyle=TextWrappingStyle.Behind;pic2.VerticalPosition=y;pic2.HorizontalAlignment=ShapeHorizontalAlignment.Center;//同理设置第三节中的页眉中的图片水印3Sectionsection3=doc.Sections[2];HeaderFooterheader3=section3.HeadersFooters.Header;header3.Paragraphs.Clear();Paragraphpara3=header3.AddParagraph();DocPicturepic3=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");}}}vb.net
ImportsSpire.DocImportsSpire.Doc.DocumentsImportsSpire.Doc.FieldsNamespaceImageWatermark2ClassProgramPrivateSharedSubMain(argsAsString())'加载Word测试文档DimdocAsNewDocument()doc.LoadFromFile("test.docx")'获取文档第一节Dimsection1AsSection=doc.Sections(0)'定义水印图片的纵向坐标位置DimyAsSingle=section1.PageSetup.PageSize.Height/3'添加图片水印1Dimheader1AsHeaderFooter=section1.HeadersFooters.Header'获取页眉header1.Paragraphs.Clear()'删除原有页眉格式的段落Dimpara1AsParagraph=header1.AddParagraph()'重新添加段落Dimpic1AsDocPicture=para1.AppendPicture("logo1.png")'添加图片pic1.TextWrappingStyle=TextWrappingStyle.Behind'图片置于文字下方pic1.VerticalPosition=ypic1.HorizontalAlignment=ShapeHorizontalAlignment.Center'设置图片对齐方式'同理设置第二节页眉中的图片水印2Dimsection2AsSection=doc.Sections(1)Dimheader2AsHeaderFooter=section2.HeadersFooters.Headerheader2.Paragraphs.Clear()Dimpara2AsParagraph=header2.AddParagraph()Dimpic2AsDocPicture=para2.AppendPicture("logo2.png")pic2.TextWrappingStyle=TextWrappingStyle.Behindpic2.VerticalPosition=ypic2.HorizontalAlignment=ShapeHorizontalAlignment.Center'同理设置第三节中的页眉中的图片水印3Dimsection3AsSection=doc.Sections(2)Dimheader3AsHeaderFooter=section3.HeadersFooters.Headerheader3.Paragraphs.Clear()Dimpara3AsParagraph=header3.AddParagraph()Dimpic3AsDocPicture=para3.AppendPicture("logo3.png")pic3.TextWrappingStyle=TextWrappingStyle.Behindpic3.VerticalPosition=ypic3.HorizontalAlignment=ShapeHorizontalAlignment.Center'保存文档doc.SaveToFile("DifferentImageWatermark.docx",FileFormat.Docx2013)System.Diagnostics.Process.Start("DifferentImageWatermark.docx")EndSubEndClassEndNamespace
如图,每一页均可显示不同的图片水印效果:
“C#怎么实现给Word每一页设置不同图片水印”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。