本教程将展示如何在一个现成的pdf文档中添加页眉页脚。在现成的pdf页面中,坐标体系有以下设定:
- 坐标原点是页面的左上角定点
- x轴正半轴向右延伸,y轴正半轴向下延伸
在页面的空白部分添加图片、文字或横线时,需要自己把握现有文档的边距,防止页眉页脚的内容覆盖到正文。
方法一:直接在现有文档绘制页眉页脚
c#
static void main(string[] args)
{
//加载一个测试文档
pdfdocument existingpdf = new pdfdocument();
existingpdf.loadfromfile(@"c:\test.pdf");
//调用drawheader方法在现有文档添加页眉
drawheader(existingpdf);
//调用drawfooter方法在现有文档添加页脚
drawfooter(existingpdf);
//保存文档
existingpdf.savetofile("output.pdf");
}
//在页面上方空白部位绘制页眉
static void drawheader(pdfdocument doc)
{
//获取页面大小
sizef pagesize = doc.pages[0].size;
//声明x,y两个float型变量
float x = 90;
float y = 20;
for (int i = 0; i < doc.pages.count; i )
{
//在每一页的指定位置绘制图片
pdfimage headerimage = pdfimage.fromfile(@"c:\logo.png");
float width = headerimage.width / 3;
float height = headerimage.height / 3;
doc.pages[i].canvas.drawimage(headerimage, x, y, width, height);
//在每一页的指定位置绘制横线
pdfpen pen = new pdfpen(pdfbrushes.gray, 0.5f);
doc.pages[i].canvas.drawline(pen, x, y height 2, pagesize.width - x, y height 2);
}
}
//在页面下方空白部位绘制页脚
static void drawfooter(pdfdocument doc)
{
//获取页面大小
sizef pagesize = doc.pages[0].size;
//声明x,y两个float型变量
float x = 90;
float y = pagesize.height - 72;
for (int i = 0; i < doc.pages.count; i )
{
//在每一页的指定位置绘制横线
pdfpen pen = new pdfpen(pdfbrushes.gray, 0.5f);
doc.pages[i].canvas.drawline(pen, x, y, pagesize.width - x, y);
//在每一页的指定位置绘制文字
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";
doc.pages[i].canvas.drawstring(footertext, font, pdfbrushes.black, x, y, format);
//在每一页的指定位置当前页码和总页码
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(doc.pages[i].canvas);
}
}
vb.net
private shared sub main(args as string())
'加载一个测试文档
dim existingpdf as new pdfdocument()
existingpdf.loadfromfile("c:\test.pdf")
'调用drawheader方法在现有文档添加页眉
drawheader(existingpdf)
'调用drawfooter方法在现有文档添加页脚
drawfooter(existingpdf)
'保存文档
existingpdf.savetofile("output.pdf")
end sub
'在页面上方空白部位绘制页眉
private shared sub drawheader(doc as pdfdocument)
'获取页面大小
dim pagesize as sizef = doc.pages(0).size
'声明x,y两个float型变量
dim x as single = 90
dim y as single = 20
for i as integer = 0 to doc.pages.count - 1
'在每一页的指定位置绘制图片
dim headerimage as pdfimage = pdfimage.fromfile("c:\logo.png")
dim width as single = headerimage.width / 3
dim height as single = headerimage.height / 3
doc.pages(i).canvas.drawimage(headerimage, x, y, width, height)
'在每一页的指定位置绘制横线
dim pen as new pdfpen(pdfbrushes.gray, 0.5f)
doc.pages(i).canvas.drawline(pen, x, y height 2, pagesize.width - x, y height 2)
next
end sub
'在页面下方空白部位绘制页脚
private shared sub drawfooter(doc as pdfdocument)
'获取页面大小
dim pagesize as sizef = doc.pages(0).size
'声明x,y两个float型变量
dim x as single = 90
dim y as single = pagesize.height - 72
for i as integer = 0 to doc.pages.count - 1
'在每一页的指定位置绘制横线
dim pen as new pdfpen(pdfbrushes.gray, 0.5f)
doc.pages(i).canvas.drawline(pen, x, y, pagesize.width - x, y)
'在每一页的指定位置绘制文字
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"
doc.pages(i).canvas.drawstring(footertext, font, pdfbrushes.black, x, y, format)
'在每一页的指定位置当前页码和总页码
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(doc.pages(i).canvas)
next
end sub
方法二:通过模板功能添加页眉页脚
c#
static void main(string[] args)
{
//加载一个测试文档
pdfdocument existingpdf = new pdfdocument();
existingpdf.loadfromfile(@"c:\test.pdf");
//创建新的pdf文档
pdfdocument newpdf = new pdfdocument();
//设置页面大小
newpdf.pagesettings.size = existingpdf.pages[0].size;
//将页边距设置为0
newpdf.pagesettings.margins = new pdfmargins(0);
//创建pdfmargins对象,指定期望设置的页边距
//期望边距需小于或等于现有文档的实际边距,否则页眉页脚可能覆盖主体内容
pdfmargins margins = new pdfmargins(90, 72, 90, 72);
//在新建文档的顶部和底部应用页眉页脚模板
newpdf.template.top = createheadertemplate(newpdf, margins);
newpdf.template.bottom = createfootertemplate(newpdf, margins);
//在新建文档的左右部分应用空白模板
newpdf.template.left = new pdfpagetemplateelement(margins.left, newpdf.pagesettings.size.height);
newpdf.template.right = new pdfpagetemplateelement(margins.right, newpdf.pagesettings.size.height);
for (int i = 0; i < existingpdf.pages.count; i )
{
//添加页面到新建文档,并将现有文档当做模板绘制到新页面
newpdf.pages.add().canvas.drawtemplate(existingpdf.pages[i].createtemplate(), new pointf(-margins.left, -margins.top));
}
//保存文档
newpdf.savetofile("output.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())
'加载一个测试文档
dim existingpdf as new pdfdocument()
existingpdf.loadfromfile("c:\test.pdf")
'创建新的pdf文档
dim newpdf as new pdfdocument()
'设置页面大小
newpdf.pagesettings.size = existingpdf.pages(0).size
'将页边距设置为0
newpdf.pagesettings.margins = new pdfmargins(0)
'创建pdfmargins对象,指定期望设置的页边距
'期望边距需小于或等于现有文档的实际边距,否则页眉页脚可能覆盖主体内容
dim margins as new pdfmargins(90, 72, 90, 72)
'在新建文档的顶部和底部应用页眉页脚模板
newpdf.template.top = createheadertemplate(newpdf, margins)
newpdf.template.bottom = createfootertemplate(newpdf, margins)
'在新建文档的左右部分应用空白模板
newpdf.template.left = new pdfpagetemplateelement(margins.left, newpdf.pagesettings.size.height)
newpdf.template.right = new pdfpagetemplateelement(margins.right, newpdf.pagesettings.size.height)
for i as integer = 0 to existingpdf.pages.count - 1
'添加页面到新建文档,并将现有文档当做模板绘制到新页面
newpdf.pages.add().canvas.drawtemplate(existingpdf.pages(i).createtemplate(), new pointf(-margins.left, -margins.top))
next
'保存文档
newpdf.savetofile("output.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
效果图: