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: PDF2.0 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

Public Class Methods

media_box(paper_size, orientation: :portrait)

Returns the media box for the given paper size or array.

If an array is specified, it needs to contain exactly four numbers. The orientation argument is not used in this case.

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 = :crop) → box
box(type = :crop, 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: PDF2.0 s14.11.2

canvas(type: :page, translate_origin: true)

Returns the requested type of canvas for the page.

There are potentially three different canvas objects, one for each of the types :underlay, :page, and :overlay. The canvas objects are cached once they are created so that their graphics states are correctly retained without the need for parsing the contents. This also means that on subsequent invocations the graphic states of the canvases might already be changed.


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


Specifies whether the origin should automatically be translated into the lower-left corner of the crop box.

Note that this argument is only used for the first invocation for every canvas type. So if a canvas was initially requested with this argument set to false and then with true, it won’t have any effect as the cached canvas is returned.

To check whether the origin has been translated or not, use

canvas.pos(0, 0)

and check whether the result is [0, 0]. If it is, then the origin has not been translated.


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).

each_annotation {|annotation| block} → page
each_annotation → Enumerator

Yields each annotation of this page.

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 the label of the page which is an optional, alternative description of the page index.

See HexaPDF::Document::Pages for details.


Returns true since page objects must always be indirect.

orientation(type = :crop)

Returns the orientation of the specified box (default is the crop 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 (i.e. the /Rotate key) data but by rotating the canvas itself and all other necessary objects like the various page boxes and annotations.


  • The given angle is applied in addition to a possibly already existing rotation (specified via the /Rotate key) and does not replace it.

  • Specifying 0 for angle is valid and means that no additional rotation should be applied. The only meaningful usage of 0 for angle is when flatten is set to true (so that the /Rotate key is removed and the existing rotation information incorporated into the canvas, page boxes and annotations).

  • 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.