Class: Pdfrb::Document::Outline

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/document/outline.rb

Overview

Bookmark/outline facade. Builds the /Outlines tree on the Catalog so PDF viewers show a navigation panel.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Outline

Returns a new instance of Outline.



10
11
12
13
# File 'lib/pdfrb/document/outline.rb', line 10

def initialize(document)
  @document = document
  @entries = []
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/pdfrb/document/outline.rb', line 8

def document
  @document
end

#entriesObject (readonly)

Returns the value of attribute entries.



8
9
10
# File 'lib/pdfrb/document/outline.rb', line 8

def entries
  @entries
end

Instance Method Details

#add(title, dest: nil, parent: nil) ⇒ OutlineEntry

Add a top-level bookmark entry.

Parameters:

  • title (String)

    display text.

  • dest (Pdfrb::Model::Reference, Array, nil) (defaults to: nil)

    destination (page reference or explicit destination array).

  • parent (OutlineEntry, nil) (defaults to: nil)

    for nested entries.

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pdfrb/document/outline.rb', line 22

def add(title, dest: nil, parent: nil)
  entry = OutlineEntry.new(
    title: title.to_s,
    dest: dest,
    document: @document
  )
  if parent
    parent.add_child(entry)
  else
    @entries << entry
  end
  entry
end

#build!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pdfrb/document/outline.rb', line 36

def build!
  return if @entries.empty?

  entries = @entries
  root = @document.add(
    { Type: :Outlines, First: nil, Last: nil, Count: entries.length },
    type: Pdfrb::Model::Cos::Dictionary
  )

  prev_ref = nil
  first_ref = nil
  entries.each_with_index do |entry, _i|
    ref = entry.build!(@document)
    entry.value[:Parent] = Pdfrb::Model::Reference.new(root.oid, root.gen)
    entry.value[:Prev] = prev_ref if prev_ref
    entry.value[:Next] = nil
    prev_ref&.value&.[]=(:Next, ref)
    first_ref ||= ref
    prev_ref = ref
  end

  root.value[:First] = first_ref
  root.value[:Last] = prev_ref

  @document.catalog.value[:Outlines] =
    Pdfrb::Model::Reference.new(root.oid, root.gen)

  root
end