C# 给Word不同页面设置不同背景
给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