Class: CorpPdf::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/corp_pdf/field.rb

Overview

Represents a PDF form field

Constant Summary collapse

TYPES =
{
  text: "/Tx",
  button: "/Btn",
  checkbox: "/Btn",
  radio: "/Btn",
  choice: "/Ch",
  signature: "/Sig"
}.freeze
TYPE_KEYS =

Reverse lookup: map type strings to symbol keys

TYPES.invert.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, type, ref, document = nil, position = {}) ⇒ Field

Returns a new instance of Field.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/corp_pdf/field.rb', line 21

def initialize(name, value, type, ref, document = nil, position = {})
  @name = name
  @value = value
  # Normalize type: accept symbol keys or type strings, default to "/Tx"
  normalized_type = if type.is_a?(Symbol)
                      TYPES[type] || "/Tx"
                    else
                      type.to_s.strip
                    end
  @type = normalized_type.empty? ? "/Tx" : normalized_type
  @ref = ref
  @document = document
  @x = position[:x]
  @y = position[:y]
  @width = position[:width]
  @height = position[:height]
  @page = position[:page]
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



7
8
9
# File 'lib/corp_pdf/field.rb', line 7

def height
  @height
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/corp_pdf/field.rb', line 7

def name
  @name
end

#pageObject (readonly)

Returns the value of attribute page.



7
8
9
# File 'lib/corp_pdf/field.rb', line 7

def page
  @page
end

#refObject (readonly)

Returns the value of attribute ref.



7
8
9
# File 'lib/corp_pdf/field.rb', line 7

def ref
  @ref
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/corp_pdf/field.rb', line 7

def type
  @type
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/corp_pdf/field.rb', line 6

def value
  @value
end

#widthObject (readonly)

Returns the value of attribute width.



7
8
9
# File 'lib/corp_pdf/field.rb', line 7

def width
  @width
end

#xObject (readonly)

Returns the value of attribute x.



7
8
9
# File 'lib/corp_pdf/field.rb', line 7

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



7
8
9
# File 'lib/corp_pdf/field.rb', line 7

def y
  @y
end

Instance Method Details

#==(other) ⇒ Object

Equality comparison



81
82
83
84
85
86
87
88
# File 'lib/corp_pdf/field.rb', line 81

def ==(other)
  return false unless other.is_a?(Field)

  name == other.name &&
    value == other.value &&
    type == other.type &&
    ref == other.ref
end

#button_field?Boolean

Check if this is a button field (checkbox/radio)

Returns:

  • (Boolean)


46
47
48
# File 'lib/corp_pdf/field.rb', line 46

def button_field?
  type == "/Btn"
end

#choice_field?Boolean

Check if this is a choice field (dropdown/list)

Returns:

  • (Boolean)


51
52
53
# File 'lib/corp_pdf/field.rb', line 51

def choice_field?
  type == "/Ch"
end

#generationObject

Get the generation number (second element of ref)



71
72
73
# File 'lib/corp_pdf/field.rb', line 71

def generation
  ref[1]
end

#has_position?Boolean

Check if position is known

Returns:

  • (Boolean)


106
107
108
# File 'lib/corp_pdf/field.rb', line 106

def has_position?
  !x.nil? && !y.nil? && !width.nil? && !height.nil?
end

#has_value?Boolean

Check if the field has a value

Returns:

  • (Boolean)


61
62
63
# File 'lib/corp_pdf/field.rb', line 61

def has_value?
  !value.nil? && !value.to_s.empty?
end

#object_numberObject

Get the object number (first element of ref)



66
67
68
# File 'lib/corp_pdf/field.rb', line 66

def object_number
  ref[0]
end

#removeObject

Remove this field from the AcroForm /Fields array and mark the field object as deleted. Note: This does not purge page /Annots widgets (non-trivial); most viewers will hide the field once it is no longer in the field tree. Returns true if the field was removed.



137
138
139
140
141
142
143
# File 'lib/corp_pdf/field.rb', line 137

def remove
  return false unless @document
  return false unless valid_ref?

  action = Actions::RemoveField.new(@document, self)
  action.call
end

#signature_field?Boolean

Check if this is a signature field

Returns:

  • (Boolean)


56
57
58
# File 'lib/corp_pdf/field.rb', line 56

def signature_field?
  type == "/Sig"
end

#text_field?Boolean

Check if this is a text field

Returns:

  • (Boolean)


41
42
43
# File 'lib/corp_pdf/field.rb', line 41

def text_field?
  type == "/Tx"
end

#to_sObject Also known as: inspect

String representation for debugging



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/corp_pdf/field.rb', line 91

def to_s
  type_str = type.inspect
  type_str += " (:#{type_key})" if type_key
  pos_str = if x && y && width && height
              " x=#{x} y=#{y} w=#{width} h=#{height}"
            else
              " position=(unknown)"
            end
  page_str = page ? " page=#{page}" : ""
  "#<CorpPdf::Field name=#{name.inspect} type=#{type_str} value=#{value.inspect} ref=#{ref.inspect}#{pos_str}#{page_str}>"
end

#type_keyObject

Get the symbol key for the field type (e.g., :text for “/Tx”) Returns nil if the type is not in the TYPES mapping



112
113
114
# File 'lib/corp_pdf/field.rb', line 112

def type_key
  TYPE_KEYS[type]
end

#update(new_value, new_name: nil) ⇒ Object

Update this field’s value and optionally rename it in the document Returns true if the field was found and queued for write.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/corp_pdf/field.rb', line 118

def update(new_value, new_name: nil)
  return false unless @document
  return false unless valid_ref?

  action = Actions::UpdateField.new(@document, @name, new_value, new_name: new_name)
  result = action.call

  # Update the local value if update was successful
  @value = new_value if result
  # Update the local name if rename was successful
  @name = new_name if result && new_name && !new_name.empty?

  result
end

#valid_ref?Boolean

Check if field reference is valid (not [-1, 0] placeholder)

Returns:

  • (Boolean)


76
77
78
# File 'lib/corp_pdf/field.rb', line 76

def valid_ref?
  ref != [-1, 0]
end