Class: Pdfrb::Model::Type::Page

Inherits:
Cos::Dictionary show all
Defined in:
lib/pdfrb/model/type/page.rb

Overview

Page object (s7.7.3.3). Fields: /Type, /Parent, /LastModified, /Resources, /MediaBox, /CropBox, /BleedBox, /TrimBox, /ArtBox, /BoxColorInfo, /Contents, /Rotate, /Group, /Thumb, /B, /Dur, /Trans, /Annots, /AA, /Metadata, /PieceInfo, /StructParents, /ID, /PZ, /SeparationInfo, /Tabs, /TemplateInstantiated, /PresSteps, /UserUnit, /VP, /AF, /OutputIntents, /DPart.

Instance Attribute Summary

Attributes inherited from Object

#document, #gen, #oid, #value

Instance Method Summary collapse

Methods inherited from Cos::Dictionary

#[], #[]=, apply_arlington_definition, arlington_default, arlington_key_to_symbol, arlington_one_type_to_ruby, arlington_required?, arlington_scalar, arlington_types_to_ruby, arlington_version, define_field, define_field_from_arlington, #delete, #each, each_field, #each_raw, #empty?, field, #initialize, #key?, #keys, lookup_type, merged_field, own_fields, #pdf_type, register_type, type_map, #validate

Methods included from Cos::ArlingtonBacked

#arlington_object

Methods inherited from Object

#==, define_type, #deref, #indirect?, #initialize, #must_be_indirect?, #pdf_type, pdf_type, #ref

Constructor Details

This class inherits a constructor from Pdfrb::Model::Cos::Dictionary

Instance Method Details

#annotationsObject



106
107
108
# File 'lib/pdfrb/model/type/page.rb', line 106

def annotations
  self[:Annots]
end

#art_boxObject



91
92
93
# File 'lib/pdfrb/model/type/page.rb', line 91

def art_box
  value[:ArtBox]
end

#art_box=(box) ⇒ Object



75
76
77
# File 'lib/pdfrb/model/type/page.rb', line 75

def art_box=(box)
  value[:ArtBox] = box
end

#associated_filesObject



142
143
144
# File 'lib/pdfrb/model/type/page.rb', line 142

def associated_files
  self[:AF]
end

#bleed_boxObject



83
84
85
# File 'lib/pdfrb/model/type/page.rb', line 83

def bleed_box
  value[:BleedBox]
end

#bleed_box=(box) ⇒ Object



67
68
69
# File 'lib/pdfrb/model/type/page.rb', line 67

def bleed_box=(box)
  value[:BleedBox] = box
end

#canvasObject

Returns a Content::Canvas bound to this page's /Contents stream. Auto-creates a Contents stream if the page has none. Memoised per page instance.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pdfrb/model/type/page.rb', line 19

def canvas
  @canvas ||= begin
    ref = value[:Contents]
    stream =
      if ref.nil?
        new_stream = document.add({}, type: Pdfrb::Model::Cos::Stream)
        new_stream.stream = ""
        value[:Contents] = new_stream.ref
        new_stream
      else
        document.resolve(ref)
      end
    Pdfrb::Content::Canvas.new(stream, document: document)
  end
end

#crop_boxObject



55
56
57
# File 'lib/pdfrb/model/type/page.rb', line 55

def crop_box
  inheritable(:CropBox) || media_box
end

#crop_box=(box) ⇒ Object



79
80
81
# File 'lib/pdfrb/model/type/page.rb', line 79

def crop_box=(box)
  value[:CropBox] = box
end

#decoded_contentObject

Resolve and concatenate the page's content-stream bytes. /Contents is either a single Stream or an array of Streams.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pdfrb/model/type/page.rb', line 37

def decoded_content
  contents = value[:Contents]
  return "".b if contents.nil?

  streams = contents.is_a?(::Array) ? contents : [contents]
  streams.each_with_object(+"".b) do |ref_or_stream, buf|
    stream = document&.resolve(ref_or_stream)
    next unless stream.is_a?(Pdfrb::Model::Cos::Stream)

    buf << stream.decoded_stream
  end
end

#durationObject



118
119
120
# File 'lib/pdfrb/model/type/page.rb', line 118

def duration
  self[:Dur]
end

#each_annotationObject



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/pdfrb/model/type/page.rb', line 180

def each_annotation
  return enum_for(:each_annotation) unless block_given?
  return unless annotations && document

  arr = document.resolve(annotations)
  return unless arr.is_a?(Array) || arr.is_a?(Pdfrb::Model::PdfArray)

  arr.each do |entry|
    obj = document.resolve(entry)
    yield obj if obj
  end
end

#groupObject



150
151
152
# File 'lib/pdfrb/model/type/page.rb', line 150

def group
  self[:Group]
end

#has_annotations?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/pdfrb/model/type/page.rb', line 110

def has_annotations?
  !!annotations
end

#has_group?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/pdfrb/model/type/page.rb', line 154

def has_group?
  !!group
end

#landscape?Boolean

Returns:

  • (Boolean)


162
163
164
165
166
167
168
169
# File 'lib/pdfrb/model/type/page.rb', line 162

def landscape?
  mb = media_box
  return false unless mb.is_a?(Array) && mb.size >= 4

  width = mb[2].to_f - mb[0].to_f
  height = mb[3].to_f - mb[1].to_f
  width > height
end

#media_boxObject

Resolve inheritable /MediaBox by walking up the page tree.



51
52
53
# File 'lib/pdfrb/model/type/page.rb', line 51

def media_box
  inheritable(:MediaBox)
end

#media_box=(box) ⇒ Object



63
64
65
# File 'lib/pdfrb/model/type/page.rb', line 63

def media_box=(box)
  value[:MediaBox] = box
end

#metadataObject



138
139
140
# File 'lib/pdfrb/model/type/page.rb', line 138

def 
  self[:Metadata]
end

#output_intentsObject



146
147
148
# File 'lib/pdfrb/model/type/page.rb', line 146

def output_intents
  self[:OutputIntents]
end

#parentObject



99
100
101
102
103
104
# File 'lib/pdfrb/model/type/page.rb', line 99

def parent
  ref = value[:Parent]
  return nil unless ref && document

  document.object(ref)
end

#portrait?Boolean

Returns:

  • (Boolean)


171
172
173
174
175
176
177
178
# File 'lib/pdfrb/model/type/page.rb', line 171

def portrait?
  mb = media_box
  return false unless mb.is_a?(Array) && mb.size >= 4

  width = mb[2].to_f - mb[0].to_f
  height = mb[3].to_f - mb[1].to_f
  height > width
end

#resourcesObject



59
60
61
# File 'lib/pdfrb/model/type/page.rb', line 59

def resources
  inheritable(:Resources)
end

#rotateObject



95
96
97
# File 'lib/pdfrb/model/type/page.rb', line 95

def rotate
  inheritable(:Rotate) || 0
end

#rotated?Boolean

Returns:

  • (Boolean)


158
159
160
# File 'lib/pdfrb/model/type/page.rb', line 158

def rotated?
  rotate != 0
end

#structural_parentObject



134
135
136
# File 'lib/pdfrb/model/type/page.rb', line 134

def structural_parent
  self[:StructParents]
end

#tab_orderObject



130
131
132
# File 'lib/pdfrb/model/type/page.rb', line 130

def tab_order
  self[:Tabs]&.to_sym
end

#thumbnailObject



126
127
128
# File 'lib/pdfrb/model/type/page.rb', line 126

def thumbnail
  self[:Thumb]
end

#transitionObject



122
123
124
# File 'lib/pdfrb/model/type/page.rb', line 122

def transition
  self[:Trans]
end

#trim_boxObject



87
88
89
# File 'lib/pdfrb/model/type/page.rb', line 87

def trim_box
  value[:TrimBox]
end

#trim_box=(box) ⇒ Object



71
72
73
# File 'lib/pdfrb/model/type/page.rb', line 71

def trim_box=(box)
  value[:TrimBox] = box
end

#user_unitObject



114
115
116
# File 'lib/pdfrb/model/type/page.rb', line 114

def user_unit
  self[:UserUnit] || 1.0
end