Class: HexaPDF::Type::Page
- Inherits:
-
Dictionary
- Object
- Object
- Dictionary
- HexaPDF::Type::Page
- Defined in:
- lib/hexapdf/type/page.rb
Overview
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
Constant Summary collapse
- 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
-
{ A0x4: [0, 0, 4768, 6741].freeze, A0x2: [0, 0, 3370, 4768].freeze, A0: [0, 0, 2384, 3370].freeze, A1: [0, 0, 1684, 2384].freeze, A2: [0, 0, 1191, 1684].freeze, A3: [0, 0, 842, 1191].freeze, A4: [0, 0, 595, 842].freeze, A5: [0, 0, 420, 595].freeze, A6: [0, 0, 298, 420].freeze, A7: [0, 0, 210, 298].freeze, A8: [0, 0, 147, 210].freeze, A9: [0, 0, 105, 147].freeze, A10: [0, 0, 74, 105].freeze, B0: [0, 0, 2835, 4008].freeze, B1: [0, 0, 2004, 2835].freeze, B2: [0, 0, 1417, 2004].freeze, B3: [0, 0, 1001, 1417].freeze, B4: [0, 0, 709, 1001].freeze, B5: [0, 0, 499, 709].freeze, B6: [0, 0, 354, 499].freeze, B7: [0, 0, 249, 354].freeze, B8: [0, 0, 176, 249].freeze, B9: [0, 0, 125, 176].freeze, B10: [0, 0, 88, 125].freeze, C0: [0, 0, 2599, 3677].freeze, C1: [0, 0, 1837, 2599].freeze, C2: [0, 0, 1298, 1837].freeze, C3: [0, 0, 918, 1298].freeze, C4: [0, 0, 649, 918].freeze, C5: [0, 0, 459, 649].freeze, C6: [0, 0, 323, 459].freeze, C7: [0, 0, 230, 323].freeze, C8: [0, 0, 162, 230].freeze, C9: [0, 0, 113, 162].freeze, C10: [0, 0, 79, 113].freeze, Letter: [0, 0, 612, 792].freeze, Legal: [0, 0, 612, 1008].freeze, Ledger: [0, 0, 792, 1224].freeze, Tabloid: [0, 0, 1224, 792].freeze, Executive: [0, 0, 522, 756].freeze, }.freeze
- INHERITABLE_FIELDS =
The inheritable fields.
[:Resources, :MediaBox, :CropBox, :Rotate].freeze
- REQUIRED_INHERITABLE_FIELDS =
The required inheritable fields.
[:Resources, :MediaBox].freeze
Constants included from DictionaryFields
DictionaryFields::Boolean, DictionaryFields::PDFByteString, DictionaryFields::PDFDate
Constants inherited from Object
Object::NOT_DUPLICATABLE_CLASSES
Instance Attribute Summary
Attributes inherited from Object
#data, #document, #must_be_indirect
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns the value for the entry
name
. -
#box(type = :media) ⇒ Object
Returns the rectangle defining a certain kind of box for the page.
-
#canvas(type: :page) ⇒ Object
Returns the requested type of canvas for the page.
-
#contents ⇒ Object
Returns the concatenated stream data from the content streams as binary string.
-
#contents=(data) ⇒ Object
Replaces the contents of the page with the given string.
-
#copy_inherited_values ⇒ Object
Copies the page’s inherited values from the ancestor page tree nodes into a hash and returns the hash.
-
#index ⇒ Object
Returns the index of the page in the page tree.
-
#must_be_indirect? ⇒ Boolean
Returns
true
since page objects must always be indirect. -
#process_contents(processor) ⇒ Object
Processes the content streams associated with the page with the given processor object.
-
#resources ⇒ Object
Returns the possibly inherited resource dictionary which is automatically created if it doesn’t exist.
-
#to_form_xobject(reference: true) ⇒ Object
Creates a Form XObject from the page’s dictionary and contents for the given PDF document.
Methods inherited from Dictionary
#[]=, define_field, #delete, #each, each_field, #empty?, field, #key?, #to_hash, #type
Methods inherited from Object
#<=>, #==, #deep_copy, deep_copy, #document?, #eql?, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, #null?, #oid, #oid=, #type, #validate, #value, #value=
Constructor Details
This class inherits a constructor from HexaPDF::Object
Instance Method Details
#[](name) ⇒ Object
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#[]
151 152 153 154 155 156 157 158 159 |
# File 'lib/hexapdf/type/page.rb', line 151 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 |
#box(type = :media) ⇒ Object
Returns the rectangle defining a certain kind of box for the page.
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
199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/hexapdf/type/page.rb', line 199 def box(type = :media) 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 |
#canvas(type: :page) ⇒ Object
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.
- 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
-
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/hexapdf/type/page.rb', line 277 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 document.cache(@data, cache_key) if document.cached?(@data, cache_key) if type == :page && key?(:Contents) raise HexaPDF::Error, "Cannot get the canvas for a page with contents" end contents = self[:Contents] if contents.nil? page_canvas = document.cache(@data, :page_canvas, Content::Canvas.new(self)) self[:Contents] = document.add({Filter: :FlateDecode}, stream: page_canvas.stream_data) end if type == :overlay || type == :underlay underlay_canvas = document.cache(@data, :underlay_canvas, Content::Canvas.new(self)) = document.cache(@data, :overlay_canvas, Content::Canvas.new(self)) 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 ") fiber = .stream_data.fiber while fiber.alive? && (data = fiber.resume) Fiber.yield(data) end end = document.add({Filter: :FlateDecode}, stream: stream) self[:Contents] = [underlay, *self[:Contents], ] end document.cache(@data, cache_key) end |
#contents ⇒ Object
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!
215 216 217 218 219 220 |
# File 'lib/hexapdf/type/page.rb', line 215 def contents Array(self[:Contents]).each_with_object("".b) do |content_stream, content| content << " ".freeze unless content.empty? content << document.deref(content_stream).stream end end |
#contents=(data) ⇒ Object
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.
226 227 228 229 230 231 232 233 234 235 |
# File 'lib/hexapdf/type/page.rb', line 226 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 ⇒ Object
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).
166 167 168 169 170 |
# File 'lib/hexapdf/type/page.rb', line 166 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 |
#index ⇒ Object
Returns the index of the page in the page tree.
253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/hexapdf/type/page.rb', line 253 def index idx = 0 node = self while (parent_node = node[:Parent]) parent_node[:Kids].each do |kid| kid = document.deref(kid) break if kid.data == node.data idx += (kid.type == :Page ? 1 : kid[:Count]) end node = parent_node end idx end |
#must_be_indirect? ⇒ Boolean
Returns true
since page objects must always be indirect.
141 142 143 |
# File 'lib/hexapdf/type/page.rb', line 141 def must_be_indirect? true end |
#process_contents(processor) ⇒ Object
Processes the content streams associated with the page with the given processor object.
See: HexaPDF::Content::Processor
246 247 248 249 250 |
# File 'lib/hexapdf/type/page.rb', line 246 def process_contents(processor) self[:Resources] = {} if self[:Resources].nil? processor.resources = self[:Resources] Content::Parser.parse(contents, processor) end |
#resources ⇒ Object
Returns the possibly inherited resource dictionary which is automatically created if it doesn’t exist.
239 240 241 |
# File 'lib/hexapdf/type/page.rb', line 239 def resources self[:Resources] ||= document.wrap({}, type: :XXResources) end |
#to_form_xobject(reference: true) ⇒ Object
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.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/hexapdf/type/page.rb', line 335 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 |