Class: HexaPDF::Type::Form

Inherits:
Stream show all
Defined in:
lib/hexapdf/type/form.rb

Overview

Represents a form XObject of a PDF document.

See: PDF1.7 s8.10

Constant Summary

Constants included from DictionaryFields

DictionaryFields::Boolean, DictionaryFields::PDFByteString, DictionaryFields::PDFDate

Instance Attribute Summary collapse

Attributes inherited from Object

#data, #document, #must_be_indirect

Instance Method Summary collapse

Methods inherited from Stream

#must_be_indirect?, #raw_stream, #set_filter, #stream, #stream=, #stream_decoder, #stream_encoder, #stream_source

Methods inherited from Dictionary

#[], #[]=, define_field, define_type, #delete, #each, each_field, #empty?, field, #key?, #to_h, type, #type

Methods inherited from Object

#<=>, #==, #cache, #cached?, #clear_cache, deep_copy, #deep_copy, #document?, #eql?, #gen, #gen=, #hash, #indirect?, #initialize, #inspect, #must_be_indirect?, #null?, #oid, #oid=, #type, #validate, #value, #value=

Constructor Details

This class inherits a constructor from HexaPDF::Object

Instance Attribute Details

#source_pathObject

Returns the path to the PDF file that was used when creating the form object.

This value is only set when the form object was created by using the image loading facility (i.e. when treating a single page PDF file as image) and not when the form object was created in any other way (i.e. manually created or already part of a loaded PDF file).



71
72
73
# File 'lib/hexapdf/type/form.rb', line 71

def source_path
  @source_path
end

Instance Method Details

#boxObject

Returns the rectangle defining the bounding box of the form.



74
75
76
# File 'lib/hexapdf/type/form.rb', line 74

def box
  self[:BBox]
end

#canvasObject

Returns the canvas for the form XObject.

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 bounding box of the form XObject 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 bounding box.

Note that a canvas can only be retrieved for initially empty form XObjects!



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/hexapdf/type/form.rb', line 139

def canvas
  cache(:canvas) do
    unless stream.empty?
      raise HexaPDF::Error, "Cannot create a canvas for a form XObjects with contents"
    end

    canvas = Content::Canvas.new(self)
    if box.left != 0 || box.bottom != 0
      canvas.save_graphics_state.translate(box.left, box.bottom)
    end
    self.stream = canvas.stream_data
    set_filter(:FlateDecode)
    canvas
  end
end

#contentsObject

Returns the contents of the form XObject.

Note: This is the same as #stream but here for interface compatibility with Page.



91
92
93
# File 'lib/hexapdf/type/form.rb', line 91

def contents
  stream
end

#contents=(data) ⇒ Object

Replaces the contents of the form XObject with the given string.

This also clears the cache to avoid returning invalid objects.

Note: This is the same as #stream= but here for interface compatibility with Page.



100
101
102
103
# File 'lib/hexapdf/type/form.rb', line 100

def contents=(data)
  self.stream = data
  clear_cache
end

#heightObject

Returns the height of the bounding box (see #box).



84
85
86
# File 'lib/hexapdf/type/form.rb', line 84

def height
  box.height
end

#process_contents(processor, original_resources: nil) ⇒ Object

Processes the content stream of the form XObject with the given processor object.

The original_resources argument has to be set to a page’s resources if this form XObject is processed as part of this page.

See: HexaPDF::Content::Processor



117
118
119
120
121
122
123
124
125
126
# File 'lib/hexapdf/type/form.rb', line 117

def process_contents(processor, original_resources: nil)
  processor.resources = if self[:Resources]
                          self[:Resources]
                        elsif original_resources
                          original_resources
                        else
                          document.wrap({}, type: :XXResources)
                        end
  Content::Parser.parse(contents, processor)
end

#resourcesObject

Returns the resource dictionary which is automatically created if it doesn’t exist.



106
107
108
109
# File 'lib/hexapdf/type/form.rb', line 106

def resources
  self[:Resources] ||= document.wrap({ProcSet: [:PDF, :Text, :ImageB, :ImageC, :ImageI]},
                                     type: :XXResources)
end

#widthObject

Returns the width of the bounding box (see #box).



79
80
81
# File 'lib/hexapdf/type/form.rb', line 79

def width
  box.width
end