概述
页眉 页脚 是一篇完整、精致的文档的重要组成部分。在页眉页脚处,可以呈现的内容很多,如公司名称、页码、工作表名、日期、图片,如logo、标记等。在下面的文章中,将介绍如何在pdf中添加页眉页脚。通过代码测试,添加页眉页脚可以分两种情况来实现效果:
1.通过添加新的一页,在新建的页面上来添加页眉页脚
2.通过给现有文档直接添加页眉页脚
下面将根据这两种情况介绍具体的c#代码操作
使用工具
free spire.pdf for .net 4.3(社区版)
示例代码(供参考)
1.新建一页来添加页眉页脚
1.1 添加页眉
【c#】
using spire.pdf;
using spire.pdf.graphics;
using system.drawing;
using system;
namespace addheader_pdf
{
class program
{
static void main( string [] args)
{
//新建一个pdfdocument类对象,并添加一页
pdfdocument pdf = new pdfdocument();
pdfpagebase page = pdf.pages.add();
//设置margin
pdfunitconvertor unitcvtr = new pdfunitconvertor();
pdfmargins margin = new pdfmargins();
margin.top = unitcvtr.convertunits(2.54f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point);
margin.bottom = margin.top;
margin.left = unitcvtr.convertunits(4.17f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point);
margin.right = margin.left;
//调用addheader()方法添加页眉
addheader(pdf, pdfpagesize.a4, margin);
//保存并打开文档
pdf.savetofile( "pdf页眉.pdf" );
system.diagnostics.process.start( "pdf页眉.pdf" );
}
static void addheader(pdfdocument doc, sizef pagesize, pdfmargins margin)
{
//初始化一个pdfpagetemplateelement对象,用于创建页眉
pdfpagetemplateelement headerspace = new pdfpagetemplateelement(pagesize.width, margin.top);
headerspace.foreground = true ;
doc.template.top = headerspace;
//在页眉部分绘入文字
pdftruetypefont font = new pdftruetypefont( new font( "arial unicode ms" , 10f), true );
pdfstringformat format = new pdfstringformat(pdftextalignment.right);
string headertext = "world trade organization, wto \n the international organization that regulates international trade" ;
float x = pdfpagesize.a4.width;
float y = 0;
headerspace.graphics.drawstring(headertext, font, pdfbrushes.black, x, y, format);
//在页眉部分绘入图片
pdfimage headerimage = pdfimage.fromfile( @"c:\users\administrator\desktop\1.png" );
float width = headerimage.width / 2;
float height = headerimage.height / 3;
headerspace.graphics.drawimage(headerimage, 0, 0, width, height);
}
}
}
页眉添加效果:
1.2添加页脚
【c#】
using spire.pdf;
using spire.pdf.graphics;
using system.drawing;
using system;
using spire.pdf.automaticfields;
namespace addfooter_pdf
{
class program
{
static void main( string [] args)
{
//新建一个pdfdocument类对象,添加一页
pdfdocument doc = new pdfdocument();
pdfpagebase page = doc.pages.add();
//设置margin
pdfunitconvertor unitcvtr = new pdfunitconvertor();
pdfmargins margin = new pdfmargins();
margin.top = unitcvtr.convertunits(2.54f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point);
margin.bottom = margin.top;
margin.left = unitcvtr.convertunits(4.17f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point);
margin.right = margin.left;
//调用addfooter()方法添加页脚
addfooter(doc, pdfpagesize.a4, margin);
//调用addpagenumber()方法添加页码
addpagenumber(doc, margin);
//保存并打开文档
doc.savetofile( "pdf页脚.pdf" );
system.diagnostics.process.start( "pdf页脚.pdf" );
}
static void addfooter(pdfdocument doc, sizef pagesize, pdfmargins margin)
{
//初始化一个pdfpagetemplateelement对象,用于创建页脚
pdfpagetemplateelement footerspace = new pdfpagetemplateelement(pagesize.width, margin.bottom);
footerspace.foreground = true ;
doc.template.bottom = footerspace;
//在页脚部分绘入文字
pdftruetypefont font = new pdftruetypefont( new font( "arial unicode ms" , 10f), true );
pdfstringformat format = new pdfstringformat(pdftextalignment.center);
string headertext = "website : www.wto.org" ;
float x = pdfpagesize.a4.width / 2;
float y = 0;
footerspace.graphics.drawstring(headertext, font, pdfbrushes.black, x, y, format);
}
static void addpagenumber(pdfdocument doc, pdfmargins margin)
{
//添加页码到页脚部分
foreach (pdfpagebase page in doc.pages)
{
pdfstringformat format1 = new pdfstringformat(pdftextalignment.left);
int x1 = convert.toint32(page.canvas.clientsize.width / 2);
int y1 = convert.toint32(page.canvas.clientsize.height - margin.bottom + 20);
rectangle bounds = new rectangle(x1, y1, 20, 20);
pdfpagenumberfield field = new pdfpagenumberfield();
pdftruetypefont font = new pdftruetypefont( new font( "arial unicode ms" , 10f), true );
field.font = font;
field.stringformat = format1;
field.brush = pdfbrushes.black;
field.bounds = bounds;
field.draw(page.canvas);
}
}
}
}
页脚添加效果:
2.给现有pdf文档添加页眉页脚
【c#】
using spire.pdf;
using spire.pdf.automaticfields;
using spire.pdf.graphics;
using system;
using system.drawing;
namespace pdfheader
{
class program
{
static void main( string [] args)
{
//加载一个测试文档
pdfdocument existingpdf = new pdfdocument();
existingpdf.loadfromfile( "test.pdf" );
//调用drawheader方法在现有文档添加页眉
drawheader(existingpdf);
//调用drawfooter方法在现有文档添加页脚
drawfooter(existingpdf);
//保存并打开文档
existingpdf.savetofile( "output.pdf" );
system.diagnostics.process.start( "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( "logo.png" );
float width = headerimage.width / 7;
float height = headerimage.height / 7;
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 = " website\n https://g20.org/" ;
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);
}
}
}
}
测试效果:
注意事项
安装之后,添加引用spire.pdf.dll文件到项目程序即可,dll文件可在安装路径下的bin文件夹中获取。
以上是本次关于c#添加pdf页眉页脚的全部内容,两种情况中的示例方法,可以选择参考使用。
原文链接:https://segmentfault.com/a/1190000016209249
dy("nrwz");
查看更多关于C# 添加PDF页眉/页脚的示例代码的详细内容...