C#给Word不同页面设置不同背景
作者:E-iceblue 发布时间:2021-07-20 01:28:16
标签: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
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
来源:https://www.cnblogs.com/Yesi/p/14361897.html


猜你喜欢
- DataSource在数据库应用中,客户端与数据库服务端建立的连接对象(Connection)是宝贵的资源,每次请求数据库都创建连接,使用完
- 目录前言正文自定义NameSpaceHandler自定义schemaParserDecorator总结前言在早期基于Xml配置的Spring
- 前言继承是面向对象语法的三大特征之一。继承可以降低代码编写的冗余度,提高编程的效率。通过继承,子类获得了父类的成员变量和方法。一个子类如何继
- 前言Condition是在Spring4.0增加的条件判断功能,通过这个功能可以实现选择性的创建Bean对象。引入一个例子SpringBoo
- 多对多表之间关系表models.py文件代码from django.db import models# Create your models
- 本文实例讲述了Android编程之手机壁纸WallPaper设置方法。分享给大家供大家参考,具体如下:/** * Andorid设置手机屏幕
- 本文为大家分享了一个满足在线网页交流需求的实例,由于java Socket实现的网页版在线聊天功能,供大家参考,具体内容如下实现步骤:1、使
- 我们知道,Object类是所有类的父类,因此也被称为根类、祖先。那么,我们就来看一看Object类的最常用的两个方法是如何用的。1.toSt
- 一、广播机制概述通常情况下在学校的每个教室都会装有一个喇叭,这些喇叭是接入到学校广播室的。如果有重要通知,会发送一条广播来告知全校师生。为了
- * 是许多框架底层实现的基础,比如Spirng的AOP等,其实弄清楚了 * 的实现原理,它就没那么神奇了,下面就来通过案例和分析JDK
- 本文记录一下,我从AndroidStudio 2.3.3升级到3.0,再升级到3.0.1一路上遇到的输入法之坑以及解决方案。前些天把Andr
- 在网上看到一些人写关于条形码的代码都很长,有的甚至拿来卖,所以查了下资料,希望能对大家有帮助。我的实现原理是:其实Windows本身就有一个
- spring FactoryBean 是创建 复杂的bean,一般的bean 直接用xml配置即可,如果一个bean的创建过程中
- 由于是多态对象,基类类型的变量可以保存派生类型。 要访问派生类型的实例成员,必须将值强制转换 * 生类型。 但是,强制转换会引发 Invali
- 前言本文主要给大家介绍了如何更改Dialog的标题与按钮颜色的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。an
- 冒泡排序原理①比较相邻的元素,如果前一个元素比后一个元素大,则交换这两个元素的位置②对每一对相邻的元素循环上面的步骤,最终最后面的元素就是最
- 本文实例实现文件上传的进度显示,我们先看看都有哪些问题我们要解决。1 上传数据的处理进度跟踪2 进度数据在用户页面的显示就这么2个问题,第一
- Android 中View.onDraw(Canvas canvas)的使用方法View通过View.onDraw(Canvas canva
- 1、题目描述找出数组中重复的数字。在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知
- 从 <<Windows Forms 2.0 Programming, 2nd Edition>> &nbs