页眉页脚

PDFCreater并没有直接设置页眉页脚的方法,而是和正文内容一样,通过绘制完成,要点为:

1、基本的思路是通过PageRectangle方法获得页面矩形区域,然后通过Inflate内缩(设置页边距)得到正文的矩形局域。

2、一般正文矩形区域之下6磅位置为页脚,正文矩形区域之上6磅位置为页眉。

3、PDCreator所有内容都会绘制在当前页,每次增加页面,新增页面就会自动变为当前页。

4、通过CurrentPage属性可以用于设置当前页的编号,例如将当前页改为第一页:

pdc.CurrentPage = 0

5、通过Pages属性可以获得一个包括所有页面的集合。

示例一

如果要绘制页脚,可以参考下面的代码:

Dim file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
Dim
rect As RectangleF = pdc.PageRectangle() '正文矩形
rect.Inflate( - 72, - 72)
'正文矩形内缩72磅,也就是页面距为2.54厘米
Dim
rcFooter As RectangleF = rect
rcFooter.Y = rect.Bottom + 6
'页脚距离正文6
rcFooter.Height = 66
'页脚高度为72-6 = 66
Dim
fnt As New Font("微软雅黑", 36)
Dim
fntFooter As New Font("微软雅黑", 10) '页脚字体
Dim
lfFooter As New StringFormat() '页脚文本格式
lfFooter.Alignment = StringAlignment.Far
'页脚文本向右对齐
pdc.Pages.Clear()

For
i As Integer = 1 To 10
    pdc.NewPage()
    pdc.DrawString(
"Hello Foxtable", fnt, Brushes.Black, rect)
    pdc.DrawString(Cexp(
"{0} {1}", i, 10), fntFooter, color.Black, rcFooter, lfFooter) '绘制页码
   
'在正文和页脚之间页脚绘制一条横线,如果不需要,可删除此行代码
    pdc.DrawLine(
New Pen(color.Gray, 0.3), rcFooter.Left, rcFooter.Top - 3, rcFooter.Right, rcFooter.Top - 3)
Next

pdc.Save(file)

Process.Start(file)

生成的页脚为:

示例二

有的时候只有绘制完全部内容后,才能知道总页数,用前面的代码就不合适了。

这种情况其实也很简单,只需在绘制完所有内容后,从头遍历所有页面,逐页绘制页脚即可:

For i As Integer = 0 To pdc.Pages.Count - 1
    pdc.CurrentPage = i
'设置当前页面
    pdc.DrawString(Cexp(
"{0} {1}", i + 1, pdc.Pages.Count), fntFooter, color.Black, rcFooter, lfFooter) '绘制页码
Next

下面是完整的参考代码:

Dim file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
Dim
fnt As New Font("Arial", 18)
Dim
txt As String = FileSys.ReadAllText(ProjectPath & "flow.txt") '内容来自于一个文本文件
Dim
nextChar As Integer '定义一个变量,用于记录开始绘制字符的位置,默认为0 ,也就是从第一个字符开始绘制
Dim
rect As RectangleF = pdc.PageRectangle()'正文矩形
rect.Inflate( - 72, - 72)
'正文矩形内缩72磅,也就是页面距为2.54厘米
Dim
rcFooter As RectangleF = rect
rcFooter.Y = rect.Bottom + 6
'页脚距离正文6
rcFooter.Height = 66
'页脚高度为72-6 = 66
Dim
fntFooter As New Font("微软雅黑", 10) '页脚字体
Dim
lfFooter As New StringFormat() '页脚文本格式
lfFooter.Alignment = StringAlignment.Far
'页脚文本向右对齐
pdc.Pages.Clear()
'默认已经有一个页面,先清除掉
While
nextChar < txt.Length - 1 '如果还有剩余字符没有绘制
    pdc.NewPage()
'新增一页
    nextChar = pdc.DrawString(txt, fnt, Brushes.Black, rect, nextChar)
'注意DrawString返回值为未绘制内容的起始位置
End
While
For
i As Integer = 0 To pdc.Pages.Count - 1
    pdc.CurrentPage = i
'设置当前页面
    pdc.DrawString(Cexp(
"{0} {1}", i + 1, pdc.Pages.Count), fntFooter, color.Black, rcFooter, lfFooter) '绘制页码
Next

pdc.Save(file)

Process.Start(file)

示例三

如果需要将页脚分成多个区,例如左中右三个区,可以参考代码:

Dim file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
Dim
fnt As New Font("Arial", 18)
Dim
txt As String = FileSys.ReadAllText(ProjectPath & "flow.txt") '内容来自于一个文本文件
Dim
nextChar As Integer '定义一个变量,用于记录开始绘制字符的位置,默认为0 ,也就是从第一个字符开始绘制
Dim
rect As RectangleF = pdc.PageRectangle()'正文矩形
rect.Inflate( - 72, - 72)
'正文矩形内缩72磅,也就是页面距为2.54厘米
Dim
rcFooter As RectangleF = rect
rcFooter.Y = rect.Bottom + 6
'页脚距离正文6
rcFooter.Height = 66
'页脚高度为72-6 = 66
Dim
fntFooter As New Font("微软雅黑", 10) '页脚字体
Dim
lfFooter As New StringFormat() '页脚文本格式
lfFooter.Alignment = StringAlignment.Far
'页脚文本向右对齐
pdc.Pages.Clear()
'默认已经有一个页面,先清除掉
While
nextChar < txt.Length - 1 '如果还有剩余字符没有绘制
    pdc.NewPage()
'新增一页
    nextChar = pdc.DrawString(txt, fnt, Brushes.Black, rect, nextChar)
'注意DrawString返回值为未绘制内容的起始位置
End
While
For
i As Integer = 0 To pdc.Pages.Count - 1
    pdc.CurrentPage = i
'设置当前页面
   
Dim rcTemp As RectangleF = rcFooter
    rcTemp.Width = rcFooter.Width / 3
   
'左页脚
    lfFooter.Alignment = StringAlignment.Near
'靠左对齐
    pdc.DrawString(
"Foxtale", fntFooter, color.Black, rcFooter, lfFooter)
   
'中页脚
    rcTemp.Offset(rcTemp.Width, 0)
    lfFooter.Alignment = StringAlignment.Center
'居中对齐
    pdc.DrawString(
"2026", fntFooter, color.Black, rcFooter, lfFooter)
   
'右页脚
    rcTemp.Offset(rcTemp.Width, 0)
    lfFooter.Alignment = StringAlignment.Far
'靠右对齐
    pdc.DrawString(Cexp(
"{0} {1}", i + 1, pdc.Pages.Count), fntFooter, color.Black, rcFooter, lfFooter) '绘制页码
    pdc.DrawLine(pens.Gray, rcFooter.Left, rcFooter.Top - 3, rcFooter.Right, rcFooter.Top - 3)

Next

pdc.Save(file)
Process.Start(file)

这是生成的页脚,包括左中右三部分:

示例四

页眉也是一样的绘制,参考代码:

Dim file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
Dim
fnt As New Font("Arial", 18)
Dim
txt As String = FileSys.ReadAllText(ProjectPath & "flow.txt") '内容来自于一个文本文件
Dim
nextChar As Integer '定义一个变量,用于记录开始绘制字符的位置,默认为0 ,也就是从第一个字符开始绘制
Dim
rect As RectangleF = pdc.PageRectangle()'正文矩形
rect.Inflate( - 72, - 72)
'正文矩形内缩72磅,也就是页面距为2.54厘米
Dim
rcHeader As RectangleF = rect
rcHeader.Y = 0
'
rcHeader.Height = 66
'页眉距离正文6,72-6
Dim
fntHeader As New Font("微软雅黑", 10) '页眉字体
Dim
lfHeader As New StringFormat() '页眉文本格式
lfHeader.Alignment = StringAlignment.Far
'页眉文本向右对齐
lfHeader.LineAlignment = StringAlignment.Far
'页眉文本向下对齐
pdc.Pages.Clear()
'默认已经有一个页面,先清除掉
While
nextChar < txt.Length - 1 '如果还有剩余字符没有绘制
    pdc.NewPage()
'新增一页
    nextChar = pdc.DrawString(txt, fnt, Brushes.Black, rect, nextChar)
'注意DrawString返回值为未绘制内容的起始位置
End
While
For
i As Integer = 0 To pdc.Pages.Count - 1
    pdc.CurrentPage = i
'设置当前页面
    pdc.DrawString(Cexp(
"{0} {1}", i + 1, pdc.Pages.Count), fntHeader, color.Black, rcHeader, lfHeader) '绘制页码
   
'在正文和页眉之间页脚绘制一条横线,如果不需要,可删除此行代码
    pdc.DrawLine(pens.Gray, rcHeader.Left, rcHeader.Bottom + 3, rcHeader.Right, rcHeader.Bottom + 3)

Next

pdc.Save(file)

Process.Start(file)

示例五

除了页眉页脚,你还可以绘制左边栏和右边栏,以绘制左边栏为例,参考代码为:

Dim file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
Dim
fnt As New Font("Arial", 18)
Dim
txt As String = FileSys.ReadAllText(ProjectPath & "flow.txt") '内容来自于一个文本文件
Dim
nextChar As Integer '定义一个变量,用于记录开始绘制字符的位置,默认为0 ,也就是从第一个字符开始绘制
Dim
rect As RectangleF = pdc.PageRectangle()'正文矩形
rect.Inflate( - 72, - 72)
'正文矩形内缩72磅,也就是页面距为2.54厘米
Dim
rcHeader As RectangleF = rect
rcHeader.X = 0
rcHeader.Width = 60
'左边栏距离正文12,72-12
Dim
fntHeader As New Font("微软雅黑", 12) '左边栏字体
Dim
lfHeader As New StringFormat() '左边栏文本格式
lfHeader.FormatFlags = StringFormatFlags.DirectionVertical
Or lfHeader.FormatFlags
lfHeader.Alignment = StringAlignment.Center
'左边栏文本向右对齐
lfHeader.LineAlignment = StringAlignment.Far
'左边栏文本向下对齐
pdc.Pages.Clear()
'默认已经有一个页面,先清除掉
While
nextChar < txt.Length - 1 '如果还有剩余字符没有绘制
    pdc.NewPage()
'新增一页
    nextChar = pdc.DrawString(txt, fnt, Brushes.Black, rect, nextChar)
'注意DrawString返回值为未绘制内容的起始位置
End
While
Dim
LeftHeader As String = "考生姓名                             准考证号                             "
For
i As Integer = 0 To pdc.Pages.Count - 1
    pdc.CurrentPage = i
'设置当前页面
    pdc.DrawString(LeftHeader , fntHeader, color.Black, rcHeader, lfHeader)
'绘制页码
   
'在正文和左边栏之间页脚绘制一条横线,如果不需要,可删除此行代码
    pdc.DrawLine(pens.LightGray, rcHeader.Right + 3, rcHeader.Top, rcHeader.Right + 3, rcHeader.Bottom)

Next

pdc.Save(file)

Process.Start(file)

生成的左边栏为:


本页地址:http://www.foxtable.com/webhelp/topics/6059.htm