给Word文档设置背景时,通常只能针对整篇文档设置统一的背景,如果需要对某些页面单独设置背景,则需要通过另外的方式来实现。本文通过C# 程序代码演示如何来实现。并附VB.NET代码作参考。
思路:通过在页眉中添加形状或者图片,并将形状或图片平铺(即设置形状或图片大小为页面大小)到整个页面。添加背景时,通过添加形状并设置形状颜色来设置成纯色背景效果;通过添加图片来实现图片背景效果。
本次程序运行环境中包括:引入Spire.Doc.dll;.Net Framework 4.5.1
设置不同背景时,分以下2种情况:
1. 只需设置首页背景和其他页面不同
1.1 设置纯色背景
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"); } } }
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 设置图片背景
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"); } } }
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 设置纯色背景
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"); } } }
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 设置图片背景
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"); } } }
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#编程环境中,处理Microsoft Word文档是一项常见的任务,其中包括对文档进行打印和预览功能的实现。本压缩包中的资源提供了实现这一功能的具体代码示例,帮助开发者快速理解和应用。下面将详细介绍C#中如何处理...
首先,需要引用`Microsoft.Office.Interop.Word`库,然后创建Word应用程序实例,打开Word文档,设置适当的页面布局和打印选项,最后模拟打印到一个TIFF虚拟打印机。这种方法的优点是直接且灵活,但缺点是需要安装...
在本项目中,“c#datagridview导出Word排版软件”是一个基于C#的系统,它专注于将DataGridView控件中的数据高效地导出到Microsoft Word文档中,以实现成绩单式的排版效果。这个系统对于需要生成报告或者批量处理表格...
二、页面设置 在创建文档后,我们需要设置页面的大小和方向。我们可以使用CT_SectPr类来设置页面的大小和方向。 CT_SectPr m_SectPr = new CT_SectPr(); m_SectPr.pgSz.w = (ulong)16838; m_SectPr.pgSz.h = (ulong...
7. **页面设置**:你可以控制PDF页面的大小、方向和边距,以便适应不同尺寸的图片。PdfPage.Size属性可以设置为预定义的PaperSize,或者自定义大小。 8. **保存PDF文件**:完成所有操作后,使用PdfDocument.Save()...
- 创建一个新的PDF文档,你可以使用`Document`类来初始化一个PDF文档对象,并设置其页面大小和边距。 ```csharp using iTextSharp.text; using iTextSharp.text.pdf; Document document = new Document(PageSize....
9. 页面设置和选项: 除了纸张大小和方向外,还可以允许用户调整边距、选择打印机等。这些可以通过`PrintDocument`的`PageSettings`属性和`PrintDialog`类来实现。 10. 打印质量与性能: 根据需要,可以设置打印...
在C#中,操作Word进行打印通常涉及到使用.NET Framework中的`System.Drawing.Printing`命名空间,这包括`PrintDocument`、`PrintPreviewDialog`和`PageSetupDialog`等类。以下是一个C#操作Word打印的示例,我们将...
实例143 设置水晶报表中节的背景图片 实例144 打印窗体中的数据 实例145 打印商品入库单据 实例146 使用打印控件实现分页打印 实例147 动态绑定水晶报表 实例148 在水晶报表中使用公式字段 实例149 设计分组统计报表...
C#示例源码 C#示例 C#源码 C#示例源代码 C#源代码 C#源代码例子 C#例子 注意:本源代码共有20章节,分五部分上传,名称分别为:明日科技《C#示例源代码》(1-4)、明日科技《C#示例源代码》(1-4)、明日科技《C#...
第1章 认识C#及开发环境 1.1 C#概述 2 1.1.1 C#发展历程 2 1.1.2 C#语言编程环境 2 1.2 .NET Framework 2.0简介 2 1.2.1 什么是.NET Framework 2.0 2 1.2.2 .NET Framework 2.0特性 3 1.3 安装集成...
4. 可能还需要调整页面背景颜色,以使水印更明显。 保护Word文档则涉及到权限管理,限制文档的编辑、复制、打印等功能。在OpenXML中,可以通过设置文档的保护属性来实现。具体步骤包括: 1. 打开Word文档的设置...
在IT领域,处理文档是日常工作中不可或缺的一部分,而Spire.Doc是一款强大的.NET库,它允许开发者使用C#或VB.NET编程语言对Microsoft Word文档进行高效的操作。本篇将深入探讨如何利用Spire.Doc来实现Word文档的编辑...
实例054 利用API设置桌面背景 196 实例055 音乐风景桌面 198 实例056 定时关闭计算机 201 实例057 设置任务栏时间 205 实例058 CPU使用率 206 实例059 进程管理器 209 实例060 数字大小写转换 ...
实例054 利用API设置桌面背景 196 实例055 音乐风景桌面 198 实例056 定时关闭计算机 201 实例057 设置任务栏时间 205 实例058 CPU使用率 206 实例059 进程管理器 209 实例060 数字大小写...
102 <br>0162 如何实现C#中用键完成TAB的功能 102 <br>0163 如何限制文本框密码输入长度 102 <br>0164 数据输入为空提示 103 <br>0165 如何设置文本框光标到末尾 103 <br>0166 输入法调整...