I have been struggling to link a text heading with a specific page in the Pdf, similar to the linking of Table of Contents
in the Pdf file. I have found link_annotation
method to achieve this however there isn't suffice documentation/examples available to use link_annotation
method with :Desc
option.
Any ideas how to use link_annotation
with :Desc
option?
Link Annotation Documentation: https://www.rubydoc.info/gems/prawn-core/Prawn%2FDocument%2FAnnotations:link_annotation
For linking text to specific page at specific offset, Destinations
class provide many helper methods. By using dest_xyz, text can be linked to specific page at specific offset.
Here is the working example which links text from 1st page to text at 2nd page at specific offset.
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)