spire.doc for .net支持给word文档设置背景,可设置单一颜色、渐变颜色以及图片背景。设置背景时,常见的方法是为整篇文章设置统一的背景,即每个页面的背景都是一样的,如这篇文章中的方法。本文,将介绍如何来给word中的不同页面设置不同背景。具体分以下情况来设置。
1、只需设置ag凯发旗舰厅首页背景和其他页面不同
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];
//设置ag凯发旗舰厅首页页眉页脚不同
section.pagesetup.differentfirstpageheaderfooter = true;
//在ag凯发旗舰厅首页页眉添加形状
headerfooter firstpageheader = section.headersfooters.firstpagefooter;//获取ag凯发旗舰厅首页页眉
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)
'设置ag凯发旗舰厅首页页眉页脚不同
section.pagesetup.differentfirstpageheaderfooter = true
'在ag凯发旗舰厅首页页眉添加形状
dim firstpageheader as headerfooter = section.headersfooters.firstpagefooter
'获取ag凯发旗舰厅首页页眉
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];
//设置ag凯发旗舰厅首页页眉页脚不同
section.pagesetup.differentfirstpageheaderfooter = true;
headerfooter firstpageheader = section.headersfooters.firstpagefooter;//获取ag凯发旗舰厅首页页眉
firstpageheader.paragraphs.clear();//清除页眉默认的段落格式(因默认页眉格式中包含有一条横线)
paragraph firstpara = firstpageheader.addparagraph();//重新添加段落
//获取页面宽度、高度
float width = section.pagesetup.pagesize.width;
float height = section.pagesetup.pagesize.height;
//添加图片到ag凯发旗舰厅首页页眉
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)
'设置ag凯发旗舰厅首页页眉页脚不同
section.pagesetup.differentfirstpageheaderfooter = true
dim firstpageheader as headerfooter = section.headersfooters.firstpagefooter
'获取ag凯发旗舰厅首页页眉
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
'添加图片到ag凯发旗舰厅首页页眉
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、设置指定多个页面背景不同
注意:给多个页面设置不同背景时,需要通过获取不同节的页眉来设置,本次程序环境中所用源文档已设置了多个节,如需手动设置分节,请参考这篇文章。
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