本教程将展示如何在一个现成的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
效果图: