Class: HexaPDF::Document::Pages
- Inherits:
-
Object
- Object
- HexaPDF::Document::Pages
- 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
-
#<<(page) ⇒ Object
:call-seq: pages << page -> pages.
-
#[](index) ⇒ Object
Returns the page for the zero-based index, or
nil
if no such page exists. -
#add(page = nil, orientation: :portrait) ⇒ Object
:call-seq: pages.add -> new_page pages.add(media_box, orientation: :portrait) -> new_page pages.add(page) -> page.
-
#count ⇒ Object
(also: #size, #length)
Returns the number of pages in the PDF document.
-
#delete(page) ⇒ Object
Deletes the given page object from the document’s page tree and the document.
-
#delete_at(index) ⇒ Object
Deletes the page object at the given index from the document’s page tree and the document.
-
#each(&block) ⇒ Object
:call-seq: pages.each {|page| block } -> pages pages.each -> Enumerator.
-
#initialize(document) ⇒ Pages
constructor
Creates a new Pages object for the given PDF document.
-
#insert(index, page = nil) ⇒ Object
Inserts the page or a new empty page at the zero-based index and returns it.
-
#root ⇒ Object
Returns the root of the page tree, a HexaPDF::Type::PageTreeNode object.
Constructor Details
#initialize(document) ⇒ Pages
Creates a new Pages object for the given PDF document.
51 52 53 |
# File 'lib/hexapdf/document/pages.rb', line 51 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.
90 91 92 93 |
# File 'lib/hexapdf/document/pages.rb', line 90 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.
121 122 123 |
# File 'lib/hexapdf/document/pages.rb', line 121 def [](index) @document.catalog.pages.page(index) end |
#add(page = nil, orientation: :portrait) ⇒ Object
:call-seq:
pages.add -> new_page
pages.add(media_box, orientation: :portrait) -> 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), the new page will have these dimensions.
If the single argument is a symbol, it is taken as referencing a pre-defined media box in HexaPDF::Type::Page::PAPER_SIZE for the new page. The optional argument orientation
can be used to change the orientation to :landscape if needed.
76 77 78 79 80 81 82 83 84 |
# File 'lib/hexapdf/document/pages.rb', line 76 def add(page = nil, orientation: :portrait) if page.kind_of?(Array) page = @document.add({Type: :Page, MediaBox: page}) elsif page.kind_of?(Symbol) box = Type::Page.media_box(page, orientation: orientation) page = @document.add({Type: :Page, MediaBox: box}) end @document.catalog.pages.add_page(page) end |
#count ⇒ Object Also known as: size, length
Returns the number of pages in the PDF document. May be zero if the document has no pages.
135 136 137 |
# File 'lib/hexapdf/document/pages.rb', line 135 def count @document.catalog.pages.page_count end |
#delete(page) ⇒ Object
Deletes the given page object from the document’s page tree and the document.
Also see: HexaPDF::Type::PageTreeNode#delete_page
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
Deletes the page object at the given index from the document’s page tree and the document.
Also see: HexaPDF::Type::PageTreeNode#delete_page
114 115 116 |
# File 'lib/hexapdf/document/pages.rb', line 114 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.
130 131 132 |
# File 'lib/hexapdf/document/pages.rb', line 130 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.
100 101 102 |
# File 'lib/hexapdf/document/pages.rb', line 100 def insert(index, page = nil) @document.catalog.pages.insert_page(index, page) end |