tab 1
此 demo 展示如何设置 word 文档的页面大小、页眉和页脚,以及如何将图片设置为页眉/页脚的背景。
如果这不是您想要的 demo,您可以通过填写表格获取免费定制 demo。
如您有与我们产品相关的其他技术问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。;销售相关的问题,请联系 该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。。
tab 2
using system;
using system.drawing;
using spire.doc;
using spire.doc.documents;
using spire.doc.fields;
namespace demoonlinecode
{
class headerandfooter
{
public void demoheaderandfooter(string docfile,
string headerimagefile = null,
string footerimagefile = null,
string pagesizename = "a4",
string headerfontcolor = null,
string footerfontcolor = null)
{
document document = new document(docfile, fileformat.auto);
sizef pagesize = (sizef)typeof(pagesize).getfield(pagesizename).getvalue(null);
pagesetup(document, pagesize);
setheader(document, convertstrtocolor(headerfontcolor), headerimagefile);
setfooter(document,convertstrtocolor(footerfontcolor), footerimagefile);
document.savetofile("demo.doc", fileformat.doc);
}
private void pagesetup(document document, sizef pagesize)
{
for (int i = 0; i < document.sections.count; i )
{
section section = document.sections[i];
section.pagesetup.pagesize = pagesize;
section.pagesetup.margins.top = (float)72.0;
section.pagesetup.margins.bottom = (float)72.0;
section.pagesetup.margins.left = (float)89.0;
section.pagesetup.margins.right = (float)89.0;
}
}
private void setfooter(document document,
color fontcolor,
string footerimagefile = null,
string footertext = "spire.doc",
string fontname = "calibri",
float fontsize = (float)12.0,
horizontalalignment alignment = horizontalalignment.left)
{
for (int i = 0; i < document.sections.count; i )
{
section section = document.sections[i];
headerfooter footer = section.headersfooters.footer;
paragraph footerparagraph = footer.addparagraph();
textrange text = footerparagraph.appendtext(footertext);
text.characterformat.fontname = fontname;
text.characterformat.fontsize = fontsize;
text.characterformat.textcolor = fontcolor;
footerparagraph.format.horizontalalignment = alignment;
//border
footerparagraph.format.borders.top.bordertype
= (borderstyle)spire.doc.documents.borderstyle.hairline;
footerparagraph.format.borders.top.space = (float)0.03;
if (footerimagefile != null)
{
byte[] imagedata = system.io.file.readallbytes(footerimagefile);
//insert picture to footer
docpicture footerpicture = footerparagraph.appendpicture(imagedata);
//footer picture layout
footerpicture.textwrappingstyle = textwrappingstyle.behind;
footerpicture.horizontalorigin = horizontalorigin.page;
footerpicture.horizontalalignment = shapehorizontalalignment.left;
footerpicture.verticalorigin = verticalorigin.page;
footerpicture.verticalalignment = shapeverticalalignment.bottom;
}
//insert page number
footerparagraph = footer.addparagraph();
footerparagraph.appendfield("page number", fieldtype.fieldpage);
footerparagraph.appendtext("of");
footerparagraph.appendfield("number of pages", fieldtype.fieldnumpages);
footerparagraph.format.horizontalalignment
= (horizontalalignment)horizontalalignment.right;
}
}
private void setheader(document document,
color fontcolor,
string headerimagefile = null,
string headertext = "e-iceblue",
string fontname = "calibri",
float fontsize = (float)12.0,
horizontalalignment alignment = horizontalalignment.center)
{
for (int i = 0; i < document.sections.count; i )
{
section section = document.sections[i];
headerfooter header = section.headersfooters.header;
//insert picture and text to header
paragraph headerparagraph = header.addparagraph();
if (headerimagefile != null)
{
byte[] imagedata = system.io.file.readallbytes(headerimagefile);
docpicture headerpicture = headerparagraph.appendpicture(imagedata);
//header picture layout - text wrapping
headerpicture.textwrappingstyle = textwrappingstyle.behind;
//header picture layout - position
headerpicture.horizontalorigin = horizontalorigin.page;
headerpicture.horizontalalignment = shapehorizontalalignment.left;
headerpicture.verticalorigin = verticalorigin.page;
headerpicture.verticalalignment = shapeverticalalignment.top;
}
//header text
textrange text = headerparagraph.appendtext(headertext);
text.characterformat.fontname = fontname;
text.characterformat.fontsize = convert.tosingle(fontsize);
text.characterformat.textcolor = fontcolor;
headerparagraph.format.horizontalalignment = alignment;
//border
headerparagraph.format.borders.bottom.bordertype
= (spire.doc.documents.borderstyle)spire.doc.documents.borderstyle.hairline;
headerparagraph.format.borders.bottom.space = (float)0.03;
}
}
private color convertstrtocolor(string strcolor)
{
if (string.isnullorwhitespace(strcolor))
{
return color.empty;
}
else
{
return colortranslator.fromhtml("#" strcolor);
}
}
}
}
tab 3
imports system.drawing
imports spire.doc
imports spire.doc.documents
imports spire.doc.fields
namespace demoonlinecode
class headerandfooter
public sub demoheaderandfooter(docfile as [string],
optional headerimagefile as [string] = nothing,
optional footerimagefile as [string] = nothing,
optional pagesizename as string = "a4",
optional headerfontcolor as [string] = nothing,
optional footerfontcolor as [string] = nothing)
dim document as new document(docfile, fileformat.auto)
dim pagesize as sizef _
= ctype(gettype(pagesize).getfield(pagesizename).getvalue(nothing), sizef)
pagesetup(document, pagesize)
setheader(document, convertstrtocolor(headerfontcolor), headerimagefile)
setfooter(document, convertstrtocolor(footerfontcolor), footerimagefile)
document.savetofile("demo.doc", fileformat.doc)
end sub
private sub pagesetup(document as document, pagesize as sizef)
for i as integer = 0 to document.sections.count - 1
dim section as section = document.sections(i)
section.pagesetup.pagesize = pagesize
section.pagesetup.margins.top = csng(72.0)
section.pagesetup.margins.bottom = csng(72.0)
section.pagesetup.margins.left = csng(89.0)
section.pagesetup.margins.right = csng(89.0)
next
end sub
private sub setfooter(document as document,
fontcolor as color,
optional footerimagefile as string = nothing,
optional footertext as string = "spire.doc",
optional fontname as string = "calibri",
optional fontsize as single = csng(12.0), _
optional alignment as horizontalalignment = horizontalalignment.left)
for i as integer = 0 to document.sections.count - 1
dim section as section = document.sections(i)
dim footer as headerfooter = section.headersfooters.footer
dim footerparagraph as paragraph = footer.addparagraph()
dim text as textrange = footerparagraph.appendtext(footertext)
text.characterformat.fontname = fontname
text.characterformat.fontsize = fontsize
text.characterformat.textcolor = fontcolor
footerparagraph.format.horizontalalignment = alignment
'border
footerparagraph.format.borders.top.bordertype _
= directcast(spire.doc.documents.borderstyle.hairline, borderstyle)
footerparagraph.format.borders.top.space = csng(0.03)
if footerimagefile isnot nothing then
dim imagedata as [byte]() = system.io.file.readallbytes(footerimagefile)
'insert picture to footer
dim footerpicture as docpicture = footerparagraph.appendpicture(imagedata)
'footer picture layout
footerpicture.textwrappingstyle = textwrappingstyle.behind
footerpicture.horizontalorigin = horizontalorigin.page
footerpicture.horizontalalignment = shapehorizontalalignment.left
footerpicture.verticalorigin = verticalorigin.page
footerpicture.verticalalignment = shapeverticalalignment.bottom
end if
'insert page number
footerparagraph = footer.addparagraph()
footerparagraph.appendfield("page number", fieldtype.fieldpage)
footerparagraph.appendtext("of")
footerparagraph.appendfield("number of pages", fieldtype.fieldnumpages)
footerparagraph.format.horizontalalignment _
= directcast(horizontalalignment.right, horizontalalignment)
next
end sub
private sub setheader(document as document,
fontcolor as color,
optional headerimagefile as string = nothing,
optional headertext as string = "e-iceblue",
optional fontname as string = "calibri",
optional fontsize as single = csng(12.0), _
optional alignment as horizontalalignment = horizontalalignment.center)
for i as integer = 0 to document.sections.count - 1
dim section as section = document.sections(i)
dim header as headerfooter = section.headersfooters.header
'insert picture and text to header
dim headerparagraph as paragraph = header.addparagraph()
if headerimagefile isnot nothing then
dim imagedata as [byte]() = system.io.file.readallbytes(headerimagefile)
dim headerpicture as docpicture = headerparagraph.appendpicture(imagedata)
'header picture layout - text wrapping
headerpicture.textwrappingstyle = textwrappingstyle.behind
'header picture layout - position
headerpicture.horizontalorigin = horizontalorigin.page
headerpicture.horizontalalignment = shapehorizontalalignment.left
headerpicture.verticalorigin = verticalorigin.page
headerpicture.verticalalignment = shapeverticalalignment.top
end if
'header text
dim text as textrange = headerparagraph.appendtext(headertext)
text.characterformat.fontname = fontname
text.characterformat.fontsize = convert.tosingle(fontsize)
text.characterformat.textcolor = fontcolor
headerparagraph.format.horizontalalignment = alignment
'border
headerparagraph.format.borders.bottom.bordertype _
= directcast(borderstyle.hairline, spire.doc.documents.borderstyle)
headerparagraph.format.borders.bottom.space = csng(0.03)
next
end sub
private function convertstrtocolor(strcolor as string) as color
if [string].isnullorwhitespace(strcolor) then
return color.empty
else
return colortranslator.fromhtml("#" & strcolor)
end if
end function
end class
end namespace