Class: Pdfrb::Document::Pages
- Inherits:
-
Object
- Object
- Pdfrb::Document::Pages
- Includes:
- Enumerable
- Defined in:
- lib/pdfrb/document/pages.rb
Overview
Page-tree facade. Exposes add, [], each, count,
delete against the Catalog's /Pages tree.
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Instance Method Summary collapse
- #[](index) ⇒ Object
-
#add(media_box: [0, 0, 612, 792], rotate: 0) ⇒ Object
(also: #<<)
Add a new page at the end of the page tree.
- #count ⇒ Object (also: #size, #length)
- #delete(page) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(document) ⇒ Pages
constructor
A new instance of Pages.
Constructor Details
#initialize(document) ⇒ Pages
Returns a new instance of Pages.
12 13 14 |
# File 'lib/pdfrb/document/pages.rb', line 12 def initialize(document) @document = document end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
10 11 12 |
# File 'lib/pdfrb/document/pages.rb', line 10 def document @document end |
Instance Method Details
#[](index) ⇒ Object
71 72 73 74 |
# File 'lib/pdfrb/document/pages.rb', line 71 def [](index) each_with_index { |p, i| return p if i == index } nil end |
#add(media_box: [0, 0, 612, 792], rotate: 0) ⇒ Object Also known as: <<
Add a new page at the end of the page tree.
Options:
media_box: [0, 0, 612, 792] # default US Letter
rotate: 0 # degrees
Returns the new Model::Type::Page.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pdfrb/document/pages.rb', line 23 def add(media_box: [0, 0, 612, 792], rotate: 0) root = pages_root contents = document.add({}, type: Pdfrb::Model::Cos::Stream) page = document.add( { Type: :Page, Parent: Pdfrb::Model::Reference.new(root.oid, root.gen), MediaBox: media_box, Resources: {}, Contents: Pdfrb::Model::Reference.new(contents.oid, 0) }, type: Pdfrb::Model::Type::Page ) page.value[:Rotate] = rotate if rotate.nonzero? kids = (root.value[:Kids] ||= []) kids << Pdfrb::Model::Reference.new(page.oid, page.gen) root.value[:Count] = (root.value[:Count] || 0) + 1 page end |
#count ⇒ Object Also known as: size, length
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/pdfrb/document/pages.rb', line 51 def count raw = pages_root[:Count] resolved = raw.is_a?(Pdfrb::Model::Reference) ? document.object(raw)&.value : raw case resolved when Integer then resolved when Numeric then resolved.to_i else # Fallback: walk the page tree. count = 0 each { count += 1 } count end end |
#delete(page) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/pdfrb/document/pages.rb', line 76 def delete(page) ref = ref_to(page) root = pages_root kids = root.value[:Kids] return unless kids&.delete(ref) root.value[:Count] = (root.value[:Count] || 1) - 1 page end |
#each(&block) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/pdfrb/document/pages.rb', line 44 def each(&block) return enum_for(:each) unless block_given? walk(pages_root, &block) self end |
#empty? ⇒ Boolean
67 68 69 |
# File 'lib/pdfrb/document/pages.rb', line 67 def empty? count.zero? end |