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

Constants

INHERITABLE_FIELDS

The inheritable fields.

PAPER_SIZE

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

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

  • Letter, Legal, Ledger, Tabloid, Executive

REQUIRED_INHERITABLE_FIELDS

The required inheritable fields.

Public Class Methods

media_box(paper_size, orientation: :portrait) click to toggle source

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

# File lib/hexapdf/type/page.rb, line 107
def self.media_box(paper_size, orientation: :portrait)
  unless PAPER_SIZE.key?(paper_size)
    raise HexaPDF::Error, "Invalid paper size specified: #{paper_size}"
  end

  media_box = PAPER_SIZE[paper_size].dup
  media_box[2], media_box[3] = media_box[3], media_box[2] if orientation == :landscape
  media_box
end

Public Instance Methods

[](name) click to toggle source

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#[]
# File lib/hexapdf/type/page.rb, line 167
def [](name)
  if value[name].nil? && INHERITABLE_FIELDS.include?(name)
    node = self
    node = node[:Parent] while node.value[name].nil? && node[:Parent]
    node == self || node.value[name].nil? ? super : node[name]
  else
    super
  end
end
ancestor_nodes() click to toggle source

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.

# File lib/hexapdf/type/page.rb, line 358
def ancestor_nodes
  parent = self[:Parent]
  result = [parent]
  result << parent while (parent = parent[:Parent])
  result
end
box(type = :media) → box click to toggle source
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:

:media

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

:crop

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.

:bleed

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.

:trim

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

:art

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

# File lib/hexapdf/type/page.rb, line 221
def box(type = :media, rectangle = nil)
  if rectangle
    case type
    when :media, :crop, :bleed, :trim, :art
      self["#{type.capitalize}Box".to_sym] = rectangle
    else
      raise ArgumentError, "Unsupported page box type provided: #{type}"
    end
  else
    case type
    when :media then self[:MediaBox]
    when :crop then self[:CropBox] || self[:MediaBox]
    when :bleed then self[:BleedBox] || self[:CropBox] || self[:MediaBox]
    when :trim then self[:TrimBox] || self[:CropBox] || self[:MediaBox]
    when :art then self[:ArtBox] || self[:CropBox] || self[:MediaBox]
    else
      raise ArgumentError, "Unsupported page box type provided: #{type}"
    end
  end
end
canvas(type: :page) click to toggle source

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.

type

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

# File lib/hexapdf/type/page.rb, line 380
def canvas(type: :page)
  unless [:page, :overlay, :underlay].include?(type)
    raise ArgumentError, "Invalid value for 'type', expected: :page, :underlay or :overlay"
  end
  cache_key = "#{type}_canvas".intern
  return cache(cache_key) if cached?(cache_key)

  if type == :page && key?(:Contents)
    raise HexaPDF::Error, "Cannot get the canvas for a page with contents"
  end

  create_canvas = lambda do
    Content::Canvas.new(self).tap do |canvas|
      media_box = box(:media)
      if media_box.left != 0 || media_box.bottom != 0
        canvas.translate(media_box.left, media_box.bottom)
      end
    end
  end

  contents = self[:Contents]
  if contents.nil?
    page_canvas = cache(:page_canvas, create_canvas.call)
    self[:Contents] = document.add({Filter: :FlateDecode},
                                   stream: page_canvas.stream_data)
  end

  if type == :overlay || type == :underlay
    underlay_canvas = cache(:underlay_canvas, create_canvas.call)
    overlay_canvas = cache(:overlay_canvas, create_canvas.call)

    stream = HexaPDF::StreamData.new do
      Fiber.yield(" q ")
      fiber = underlay_canvas.stream_data.fiber
      while fiber.alive? && (data = fiber.resume)
        Fiber.yield(data)
      end
      " Q q "
    end
    underlay = document.add({Filter: :FlateDecode}, stream: stream)

    stream = HexaPDF::StreamData.new do
      Fiber.yield(" Q q ")
      fiber = overlay_canvas.stream_data.fiber
      while fiber.alive? && (data = fiber.resume)
        Fiber.yield(data)
      end
      " Q "
    end
    overlay = document.add({Filter: :FlateDecode}, stream: stream)

    self[:Contents] = [underlay, *self[:Contents], overlay]
  end

  cache(cache_key)
end
contents() click to toggle source

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!

# File lib/hexapdf/type/page.rb, line 303
def contents
  Array(self[:Contents]).each_with_object("".b) do |content_stream, content|
    content << " " unless content.empty?
    content << content_stream.stream
  end
end
contents=(data) click to toggle source

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.

# File lib/hexapdf/type/page.rb, line 314
def contents=(data)
  first, *rest = self[:Contents]
  rest.each {|stream| document.delete(stream) }
  if first
    self[:Contents] = first
    document.deref(first).stream = data
  else
    self[:Contents] = document.add({Filter: :FlateDecode}, stream: data)
  end
end
copy_inherited_values() click to toggle source

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

# File lib/hexapdf/type/page.rb, line 182
def copy_inherited_values
  INHERITABLE_FIELDS.each_with_object({}) do |name, hash|
    hash[name] = HexaPDF::Object.deep_copy(self[name]) if value[name].nil?
  end
end
flatten_annotations(annotations = self[:Annots]) click to toggle source

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.

# File lib/hexapdf/type/page.rb, line 476
def flatten_annotations(annotations = self[:Annots])
  return [] unless key?(:Annots)

  not_flattened = annotations.to_ary
  annotations = not_flattened & self[:Annots] if annotations != self[:Annots]
  return not_flattened if annotations.empty?

  canvas = self.canvas(type: :overlay)
  canvas.save_graphics_state
  media_box = box(:media)
  if media_box.left != 0 || media_box.bottom != 0
    canvas.translate(-media_box.left, -media_box.bottom) # revert initial translation of origin
  end

  to_delete = []
  not_flattened -= annotations
  annotations.each do |annotation|
    annotation = document.wrap(annotation, type: :Annot)
    appearance = annotation.appearance
    if annotation.flagged?(:hidden) || annotation.flagged?(:invisible)
      to_delete << annotation
      next
    elsif !appearance
      not_flattened << annotation
      next
    end

    rect = annotation[:Rect]
    box = appearance.box
    matrix = appearance[:Matrix]

    # Adjust position based on matrix
    pos = [rect.left - matrix[4], rect.bottom - matrix[5]]

    # In case of a rotation we need to counter the default translation in #xobject by adding
    # box.left and box.bottom, and then translate the origin for the rotation
    angle = (-Math.atan2(matrix[2], matrix[0]) * 180 / Math::PI).to_i
    case angle
    when 0
      # Nothing to do, no rotation
    when 90
      pos[0] += box.top + box.left
      pos[1] += -box.left + box.bottom
    when -90
      pos[0] += -box.bottom + box.left
      pos[1] += box.right + box.bottom
    when 180, -180
      pos[0] += box.right + box.left
      pos[1] += box.top + box.bottom
    else
      not_flattened << annotation
      next
    end

    width, height = (angle.abs == 90 ? [rect.height, rect.width] : [rect.width, rect.height])
    canvas.xobject(appearance, at: pos, width: width, height: height)
    to_delete << annotation
  end
  canvas.restore_graphics_state

  to_delete.each do |annotation|
    if annotation[:Subtype] == :Widget
      annotation.form_field.delete_widget(annotation)
    else
      self[:Annots].delete(annotation)
      document.delete(annotation)
    end
  end

  not_flattened
end
index() click to toggle source

Returns the index of the page in the page tree.

# File lib/hexapdf/type/page.rb, line 342
def index
  idx = 0
  node = self
  while (parent_node = node[:Parent])
    parent_node[:Kids].each do |kid|
      break if kid == node
      idx += (kid.type == :Page ? 1 : kid[:Count])
    end
    node = parent_node
  end
  idx
end
must_be_indirect?() click to toggle source

Returns true since page objects must always be indirect.

# File lib/hexapdf/type/page.rb, line 157
def must_be_indirect?
  true
end
orientation() click to toggle source

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

# File lib/hexapdf/type/page.rb, line 243
def orientation
  box = self[:MediaBox]
  rotation = self[:Rotate]
  if (box.height > box.width && (rotation == 0 || rotation == 180)) ||
      (box.height < box.width && (rotation == 90 || rotation == 270))
    :portrait
  else
    :landscape
  end
end
process_contents(processor) click to toggle source

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

See: HexaPDF::Content::Processor

# File lib/hexapdf/type/page.rb, line 335
def process_contents(processor)
  self[:Resources] = {} if self[:Resources].nil?
  processor.resources = self[:Resources]
  Content::Parser.parse(contents, processor)
end
resources() click to toggle source

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

# File lib/hexapdf/type/page.rb, line 327
def resources
  self[:Resources] ||= document.wrap({ProcSet: [:PDF, :Text, :ImageB, :ImageC, :ImageI]},
                                     type: :XXResources)
end
rotate(angle, flatten: false) click to toggle source

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

# File lib/hexapdf/type/page.rb, line 263
def rotate(angle, flatten: false)
  if angle % 90 != 0
    raise ArgumentError, "Page rotation has to be multiple of 90 degrees"
  end

  cw_angle = (self[:Rotate] - angle) % 360

  if flatten
    delete(:Rotate)
    return if cw_angle == 0

    matrix, llx, lly, urx, ury = \
      case cw_angle
      when 90
        [HexaPDF::Content::TransformationMatrix.new(0, -1, 1, 0),
         box.right, box.bottom, box.left, box.top]
      when 180
        [HexaPDF::Content::TransformationMatrix.new(-1, 0, 0, -1),
         box.right, box.top, box.left, box.bottom]
      when 270
        [HexaPDF::Content::TransformationMatrix.new(0, 1, -1, 0),
         box.left, box.top, box.right, box.bottom]
      end
    [:MediaBox, :CropBox, :BleedBox, :TrimBox, :ArtBox].each do |box|
      next unless key?(box)
      self[box].value = matrix.evaluate(llx, lly).concat(matrix.evaluate(urx, ury))
    end

    before_contents = document.add({}, stream: " q #{matrix.to_a.join(' ')} cm ")
    after_contents = document.add({}, stream: " Q ")
    self[:Contents] = [before_contents, *self[:Contents], after_contents]
  else
    self[:Rotate] = cw_angle
  end
end
to_form_xobject(reference: true) click to toggle source

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.

# File lib/hexapdf/type/page.rb, line 448
def to_form_xobject(reference: true)
  first, *rest = self[:Contents]
  stream = if !first
             nil
           elsif !reference || !rest.empty? || first.raw_stream.kind_of?(String)
             contents
           else
             first.raw_stream
           end
  dict = {
    Type: :XObject,
    Subtype: :Form,
    BBox: HexaPDF::Object.deep_copy(box(:crop)),
    Resources: HexaPDF::Object.deep_copy(self[:Resources]),
    Filter: :FlateDecode,
  }
  document.wrap(dict, stream: stream)
end

Private Instance Methods

perform_validation() { |"Inheritable page field #{name} not set", name == :Resources| ... } click to toggle source

Ensures that the required inheritable fields are set.

Calls superclass method HexaPDF::Dictionary#perform_validation
# File lib/hexapdf/type/page.rb, line 551
def perform_validation(&block)
  super
  REQUIRED_INHERITABLE_FIELDS.each do |name|
    next if self[name]
    yield("Inheritable page field #{name} not set", name == :Resources)
    resources.validate(&block) if name == :Ressources
  end
end