给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不同页面设置不同背景的更多相关文章

  1. Java 给Word不同页面设置不同背景

    Word文档中,可直接通过[设计]-[页面颜色]页面颜色,通过Java代码可参考如下设置方法: 1. 设置单一颜色背景 doc.getBackground().setType(BackgroundTy ...

  2. C# 获取Word文本高亮和背景(附vb.net代码)

    Word中的文本高亮和背景是通过不同方法来设置的.文本高亮(Text Highlight Color)是通过[字体]中的快速工具栏设置:文本背景(Text Background/Shading)是通过 ...

  3. Office WORD如何设置表格背景颜色

    1 点击表格-表格属性,边框和底纹. 2 选择应用于段落,这样只会应用于问题,效果不好. 3 选择应用于单元格可以避免上面的问题.

  4. Vue系列:为不同页面设置body背景颜色

    由于SPA页面的特性,传统的设置 body 背景色的方法并不通用. 解决方案:利用组件内的路由实现 代码参考如下

  5. C# 设置Word文档背景(纯色/渐变/图片背景)

    Word是我们日常生活.学习和工作中必不可少的文档处理工具.精致美观的文档能给人带来阅读时视觉上的美感.在本篇文章中,将介绍如何使用组件Free Spire.Doc for .NET(社区版)给Wor ...

  6. Word+PS制作拼音表格

    这几天,朋友让帮忙做个拼音表格,使用Word可以直接标注音标,却无法实现小时候那种4线3格,Word的模板只有练习书法的.使用Excel却,无法将拼音标注单独标注到上一单元格(有朋友会VBA的话,帮我 ...

  7. 怎么用MathType解决Word公式排版很乱的问题

    现在办公室起草文件,期刊论文投稿.学校试着编辑都要先在Word中编辑好后再打印出来.在Word中编辑这些文本内容时,如果遇到公式就要使用专门的MathType公式编辑器.而有很多人在用MathType ...

  8. java使用freemark生成word/pdf

    目录 一. 背景 二.实现的技术选型以及遇到的坑 三.最终的效果 2.1 .doc word效果展示 2.1 .docx word效果展示 2.2 docx word转pdf效果展示 三.准备工作及代 ...

  9. 多界面开发 、 导航控制器(NavigationController)

    1 VC之间的跳转和正向传值 1.1 问题 在实际的开发中更多的应用都会有多个页面组成,每个页面展示不同的信息,页面之间的跳转是由视图控制器来实现的,本案例实现两个页面的之间的跳转和页面之间的正向传值 ...

随机推荐

  1. ProxyDroid+wifi设置抓xx点评抓不到的包

    ProxyDroid+wifi设置抓xx点评抓不到的包 一.ProxyDroid介绍 1.下载地址 下载并安装ProxyDroid.(确保您的设备已经ROOT)点击下载 2.界面介绍 (1) Auto ...

  2. C# 使用 log4net 日志组件

    一. 什么是 log4net  Apache log4net 库是帮助程序员将日志语句输出到各种输出目标的工具,它是从Java中的Log4j迁移过来的一个.Net版的开源日志框架.log4net 的一 ...

  3. 为什么import React from 'react',React首字母必须大写?

    很奇怪的是,明明没有用到 React,但是我不得不 import React.这是为什么? import React from 'react'; export default function (pr ...

  4. TurtleBot3 Waffle (tx2版华夫)(7)底盘测试

    说明:opencr本身带有自测底盘功能,通过按opencr的sw1和sw2来自检底盘是否正确安装和运行: 7.1.前进测试 1)测试前,先把小车架空,轮子不要着地: 2)接好电源后,打开opencr的 ...

  5. 在onelogin中使用OpenId Connect Authentication Flow

    目录 简介 OpenId Connect和Authentication Flow简介 onelogin的配置工作 使用应用程序连接onelogin 程序中的关键步骤 总结 简介 onelogin是一个 ...

  6. Redis学习之路(一)Redis简介

    一.Redis简介 是以key-value形式存储,和传统的关系型数据库不一样,不一定遵循传统数据库的一些基本要求.(区别于MYSQL的二维表格的形式存储).和Memcache类似,但是很大程度上拟补 ...

  7. spark的thriftservr的高可用

    triftserver是基于jdbc的一个spark的服务,可以做web查询,多客户端访问,但是thriftserver没有高可用,服务挂掉后就无法在访问,所有使用注册到zk的方式来实现高可用 一.版 ...

  8. Linux下使用acme.sh申请和管理Let’s Encrypt证书

    关于Let's Encrypt 免费SSL证书 Let's Encrypt 作为一个公共且免费 SSL 的项目逐渐被广大用户传播和使用,是由 Mozilla.Cisco.Akamai.IdenTrus ...

  9. Promise入门到精通(初级篇)-附代码详细讲解

    Promise入门到精通(初级篇)-附代码详细讲解 ​     Promise,中文翻译为承诺,约定,契约,从字面意思来看,这应该是类似某种协议,规定了什么事件发生的条件和触发方法. ​     Pr ...

  10. 【JDBC核心】数据库连接池

    数据库连接池 传统模式 使用数据库的传统模式: 在主程序(servlet.beans等)中建立数据库连接: 进行 SQL 操作: 断开数据库连接. 这种模式存在的问题: JDBC 连接数据库的方式(四 ...