我一直努力将文本标题与Pdf中的特定页面链接,类似于Pdf文件中的Table of Contents
链接。我发现link_annotation
方法可以实现此目的,但是没有足够的文档/示例可以将link_annotation
方法与:Desc
选项一起使用。
关于如何将link_annotation
与:Desc
选项一起使用的任何想法?
链接注释文档:https://www.rubydoc.info/gems/prawn-core/Prawn%2FDocument%2FAnnotations:link_annotation
用于以特定偏移量将文本链接到特定页面,Destinations
类提供许多辅助方法。通过使用dest_xyz,可以将文本以特定的偏移量链接到特定的页面。
这里是将第1页的文本链接到第2页的文本的特定偏移量的工作示例。
require 'Prawn'
margin = 36.0
extra_clickable_margin = 4.0
top_offset = 500.0
@pdf = Prawn::Document.new(margin: margin, page_size: 'A4')
clickable_text = "Clickable Annotation to 2nd page at offset #{top_offset}"
text_width = @pdf.width_of(clickable_text)
text_height = @pdf.height_of(clickable_text)
cursor = @pdf.cursor
@pdf.text clickable_text
left = margin
top = cursor + margin + extra_clickable_margin
bottom = top - text_height - extra_clickable_margin
right = left + text_width
src_rect = [left, bottom, right, top]
@pdf = Prawn::Document.new(margin: margin, page_size: 'A4')
@pdf.start_new_page
@pdf.text 'Some Text for 2nd page. ' * 40
dest = @pdf.dest_xyz(0, top_offset, nil, @pdf.state.pages[1])
@pdf.link_annotation(src_rect, Dest: dest)