class HexaPDF::Type::Page


Represents a page of a PDF document.

A page object contains the meta information for a page. Most of the fields are independent from the page's content like the /Dur field. However, some of them (like /Resources or /UserUnit) influence how or if the page's content can be rendered correctly.

A number of field values can also be inherited: /Resources, /MediaBox, /CropBox, /Rotate. Field inheritance means that if a field is not set on the page object itself, the value is taken from the nearest page tree ancestor that has this value set.

See: PDF1.7 s7.7.3.3, s7.7.3.4, Pages

Field Definitions

NameType/Allowed ValuesRequiredDefault Value
ParentHexaPDF::Type::PageTreeNode or Hashtruenil
LastModifiedHexaPDF::DictionaryFields::PDFDate or String or Time or Date or DateTimefalsenil
ResourcesHexaPDF::Type::Resources or Hashfalsenil
MediaBoxHexaPDF::Rectangle or Arrayfalsenil
CropBoxHexaPDF::Rectangle or Arrayfalsenil
BleedBoxHexaPDF::Rectangle or Arrayfalsenil
TrimBoxHexaPDF::Rectangle or Arrayfalsenil
ArtBoxHexaPDF::Rectangle or Arrayfalsenil
BoxColorInfoHexaPDF::Dictionary or Hashfalsenil
ContentsHexaPDF::Stream or HexaPDF::PDFArray or Hash or Arrayfalsenil
GroupHexaPDF::Dictionary or Hashfalsenil
ThumbHexaPDF::Stream or Hashfalsenil
BHexaPDF::PDFArray or Arrayfalsenil
TransHexaPDF::Dictionary or Hashfalsenil
AnnotsHexaPDF::PDFArray or Arrayfalsenil
AAHexaPDF::Dictionary or Hashfalsenil
MetadataHexaPDF::Stream or Hashfalsenil
PieceInfoHexaPDF::Dictionary or Hashfalsenil
IDHexaPDF::DictionaryFields::PDFByteString or Stringfalsenil
SeparationInfoHexaPDF::Dictionary or Hashfalsenil
PresStepsHexaPDF::Dictionary or Hashfalsenil
VPHexaPDF::PDFArray or Arrayfalsenil



The inheritable fields.


The predefined paper sizes in points (1/72 inch):

  • ISO sizes: A0x4, A0x2, A0-A10, B0-B10, C0-C10

  • Letter, Legal, Ledger, Tabloid, Executive


The required inheritable fields.

Public Class Methods

media_box(paper_size, orientation: :portrait)

Returns the media box for the given paper size. See PAPER_SIZE for the defined paper sizes.

Public Instance Methods


Returns the value for the entry name.

If name is an inheritable value and the value has not been set on the page object, its value is retrieved from the ancestor page tree nodes.

See: Dictionary#[]

Calls superclass method HexaPDF::Dictionary#[]

Returns all parent nodes of the page up to the root of the page tree.

The direct parent is the first node in the array and the root node the last.

box(type = :media) → box
box(type = :media, rectangle) → rectangle

If no rectangle is given, returns the rectangle defining a certain kind of box for the page. Otherwise sets the value for the given box type to rectangle (an array with four values or a HexaPDF::Rectangle).

This method should be used instead of directly accessing any of /MediaBox, /CropBox, /BleedBox, /ArtBox or /TrimBox because it also takes the fallback values into account!

The following types are allowed:


The media box defines the boundaries of the medium the page is to be printed on.


The crop box defines the region to which the contents of the page should be clipped when it is displayed or printed. The default is the media box.


The bleed box defines the region to which the contents of the page should be clipped when output in a production environment. The default is the crop box.


The trim box defines the intended dimensions of the page after trimming. The default value is the crop box.


The art box defines the region of the page's meaningful content as intended by the author. The default is the crop box.

See: PDF1.7 s14.11.2

canvas(type: :page)

Returns the requested type of canvas for the page.

The canvas object is cached once it is created so that its graphics state is correctly retained without the need for parsing its contents.

If the media box of the page doesn't have its origin at (0, 0), the canvas origin is translated into the bottom left corner so that this detail doesn't matter when using the canvas. This means that the canvas' origin is always at the bottom left corner of the media box.


Can either be

  • :page for getting the canvas for the page itself (only valid for initially empty pages)

  • :overlay for getting the canvas for drawing over the page contents

  • :underlay for getting the canvas for drawing unter the page contents


Returns the concatenated stream data from the content streams as binary string.

Note: Any modifications done to the returned value *won't* be reflected in any of the streams' data!


Replaces the contents of the page with the given string.

This is done by deleting all but the first content stream and reusing this content stream; or by creating a new one if no content stream exists.


Copies the page's inherited values from the ancestor page tree nodes into a hash and returns the hash.

The hash can then be used to update the page itself (e.g. when moving a page from one position to another) or another page (e.g. when importing a page from another document).

flatten_annotations(annotations = self[:Annots])

Flattens all or the given annotations of the page. Returns an array with all the annotations that couldn't be flattened because they don't have an appearance stream.

Flattening means making the appearances of the annotations part of the content stream of the page and deleting the annotations themselves. Invisible and hidden fields are deleted but not rendered into the content stream.

If an annotation is a form field widget, only the widget will be deleted but not the form field itself.


Returns the index of the page in the page tree.


Returns true since page objects must always be indirect.


Returns the orientation of the media box, either :portrait or :landscape.


Processes the content streams associated with the page with the given processor object.

See: HexaPDF::Content::Processor


Returns the, possibly inherited, resource dictionary which is automatically created if it doesn't exist.

rotate(angle, flatten: false)

Rotates the page angle degrees counterclockwise where angle has to be a multiple of 90.

Positive values rotate the page to the left, negative values to the right. If flatten is true, the rotation is not done via the page's meta data but by “rotating” the canvas itself.

Note that the :Rotate key of a page object describes the angle in a clockwise orientation but this method uses counterclockwise rotation to be consistent with other rotation methods (e.g. HexaPDF::Content::Canvas#rotate).

to_form_xobject(reference: true)

Creates a Form XObject from the page's dictionary and contents for the given PDF document.

If reference is true, the page's contents is referenced when possible to avoid unnecessary decoding/encoding.

Note 1: The created Form XObject is not added to the document automatically!

Note 2: If reference is false and if a canvas is used on this page (see canvas), this method should only be called once the contents of the page has been fully defined. The reason is that during the copying of the content stream data the contents may be modified to make it a fully valid content stream.