Class: HexaPDF::Type::Outline

Inherits:
Dictionary show all
Defined in:
lib/hexapdf/type/outline.rb

Overview

Represents the root of the PDF’s document outline containing a hierarchy of outline items (sometimes called bookmarks) in a linked list.

The document outline usually contains items for the sections of the document, so that clicking on an item opens the page where the section starts (the section header is). Most PDF viewers are able to display the outline to aid in navigation, though not all apply the optional attributes like the text color.

The outline dictionary is linked via the /Outlines entry from the Type::Catalog and can directly be accessed via HexaPDF::Document#outline.

Examples

Here is an example for creating an outline:

doc = HexaPDF::Document.new
5.times { doc.pages.add }
doc.outline.add_item("Section 1", destination: 0) do |sec1|
  sec1.add_item("Page 2", destination: doc.pages[1])
  sec1.add_item("Page 3", destination: 2)
  sec1.add_item("Section 1.1", text_color: "red", flags: [:bold]) do |sec11|
    sec11.add_item("Page 4", destination: 3)
  end
end

Here is one for copying the complete outline from one PDF to another:

doc = HexaPDF::Document.open(ARGV[0])
target = HexaPDF::Document.new
stack = [target.outline]
doc.outline.each_item do |item, level|
  if stack.size < level
    stack << stack.last[:Last]
  elsif stack.size > level
    (stack.size - level).times { stack.pop }
  end
  stack.last.add_item(target.import(item))
end
# Copying all the pages so that the references work.
doc.pages.each {|page| target.pages << target.import(page) }

See: PDF2.0 s12.3.3

Constant Summary

Constants included from DictionaryFields

DictionaryFields::Boolean, DictionaryFields::PDFByteString, DictionaryFields::PDFDate

Instance Attribute Summary

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from Dictionary

#[], #[]=, define_field, define_type, #delete, #each, each_field, #empty?, field, #key?, #to_hash, type, #type

Methods inherited from Object

#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, field, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, make_direct, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=

Constructor Details

This class inherits a constructor from HexaPDF::Object

Instance Method Details

#add_item(title, **options, &block) ⇒ Object

Adds a new top-level outline item.

See OutlineItem#add_item for details on the available options since this method just passes all arguments through to it.



97
98
99
100
# File 'lib/hexapdf/type/outline.rb', line 97

def add_item(title, **options, &block)
  self[:Count] ||= 0
  self_as_item.add_item(title, **options, &block)
end

#each_item(&block) ⇒ Object

:call-seq:

outline.each_item {|item| block }   -> item
outline.each_item                   -> Enumerator

Iterates over all items of the outline.

The items are yielded in-order, yielding first the item itself and then its descendants.



109
110
111
# File 'lib/hexapdf/type/outline.rb', line 109

def each_item(&block)
  self_as_item.each_item(&block)
end