在spire.pdf命名空间下有一个pdfpagetemplateelement类,用于创建页眉\页脚,水印和印章模板。在该模板上创建好页眉页脚等信息后,即可应用到pdf文档,使现有的页面出现相应信息。
c#
static void main(string[] args)
{
//创建pdf文档
pdfdocument doc = new pdfdocument();
//指定页面大小
doc.pagesettings.size = pdfpagesize.a4;
//将页边距设置为0
doc.pagesettings.margins = new pdfmargins(0);
//创建pdfmargins对象,指定期望设置的页边距
pdfmargins margins = new pdfmargins(40, 60, 40, 60);
//在文档模板的顶部和底部应用页眉页脚模板
doc.template.top = createheadertemplate(doc, margins);
doc.template.bottom = createfootertemplate(doc, margins);
//在文档模板的左右部分应用空白模板
doc.template.left = new pdfpagetemplateelement(margins.left, doc.pagesettings.size.height);
doc.template.right = new pdfpagetemplateelement(margins.right, doc.pagesettings.size.height);
//在文档中添加两页并写入文字
pdfpagebase page1 = doc.pages.add();
pdfpagebase page2 = doc.pages.add();
pdftruetypefont font = new pdftruetypefont(new font("华文楷体", 15f, fontstyle.bold), true);
page1.canvas.drawstring("正文开始的地方...", font, pdfbrushes.black, 0, 0);
page2.canvas.drawstring("正文开始的地方...", font, pdfbrushes.black, 0, 0);
//保存文档
doc.savetofile("headerandfooter.pdf");
}
//创建页眉模板
static pdfpagetemplateelement createheadertemplate(pdfdocument doc, pdfmargins margins)
{
//获取页面大小
sizef pagesize = doc.pagesettings.size;
//创建pdfpagetemplateelement对象headerspace,即作页眉模板
pdfpagetemplateelement headerspace = new pdfpagetemplateelement(pagesize.width, margins.top);
headerspace.foreground = false;
//声明x,y两个float型变量
float x = margins.left;
float y = 0;
//在headerspace中绘制图片
pdfimage headerimage = pdfimage.fromfile(@"c:\logo.png");
float width = headerimage.width / 3;
float height = headerimage.height / 3;
headerspace.graphics.drawimage(headerimage, x, margins.top - height - 2, width, height);
//在headerspace中绘制线段
pdfpen pen = new pdfpen(pdfbrushes.gray, 0.5f);
headerspace.graphics.drawline(pen, x, y margins.top - 2, pagesize.width - x, y margins.top - 2);
//返回headerspace
return headerspace;
}
// 创建页脚模板
static pdfpagetemplateelement createfootertemplate(pdfdocument doc, pdfmargins margins)
{
//获取页面大小
sizef pagesize = doc.pagesettings.size;
//创建pdfpagetemplateelement对象footerspace,即页脚模板
pdfpagetemplateelement footerspace = new pdfpagetemplateelement(pagesize.width, margins.bottom);
footerspace.foreground = false;
//声明x,y两个float型变量
float x = margins.left;
float y = 0;
//在footerspace中绘制线段
pdfpen pen = new pdfpen(pdfbrushes.gray, 0.5f);
footerspace.graphics.drawline(pen, x, y, pagesize.width - x, y);
//在footerspace中绘制文字
y = y 5;
pdftruetypefont font = new pdftruetypefont(new font("黑体", 10f, fontstyle.bold), true);
pdfstringformat format = new pdfstringformat(pdftextalignment.left);
string footertext = "成都冰蓝科技有限公司\n电话:028-81705109\n网址:http://www.e-iceblue.cn";
footerspace.graphics.drawstring(footertext, font, pdfbrushes.black, x, y, format);
//在footerspace中绘制当前页码和总页码
pdfpagenumberfield number = new pdfpagenumberfield();
pdfpagecountfield count = new pdfpagecountfield();
pdfcompositefield compositefield = new pdfcompositefield(font, pdfbrushes.black, "第{0}页/共{1}页", number, count);
compositefield.stringformat = new pdfstringformat(pdftextalignment.right, pdfverticalalignment.top);
sizef size = font.measurestring(compositefield.text);
compositefield.bounds = new rectanglef(pagesize.width - x - size.width, y, size.width, size.height);
compositefield.draw(footerspace.graphics);
//返回footerspace
return footerspace;
}
vb.net
private shared sub main(args as string())
'创建pdf文档
dim doc as new pdfdocument()
'指定页面大小
doc.pagesettings.size = pdfpagesize.a4
'将页边距设置为0
doc.pagesettings.margins = new pdfmargins(0)
'创建pdfmargins对象,指定期望设置的页边距
dim margins as new pdfmargins(40, 60, 40, 60)
'在文档模板的顶部和底部应用页眉页脚模板
doc.template.top = createheadertemplate(doc, margins)
doc.template.bottom = createfootertemplate(doc, margins)
'在文档模板的左右部分应用空白模板
doc.template.left = new pdfpagetemplateelement(margins.left, doc.pagesettings.size.height)
doc.template.right = new pdfpagetemplateelement(margins.right, doc.pagesettings.size.height)
'在文档中添加两页并写入文字
dim page1 as pdfpagebase = doc.pages.add()
dim page2 as pdfpagebase = doc.pages.add()
dim font as new pdftruetypefont(new font("华文楷体", 15f, fontstyle.bold), true)
page1.canvas.drawstring("正文开始的地方...", font, pdfbrushes.black, 0, 0)
page2.canvas.drawstring("正文开始的地方...", font, pdfbrushes.black, 0, 0)
'保存文档
doc.savetofile("headerandfooter.pdf")
end sub
'创建页眉模板
private shared function createheadertemplate(doc as pdfdocument, margins as pdfmargins) as pdfpagetemplateelement
'获取页面大小
dim pagesize as sizef = doc.pagesettings.size
'创建pdfpagetemplateelement对象headerspace,即作页眉模板
dim headerspace as new pdfpagetemplateelement(pagesize.width, margins.top)
headerspace.foreground = false
'声明x,y两个float型变量
dim x as single = margins.left
dim y as single = 0
'在headerspace中绘制图片
dim headerimage as pdfimage = pdfimage.fromfile("c:\logo.png")
dim width as single = headerimage.width / 3
dim height as single = headerimage.height / 3
headerspace.graphics.drawimage(headerimage, x, margins.top - height - 2, width, height)
'在headerspace中绘制线段
dim pen as new pdfpen(pdfbrushes.gray, 0.5f)
headerspace.graphics.drawline(pen, x, y margins.top - 2, pagesize.width - x, y margins.top - 2)
'返回headerspace
return headerspace
end function
' 创建页脚模板
private shared function createfootertemplate(doc as pdfdocument, margins as pdfmargins) as pdfpagetemplateelement
'获取页面大小
dim pagesize as sizef = doc.pagesettings.size
'创建pdfpagetemplateelement对象footerspace,即页脚模板
dim footerspace as new pdfpagetemplateelement(pagesize.width, margins.bottom)
footerspace.foreground = false
'声明x,y两个float型变量
dim x as single = margins.left
dim y as single = 0
'在footerspace中绘制线段
dim pen as new pdfpen(pdfbrushes.gray, 0.5f)
footerspace.graphics.drawline(pen, x, y, pagesize.width - x, y)
'在footerspace中绘制文字
y = y 5
dim font as new pdftruetypefont(new font("黑体", 10f, fontstyle.bold), true)
dim format as new pdfstringformat(pdftextalignment.left)
dim footertext as [string] = "成都冰蓝科技有限公司" & vblf & "电话:028-81705109" & vblf & "网址:http://www.e-iceblue.cn"
footerspace.graphics.drawstring(footertext, font, pdfbrushes.black, x, y, format)
'在footerspace中绘制当前页码和总页码
dim number as new pdfpagenumberfield()
dim count as new pdfpagecountfield()
dim compositefield as new pdfcompositefield(font, pdfbrushes.black, "第{0}页/共{1}页", number, count)
compositefield.stringformat = new pdfstringformat(pdftextalignment.right, pdfverticalalignment.top)
dim size as sizef = font.measurestring(compositefield.text)
compositefield.bounds = new rectanglef(pagesize.width - x - size.width, y, size.width, size.height)
compositefield.draw(footerspace.graphics)
'返回footerspace
return footerspace
end function