使用书签

书签类似于超链接,用于跳转到文档中的任何位置或主题,当你使用Acrobat Reader等程序打开PDF文档时,你可能已经注意到,大多数长文档都 会包含显示在左侧窗格中的大纲结构。这种大纲结构使浏览文档结构并查找特定主题变得容易。

大纲结构中的大纲条目称为书签。

AddBookmark方法用于增加书签,其语法为:

AddBookmark(text, level, y)
AddBookmark(text, level, y, isOpen)
AddBookmark(text, level, page, y)
AddBookmark(text, level, target)

参数 说明
text 书签在大纲树中显示的文本
level 书签在大纲树中的层级(0为第一级)
y 书签的垂直位置,在大纲中单击此书签会定位到此位置
isOpen 默认是否在大纲树中展开此书签,使得其子书签可见
page 书签所在页面,默认为当前页面
target 通过AddTarget增加的本地目标名,在大纲中单击书签会定位到该目标位置


示例

Dim file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
pdc.ConformanceLevel = PDF.PdfAConformanceLevel.PdfA1a
pdc.PaperKind = Drawing.Printing.PaperKind.B5

Dim
rectPage As RectangleF = pdc.PageRectangle()
rectPage.Inflate( - 72, - 72)
'设置页边距为72磅,也就是2.54厘米
'
定义个lambda过程用于定位和绘制内容,支持文本跨页绘制
Dim
RenderString =
Function
(tText As String, tFont As Font, tRect As RectangleF, tAlignment As stringAlignment, distance As Integer)
   
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 '定义一个变量,用于记录开始绘制字符的位置,默认为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
'计算最后一次绘制字符串的高度
       
End If
   
End While
    tRect.Offset(0, trect.Height + distance)
'移到下一个内容的起始位置
   
Return tRect '返回tRect
End
Function  
'
以下为内容绘制代码
Dim
s As String = "新华网绵阳516日电(记者孙承斌)16日上午,在四川抗震救灾的"
s = s &
"危急时刻,中共中央总书记?国家主席?中央军委主席胡锦涛乘飞机赶往四川"
s = s &
"省地震灾区,慰问灾区干部群众,看望奋战在抗震救灾第一线的部队官兵?"
s = s &
"安民警和医护人员,指导抗震救灾工作."
Dim
rect As RectangleF = rectPage 'rcCurrent为绘制区域,我们将通过调整rcCurrent的位置和高度,来实现动态布局
Dim
fntChapter As New Font("宋体", 12, FontStyle.Bold) '员工姓名字体
Dim
fntText As New Font("宋体", 10) '员工备注字体
For
i As Integer = 1 To 10
    pdc.AddBookmark(
"" & i & "", 0, rect.y)
    rect = RenderString.Invoke(
"" & i & "", fntChapter, rect, StringAlignment.Near, 15) '绘制员工姓名
   
For m As Integer = 1 To Rand.Next(5, 15)
         pdc.AddBookmark(
"" & m & "", 1, rect.y)
        rect = RenderString.Invoke(
"" & m & "", fntChapter, rect, StringAlignment.Near, 5)
        rect = RenderString.Invoke(s, fntText, rect, StringAlignment.Near, 10)
   
Next
    rect.Offset(0, 20)
'占间距20
Next

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

生成的文档可以显示一个大纲结构:


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