C#给Word不同页面设置不同背景
更新时间:2021年2月10日 17:29 点击:2264
给Word文档设置背景时,通常只能针对整篇文档设置统一的背景,如果需要对某些页面单独设置背景,则需要通过另外的方式来实现。本文通过C# 程序代码演示如何来实现。并附VB.NET代码作参考。
思路:通过在页眉中添加形状或者图片,并将形状或图片平铺(即设置形状或图片大小为页面大小)到整个页面。添加背景时,通过添加形状并设置形状颜色来设置成纯色背景效果;通过添加图片来实现图片背景效果。
本次程序运行环境中包括:引入Spire.Doc.dll;.Net Framework 4.5.1
设置不同背景时,分以下2种情况:
1. 只需设置首页背景和其他页面不同
1.1 设置纯色背景
【C#】
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace DifferentBackground1 { class Program { static void Main(string[] args) { //加载Word测试文档 Document doc = new Document(); doc.LoadFromFile("测试.docx"); //获取第一节 Section section = doc.Sections[0]; //设置首页页眉页脚不同 section.PageSetup.DifferentFirstPageHeaderFooter = true; //在首页页眉添加形状 HeaderFooter firstpageheader = section.HeadersFooters.FirstPageFooter;//获取首页页眉 firstpageheader.Paragraphs.Clear();//清除页眉默认的段落格式(因默认页眉格式中包含有一条横线) Paragraph firstpara = firstpageheader.AddParagraph();//重新添加段落 float width = section.PageSetup.PageSize.Width;//获取页面宽度、高度 float height = section.PageSetup.PageSize.Height; ShapeObject shape = firstpara.AppendShape(width, height, ShapeType.Rectangle);//添加形状 shape.BehindText = true;//设置形状衬于文字下方 shape.HorizontalAlignment = ShapeHorizontalAlignment.Center;//设置对齐方式,铺满页面 shape.VerticalOrigin = VerticalOrigin.TopMarginArea; shape.FillColor = Color.LightBlue;//形状颜色 //在其他页面的页眉中添加形状 HeaderFooter otherheader = section.HeadersFooters.Header; otherheader.Paragraphs.Clear(); Paragraph otherpara = otherheader.AddParagraph(); ShapeObject shape1 = otherpara.AppendShape(width, height, ShapeType.Rectangle); shape1.BehindText = true; shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center; shape1.VerticalOrigin = VerticalOrigin.TopMarginArea; shape1.FillColor = Color.Pink; //保存文档 doc.SaveToFile("ColorBackground1.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("ColorBackground1.docx"); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace DifferentBackground1 Class Program Private Shared Sub Main(args As String()) '加载Word测试文档 Dim doc As New Document() doc.LoadFromFile("测试.docx") '获取第一节 Dim section As Section = doc.Sections(0) '设置首页页眉页脚不同 section.PageSetup.DifferentFirstPageHeaderFooter = True '在首页页眉添加形状 Dim firstpageheader As HeaderFooter = section.HeadersFooters.FirstPageFooter '获取首页页眉 firstpageheader.Paragraphs.Clear() '清除页眉默认的段落格式(因默认页眉格式中包含有一条横线) Dim firstpara As Paragraph = firstpageheader.AddParagraph() '重新添加段落 Dim width As Single = section.PageSetup.PageSize.Width '获取页面宽度、高度 Dim height As Single = section.PageSetup.PageSize.Height Dim shape As ShapeObject = firstpara.AppendShape(width, height, ShapeType.Rectangle) '添加形状 shape.BehindText = True '设置形状衬于文字下方 shape.HorizontalAlignment = ShapeHorizontalAlignment.Center '设置对齐方式,铺满页面 shape.VerticalOrigin = VerticalOrigin.TopMarginArea shape.FillColor = Color.LightBlue '形状颜色 '在其他页面的页眉中添加形状 Dim otherheader As HeaderFooter = section.HeadersFooters.Header otherheader.Paragraphs.Clear() Dim otherpara As Paragraph = otherheader.AddParagraph() Dim shape1 As ShapeObject = otherpara.AppendShape(width, height, ShapeType.Rectangle) shape1.BehindText = True shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center shape1.VerticalOrigin = VerticalOrigin.TopMarginArea shape1.FillColor = Color.Pink '保存文档 doc.SaveToFile("ColorBackground1.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("ColorBackground1.docx") End Sub End Class End Namespace
1.2 设置图片背景
【C#】
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace DifferentBackground1 { class Program { static void Main(string[] args) { //加载Word测试文档 Document doc = new Document(); doc.LoadFromFile("测试.docx"); //获取第一节 Section section = doc.Sections[0]; //设置首页页眉页脚不同 section.PageSetup.DifferentFirstPageHeaderFooter = true; HeaderFooter firstpageheader = section.HeadersFooters.FirstPageFooter;//获取首页页眉 firstpageheader.Paragraphs.Clear();//清除页眉默认的段落格式(因默认页眉格式中包含有一条横线) Paragraph firstpara = firstpageheader.AddParagraph();//重新添加段落 //获取页面宽度、高度 float width = section.PageSetup.PageSize.Width; float height = section.PageSetup.PageSize.Height; //添加图片到首页页眉 DocPicture pic0 = firstpara.AppendPicture("1.png"); pic0.TextWrappingStyle = TextWrappingStyle.Behind; pic0.HorizontalAlignment = ShapeHorizontalAlignment.Center; pic0.VerticalOrigin = VerticalOrigin.TopMarginArea; pic0.Width = width; pic0.Height = height; //在其他页面的页眉中添加图片 HeaderFooter otherheader = section.HeadersFooters.Header; otherheader.Paragraphs.Clear(); Paragraph otherpara = otherheader.AddParagraph(); DocPicture pic1 = otherpara.AppendPicture("2.png"); pic1.TextWrappingStyle = TextWrappingStyle.Behind; pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center; pic1.VerticalOrigin = VerticalOrigin.TopMarginArea; pic1.Width = width; pic1.Height = height; //保存文档 doc.SaveToFile("ImageBackground1.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("ImageBackground1.docx"); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace DifferentBackground1 Class Program Private Shared Sub Main(args As String()) '加载Word测试文档 Dim doc As New Document() doc.LoadFromFile("测试.docx") '获取第一节 Dim section As Section = doc.Sections(0) '设置首页页眉页脚不同 section.PageSetup.DifferentFirstPageHeaderFooter = True Dim firstpageheader As HeaderFooter = section.HeadersFooters.FirstPageFooter '获取首页页眉 firstpageheader.Paragraphs.Clear() '清除页眉默认的段落格式(因默认页眉格式中包含有一条横线) Dim firstpara As Paragraph = firstpageheader.AddParagraph() '重新添加段落 '获取页面宽度、高度 Dim width As Single = section.PageSetup.PageSize.Width Dim height As Single = section.PageSetup.PageSize.Height '添加图片到首页页眉 Dim pic0 As DocPicture = firstpara.AppendPicture("1.png") pic0.TextWrappingStyle = TextWrappingStyle.Behind pic0.HorizontalAlignment = ShapeHorizontalAlignment.Center pic0.VerticalOrigin = VerticalOrigin.TopMarginArea pic0.Width = width pic0.Height = height '在其他页面的页眉中添加图片 Dim otherheader As HeaderFooter = section.HeadersFooters.Header otherheader.Paragraphs.Clear() Dim otherpara As Paragraph = otherheader.AddParagraph() Dim pic1 As DocPicture = otherpara.AppendPicture("2.png") pic1.TextWrappingStyle = TextWrappingStyle.Behind pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center pic1.VerticalOrigin = VerticalOrigin.TopMarginArea pic1.Width = width pic1.Height = height '保存文档 doc.SaveToFile("ImageBackground1.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("ImageBackground1.docx") End Sub End Class End Namespace
2. 设置指定多个页面背景不同
注:给多个页面设置不同背景时,需要通过获取不同节的页眉来设置,本次程序环境中所用源文档已设置了多个节,如需手动设置分节,可参考代码:
document.Sections[0].Paragraphs[0].InsertSectionBreak(SectionBreakType.NoBreak);
2.1 设置纯色背景
【C#】
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.Drawing; namespace DifferentBackground2 { class Program { static void Main(string[] args) { //加载Word文档 Document doc = new Document(); doc.LoadFromFile("测试.docx"); //获取第一节 Section section1 = doc.Sections[0]; //获取页面宽度、高度 float width = section1.PageSetup.PageSize.Width; float height = section1.PageSetup.PageSize.Height; //添加图片到页眉 HeaderFooter header1 = section1.HeadersFooters.Header; header1.Paragraphs.Clear(); Paragraph para1 = header1.AddParagraph(); ShapeObject shape1 = para1.AppendShape(width, height, ShapeType.Rectangle);//添加形状 shape1.BehindText = true;//设置形状衬于文字下方 shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center;//设置对齐方式,铺满页面 shape1.VerticalOrigin = VerticalOrigin.TopMarginArea; shape1.FillColor = Color.LightSalmon;//形状颜色 //同理设置第二节页眉中的形状 Section section2 = doc.Sections[1]; HeaderFooter header2 = section2.HeadersFooters.Header; header2.Paragraphs.Clear(); Paragraph para2 = header2.AddParagraph(); ShapeObject shape2 = para2.AppendShape(width, height, ShapeType.Rectangle);//添加形状 shape2.BehindText = true;//设置形状衬于文字下方 shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center;//设置对齐方式,铺满页面 shape2.VerticalOrigin = VerticalOrigin.TopMarginArea; shape2.FillColor = Color.Moccasin;//形状颜色 //同理设置第三节中的页眉中的形状 Section section3 = doc.Sections[2]; HeaderFooter header3 = section3.HeadersFooters.Header; header3.Paragraphs.Clear(); Paragraph para3 = header3.AddParagraph(); ShapeObject shape3 = para3.AppendShape(width, height, ShapeType.Rectangle);//添加形状 shape3.BehindText = true;//设置形状衬于文字下方 shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center;//设置对齐方式,铺满页面 shape3.VerticalOrigin = VerticalOrigin.TopMarginArea; shape3.FillColor = Color.LawnGreen;//形状颜色 //保存文档 doc.SaveToFile("ColorBackground2.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("ColorBackground2.docx"); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Imports System.Drawing Namespace DifferentBackground2 Class Program Private Shared Sub Main(args As String()) '加载Word文档 Dim doc As New Document() doc.LoadFromFile("测试.docx") '获取第一节 Dim section1 As Section = doc.Sections(0) '获取页面宽度、高度 Dim width As Single = section1.PageSetup.PageSize.Width Dim height As Single = section1.PageSetup.PageSize.Height '添加图片到页眉 Dim header1 As HeaderFooter = section1.HeadersFooters.Header header1.Paragraphs.Clear() Dim para1 As Paragraph = header1.AddParagraph() Dim shape1 As ShapeObject = para1.AppendShape(width, height, ShapeType.Rectangle) '添加形状 shape1.BehindText = True '设置形状衬于文字下方 shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center '设置对齐方式,铺满页面 shape1.VerticalOrigin = VerticalOrigin.TopMarginArea shape1.FillColor = Color.LightSalmon '形状颜色 '同理设置第二节页眉中的图片 Dim section2 As Section = doc.Sections(1) Dim header2 As HeaderFooter = section2.HeadersFooters.Header header2.Paragraphs.Clear() Dim para2 As Paragraph = header2.AddParagraph() Dim shape2 As ShapeObject = para2.AppendShape(width, height, ShapeType.Rectangle) '添加形状 shape2.BehindText = True '设置形状衬于文字下方 shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center '设置对齐方式,铺满页面 shape2.VerticalOrigin = VerticalOrigin.TopMarginArea shape2.FillColor = Color.Moccasin '形状颜色 '同理设置第三节中的页眉中的图片 Dim section3 As Section = doc.Sections(2) Dim header3 As HeaderFooter = section3.HeadersFooters.Header header3.Paragraphs.Clear() Dim para3 As Paragraph = header3.AddParagraph() Dim shape3 As ShapeObject = para3.AppendShape(width, height, ShapeType.Rectangle) '添加形状 shape3.BehindText = True '设置形状衬于文字下方 shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center '设置对齐方式,铺满页面 shape3.VerticalOrigin = VerticalOrigin.TopMarginArea shape3.FillColor = Color.LawnGreen '形状颜色 '保存文档 doc.SaveToFile("ColorBackground2.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("ColorBackground2.docx") End Sub End Class End Namespace
2.2 设置图片背景
【C#】
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace DifferentBackground2 { class Program { static void Main(string[] args) { //加载Word文档 Document doc = new Document(); doc.LoadFromFile("测试.docx"); //获取第一节 Section section1 = doc.Sections[0]; //添加图片到页眉 HeaderFooter header1 = section1.HeadersFooters.Header; header1.Paragraphs.Clear(); Paragraph para1 = header1.AddParagraph(); DocPicture pic1 = para1.AppendPicture("1.png"); pic1.TextWrappingStyle = TextWrappingStyle.Behind; pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center; pic1.VerticalOrigin = VerticalOrigin.TopMarginArea; float width = section1.PageSetup.PageSize.Width; float height = section1.PageSetup.PageSize.Height; pic1.Width = width; pic1.Height = height; //同理设置第二节页眉中的图片 Section section2 = doc.Sections[1]; HeaderFooter header2 = section2.HeadersFooters.Header; header2.Paragraphs.Clear(); Paragraph para2 = header2.AddParagraph(); DocPicture pic2 = para2.AppendPicture("2.png"); pic2.TextWrappingStyle = TextWrappingStyle.Behind; pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center; pic2.VerticalOrigin = VerticalOrigin.TopMarginArea; pic2.Width = width; pic2.Height = height; //同理设置第三节中的页眉中的图片 Section section3 = doc.Sections[2]; HeaderFooter header3 = section3.HeadersFooters.Header; header3.Paragraphs.Clear(); Paragraph para3 = header3.AddParagraph(); DocPicture pic3 = para3.AppendPicture("3.png"); pic3.TextWrappingStyle = TextWrappingStyle.Behind; pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center; pic3.VerticalOrigin = VerticalOrigin.TopMarginArea; pic3.Width = width; pic3.Height = height; //保存文档 doc.SaveToFile("ImageBackground2.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("ImageBackground2.docx"); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace DifferentBackground2 Class Program Private Shared Sub Main(args As String()) '加载Word文档 Dim doc As New Document() doc.LoadFromFile("测试.docx") '获取第一节 Dim section1 As Section = doc.Sections(0) '添加图片到页眉 Dim header1 As HeaderFooter = section1.HeadersFooters.Header header1.Paragraphs.Clear() Dim para1 As Paragraph = header1.AddParagraph() Dim pic1 As DocPicture = para1.AppendPicture("1.png") pic1.TextWrappingStyle = TextWrappingStyle.Behind pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center pic1.VerticalOrigin = VerticalOrigin.TopMarginArea Dim width As Single = section1.PageSetup.PageSize.Width Dim height As Single = section1.PageSetup.PageSize.Height pic1.Width = width pic1.Height = height '同理设置第二节页眉中的图片 Dim section2 As Section = doc.Sections(1) Dim header2 As HeaderFooter = section2.HeadersFooters.Header header2.Paragraphs.Clear() Dim para2 As Paragraph = header2.AddParagraph() Dim pic2 As DocPicture = para2.AppendPicture("2.png") pic2.TextWrappingStyle = TextWrappingStyle.Behind pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center pic2.VerticalOrigin = VerticalOrigin.TopMarginArea pic2.Width = width pic2.Height = height '同理设置第三节中的页眉中的图片 Dim section3 As Section = doc.Sections(2) Dim header3 As HeaderFooter = section3.HeadersFooters.Header header3.Paragraphs.Clear() Dim para3 As Paragraph = header3.AddParagraph() Dim pic3 As DocPicture = para3.AppendPicture("3.png") pic3.TextWrappingStyle = TextWrappingStyle.Behind pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center pic3.VerticalOrigin = VerticalOrigin.TopMarginArea pic3.Width = width pic3.Height = height '保存文档 doc.SaveToFile("ImageBackground2.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("ImageBackground2.docx") End Sub End Class End Namespace
到此这篇关于C#给Word不同页面设置不同背景的文章就介绍到这了,更多相关C#给Word设置背景内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!
上一篇: C#数据结构之最小堆的实现方法
下一篇: C#中word导出功能的骚操作详解
相关文章
- 我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了C#中list用法,结合实例形式分析了C#中list排序、运算、转换等常见操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25