class HexaPDF::Document::Pages

Parent
Included Modules

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

For page manipulation it uses the methods of HexaPDF::Type::PageTreeNode underneath but provides a more convenient interface.

Page Labels

In addition to page manipulation, the class provides methods for managing the page labels which are alternative descriptions for the pages. In contrast to the page indices which are fixed the page labels can be freely defined.

The way this works is that one can assign page label objects (HexaPDF::Type::PageLabel) to page ranges via the /PageLabels number tree in the catalog. The page label objects specify how the pages in their range shall be labeled. See HexaPDF::Type::PageLabel for examples of page labels.

To facilitate the easy use of page labels the following methods are provided:

Public Class Methods

new(document)

Creates a new Pages object for the given PDF document.

Public Instance Methods

pages << page → pages

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

[](index)

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.

add → new_page
add(page) → page
add(media_box, orientation: nil) → new_page

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

If called with a page object as argument, that page object is used. Otherwise create is called with the arguments media_box and orientation to create a new page.

add_labelling_range(start_index, numbering_style: nil, prefix: nil, start_number: nil)

Adds a new labelling range starting at start_index and returns it.

See HexaPDF::Type::PageLabel for information on the arguments numbering_style, prefix, and start_number.

If a labelling range already exists for the given start_index, its value will be overwritten.

If there are no existing labelling ranges and the given start_index isn’t 0, a default labelling range using start index 0 and numbering style :decimal is added.

count()

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

Also aliased as: size, length
create(media_box: nil, orientation: nil)

Creates a page object and returns it without adding it to the page tree.

media_box

If this argument is nil/not specified, the value is taken from the configuration option ‘page.default_media_box’.

If the resulting value is an array with four numbers (specifying the media box), the new page will have these exact dimensions.

If the value is a symbol, it is taken as a reference to a pre-defined media box in HexaPDF::Type::Page::PAPER_SIZE. The orientation can then be used to specify the page orientation.

orientation

If this argument is not specified, it is taken from ‘page.default_media_orientation’. It is only used if media_box is a symbol and not an array.

delete(page)

Deletes the given page object from the document’s page tree and the document.

Also see: HexaPDF::Type::PageTreeNode#delete_page

delete_at(index)

Deletes the page object at the given index from the document’s page tree and the document.

Also see: HexaPDF::Type::PageTreeNode#delete_page

delete_labelling_range(start_index)

Deletes the page labelling range starting at start_index and returns the associated page label object.

Note: The page label for the range starting at zero can only be deleted last!

each {|page| block } → pages
each → Enumerator

Iterates over all pages inorder.

each_labelling_range {|first_index, count, page_label| block } → pages
each_labelling_range → Enumerator

Iterates over all defined labelling ranges inorder, yielding the page index of the first page in the labelling range, the number of pages in the range, and the associated page label object.

The last yielded count might be equal or lower than zero in case the document has fewer pages than anticipated by the labelling ranges.

insert(index, page = nil)

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.

length()
Alias for: count
move(page, to_index)
move(index, to_index)

Moves the given page or the page at the position specified by the zero-based index to the to_index position.

If the page that should be moved, doesn’t exist or is invalid, an error is raised.

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

page_label(page_index)

Returns the constructed page label for the given page index.

If no page labels are defined, nil is returned.

See HexaPDF::Type::PageLabel for examples.

root()

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

size()
Alias for: count