使用本地目标

请先打开CaseStyudy目录下的文件"PDFCreator示例.Table"再运行本节的示例代码。

AddTarget方法用于增加本地目标,其语法为:

AddTarget(name,rect)
AddTarget(name,page,rect)

参数 说明
name 目标名称,供AddLink方法调用
rect RectangleF类型,指定目标所在的矩形区域
page 整数型,指定目标所在的矩形区域,默认为当前页面。

Addlink可以用AddTarget增加的本地目标,不过需要在目标名前面加上符号"#"。

示例

假定一个文档有多页,希望在最后一页加上一个"返回第一页"的链接,参考代码:

Dim file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
Dim
rectPage As RectangleF = pdc.PageRectangle
rectPage.Inflate( - 72, - 72) 

'这个lambda过程是从前面复制过来的,用于跨页绘制长文本
Dim
drawText =
Function
(tText As String, tFont As Font, tAlignment As StringAlignment, tRect As RectangleF)
   
Dim tsf As New StringFormat
    tsf.Alignment = tAlignment
'设置对齐方式
    tRect.Height = tRect.Height + (rectPage.Bottom - tRect.Bottom)
'剩余空间全部分配给tRect
   
If trect.Height <= 0 Then '如果没有剩余空间
        pdc.NewPage()
'则另起一页
        tRect = rectPage
   
End If
   
Dim nextChar As Integer '定义一个变量,用于记录开始绘制字符的位置,默认为0 ,也就是从第一个字符开始绘制
   
While nextChar < tText.Length - 1 '如果还有剩余字符没有绘制
       
Dim firstChar = nextChar '用于记录每次绘制的开始位置
        nextChar = pdc.DrawString(tText, tFont, color.Black, tRect, nextChar, tsf)
'注意DrawString返回的就是未绘制内容的第一个的位置
       
If nextChar < tText.Length - 1 Then '如果还有剩余字符没有绘制
            pdc.NewPage()
'则另起一页
            tRect = rectPage
       
Else '如果已经绘制完毕
            tRect.Height = pdc.MeasureString(tText, tFont, trect.Width, tsf, firstChar).Height
            tRect.Offset(0, trect.Height)
'移到下一个内容的起始位置
       
End If
   
End While
   
Return tRect '返回tRect
End
Function  

Dim fnt As New Font("微软雅黑", 10)
Dim
txt As String = FileSys.ReadAllText(ProjectPath & "flow.txt")
Dim
rect As RectangleF = pdc.PageRectangle()
rect.Inflate( - 72, - 72)
pdc.AddTarget(
"first", rect) '增加一个本地目标,名称为first
rect = drawText.Invoke(txt, fnt, StringAlignment.Near, rect)
rect.Offset(0, 25)
rect.Size = pdc.MeasureString(
"返回第一页", fnt).ToSize()
pdc.DrawString(
"返回第一页", fnt, color.Blue, rect)
pdc.AddLink(
"#first", rect) '增加一个超链接,链接到本地目标first,注意名称前要加上符号#
pdc.Save(file)

Process.Start(file)

生成文档的最后一页为:

 

 

 


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