Class: HexaPDF::Document::Pages

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/hexapdf/document/pages.rb

Overview

This class provides methods for managing the pages of a PDF file.

It uses the methods of HexaPDF::Type::PageTreeNode underneath but provides a more convenient interface.

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Pages

Creates a new Pages object for the given PDF document.



46
47
48
# File 'lib/hexapdf/document/pages.rb', line 46

def initialize(document)
  @document = document
end

Instance Method Details

#<<(page) ⇒ Object

:call-seq:

pages << page            -> pages

Appends the given page at the end and returns the pages object itself to allow chaining.



64
65
66
67
# File 'lib/hexapdf/document/pages.rb', line 64

def <<(page)
  add(page)
  self
end

#[](index) ⇒ Object

Returns the page for the zero-based index, or nil if no such page exists.

Negative indices count backwards from the end, i.e. -1 is the last page.



102
103
104
# File 'lib/hexapdf/document/pages.rb', line 102

def [](index)
  @document.catalog.pages.page(index)
end

#add(page = nil) ⇒ Object

Adds the page or a new empty page at the end and returns it.



56
57
58
# File 'lib/hexapdf/document/pages.rb', line 56

def add(page = nil)
  @document.catalog.pages.add_page(page)
end

#countObject Also known as: size, length

Returns the number of pages in the PDF document. May be zero if the document has no pages.



116
117
118
# File 'lib/hexapdf/document/pages.rb', line 116

def count
  @document.catalog.pages.page_count
end

#delete(page) ⇒ Object

:call-seq:

pages.delete(page)       -> page or nil

Deletes the given page object from the document’s page tree (but not from the document).

Returns the page object, or nil if the page object was not in the page tree.



84
85
86
# File 'lib/hexapdf/document/pages.rb', line 84

def delete(page)
  @document.catalog.pages.delete_page(page)
end

#delete_at(index) ⇒ Object

:call-seq:

pages.delete_at(index)       -> page or nil

Deletes the page object at the given index from the document’s page tree (but not from the document).

Returns the page object, or nil if the index was invalid.



95
96
97
# File 'lib/hexapdf/document/pages.rb', line 95

def delete_at(index)
  @document.catalog.pages.delete_page(index)
end

#each(&block) ⇒ Object

:call-seq:

pages.each {|page| block }   -> pages
pages.each                   -> Enumerator

Iterates over all pages inorder.



111
112
113
# File 'lib/hexapdf/document/pages.rb', line 111

def each(&block)
  @document.catalog.pages.each_page(&block)
end

#insert(index, page = nil) ⇒ Object

Inserts the page or a new empty page at the zero-based index and returns it.

Negative indices count backwards from the end, i.e. -1 is the last page. When using negative indices, the page will be inserted after that element. So using an index of -1 will insert the page after the last page.



74
75
76
# File 'lib/hexapdf/document/pages.rb', line 74

def insert(index, page = nil)
  @document.catalog.pages.insert_page(index, page)
end

#rootObject

Returns the root of the page tree, a HexaPDF::Type::PageTreeNode object.



51
52
53
# File 'lib/hexapdf/document/pages.rb', line 51

def root
  @document.catalog.pages
end