*这么优秀的国产工具怎能错过呢!在线下单专享“一口价”,查看折扣价!想要获取更多福利的朋友可以咨询在线客服哦~
①只需设置首页背景和其他页面不同
设置纯色背景
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.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports System.DrawingNamespace DifferentBackground1Class ProgramPrivate 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.HeightDim shape As ShapeObject = firstpara.AppendShape(width, height, ShapeType.Rectangle)'添加形状shape.BehindText = True'设置形状衬于文字下方shape.HorizontalAlignment = ShapeHorizontalAlignment.Center'设置对齐方式,铺满页面shape.VerticalOrigin = VerticalOrigin.TopMarginAreashape.FillColor = Color.LightBlue'形状颜色'在其他页面的页眉中添加形状Dim otherheader As HeaderFooter = section.HeadersFooters.Headerotherheader.Paragraphs.Clear()Dim otherpara As Paragraph = otherheader.AddParagraph()Dim shape1 As ShapeObject = otherpara.AppendShape(width, height, ShapeType.Rectangle)shape1.BehindText = Trueshape1.HorizontalAlignment = ShapeHorizontalAlignment.Centershape1.VerticalOrigin = VerticalOrigin.TopMarginAreashape1.FillColor = Color.Pink'保存文档doc.SaveToFile("ColorBackground1.docx", FileFormat.Docx2013)System.Diagnostics.Process.Start("ColorBackground1.docx")End SubEnd ClassEnd Namespace

设置图片背景
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.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsNamespace DifferentBackground1Class ProgramPrivate 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 = TrueDim firstpageheader As HeaderFooter = section.HeadersFooters.FirstPageFooter'获取首页页眉firstpageheader.Paragraphs.Clear()'清除页眉默认的段落格式(因默认页眉格式中包含有一条横线)Dim firstpara As Paragraph = firstpageheader.AddParagraph()'重新添加段落'获取页面宽度、高度Dim width As Single = section.PageSetup.PageSize.WidthDim height As Single = section.PageSetup.PageSize.Height'添加图片到首页页眉Dim pic0 As DocPicture = firstpara.AppendPicture("1.png")pic0.TextWrappingStyle = TextWrappingStyle.Behindpic0.HorizontalAlignment = ShapeHorizontalAlignment.Centerpic0.VerticalOrigin = VerticalOrigin.TopMarginAreapic0.Width = widthpic0.Height = height'在其他页面的页眉中添加图片Dim otherheader As HeaderFooter = section.HeadersFooters.Headerotherheader.Paragraphs.Clear()Dim otherpara As Paragraph = otherheader.AddParagraph()Dim pic1 As DocPicture = otherpara.AppendPicture("2.png")pic1.TextWrappingStyle = TextWrappingStyle.Behindpic1.HorizontalAlignment = ShapeHorizontalAlignment.Centerpic1.VerticalOrigin = VerticalOrigin.TopMarginAreapic1.Width = widthpic1.Height = height'保存文档doc.SaveToFile("ImageBackground1.docx", FileFormat.Docx2013)System.Diagnostics.Process.Start("ImageBackground1.docx")End SubEnd ClassEnd Namespace

②设置指定多个页面背景不同
注意:给多个页面设置不同背景时,需要通过获取不同节的页眉来设置,本次程序环境中所用源文档已设置了多个节,
设置纯色背景
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.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsImports System.DrawingNamespace DifferentBackground2Class ProgramPrivate 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.WidthDim height As Single = section1.PageSetup.PageSize.Height'添加图片到页眉Dim header1 As HeaderFooter = section1.HeadersFooters.Headerheader1.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.TopMarginAreashape1.FillColor = Color.LightSalmon'形状颜色'同理设置第二节页眉中的图片Dim section2 As Section = doc.Sections(1)Dim header2 As HeaderFooter = section2.HeadersFooters.Headerheader2.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.TopMarginAreashape2.FillColor = Color.Moccasin'形状颜色'同理设置第三节中的页眉中的图片Dim section3 As Section = doc.Sections(2)Dim header3 As HeaderFooter = section3.HeadersFooters.Headerheader3.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.TopMarginAreashape3.FillColor = Color.LawnGreen'形状颜色'保存文档doc.SaveToFile("ColorBackground2.docx", FileFormat.Docx2013)System.Diagnostics.Process.Start("ColorBackground2.docx")End SubEnd ClassEnd Namespace

设置图片背景
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.DocImports Spire.Doc.DocumentsImports Spire.Doc.FieldsNamespace DifferentBackground2Class ProgramPrivate 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.Headerheader1.Paragraphs.Clear()Dim para1 As Paragraph = header1.AddParagraph()Dim pic1 As DocPicture = para1.AppendPicture("1.png")pic1.TextWrappingStyle = TextWrappingStyle.Behindpic1.HorizontalAlignment = ShapeHorizontalAlignment.Centerpic1.VerticalOrigin = VerticalOrigin.TopMarginAreaDim width As Single = section1.PageSetup.PageSize.WidthDim height As Single = section1.PageSetup.PageSize.Heightpic1.Width = widthpic1.Height = height'同理设置第二节页眉中的图片Dim section2 As Section = doc.Sections(1)Dim header2 As HeaderFooter = section2.HeadersFooters.Headerheader2.Paragraphs.Clear()Dim para2 As Paragraph = header2.AddParagraph()Dim pic2 As DocPicture = para2.AppendPicture("2.png")pic2.TextWrappingStyle = TextWrappingStyle.Behindpic2.HorizontalAlignment = ShapeHorizontalAlignment.Centerpic2.VerticalOrigin = VerticalOrigin.TopMarginAreapic2.Width = widthpic2.Height = height'同理设置第三节中的页眉中的图片Dim section3 As Section = doc.Sections(2)Dim header3 As HeaderFooter = section3.HeadersFooters.Headerheader3.Paragraphs.Clear()Dim para3 As Paragraph = header3.AddParagraph()Dim pic3 As DocPicture = para3.AppendPicture("3.png")pic3.TextWrappingStyle = TextWrappingStyle.Behindpic3.HorizontalAlignment = ShapeHorizontalAlignment.Centerpic3.VerticalOrigin = VerticalOrigin.TopMarginAreapic3.Width = widthpic3.Height = height'保存文档doc.SaveToFile("ImageBackground2.docx", FileFormat.Docx2013)System.Diagnostics.Process.Start("ImageBackground2.docx")End SubEnd ClassEnd Namespace

是E-iceblue官方友好合作伙伴,如果您对spire.Doc感兴趣,可以联系在线客服了解具体授权价格和使用机制。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!