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.



48
49
50
# File 'lib/hexapdf/document/pages.rb', line 48

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.



87
88
89
90
# File 'lib/hexapdf/document/pages.rb', line 87

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.



125
126
127
# File 'lib/hexapdf/document/pages.rb', line 125

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

#add(page = nil) ⇒ Object

:call-seq:

pages.add             -> new_page
pages.add(media_box)  -> new_page
pages.add(page)       -> page

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

If no argument is given, a new page with the default dimensions (see configuration option ‘page.default_media_box’) is used. If the single argument is an array with four numbers (specifying the media box) or a symbol (referencing a pre-defined media box, see HexaPDF::Type::Page::PAPER_SIZE), the new page will have these dimensions.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hexapdf/document/pages.rb', line 68

def add(page = nil)
  case page
  when Array
    page = @document.add(Type: :Page, MediaBox: page)
  when Symbol
    if Type::Page::PAPER_SIZE.key?(page)
      media_box = Type::Page::PAPER_SIZE[page].dup
      page = @document.add(Type: :Page, MediaBox: media_box)
    else
      raise HexaPDF::Error, "Invalid page format specified: #{page}"
    end
  end
  @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.



139
140
141
# File 'lib/hexapdf/document/pages.rb', line 139

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.



107
108
109
# File 'lib/hexapdf/document/pages.rb', line 107

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.



118
119
120
# File 'lib/hexapdf/document/pages.rb', line 118

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.



134
135
136
# File 'lib/hexapdf/document/pages.rb', line 134

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.



97
98
99
# File 'lib/hexapdf/document/pages.rb', line 97

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.



53
54
55
# File 'lib/hexapdf/document/pages.rb', line 53

def root
  @document.catalog.pages
end