Class: Rpdfium::Form::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/rpdfium/form/form.rb

Overview

Wrapper per un widget di form. Si costruisce a partire da un’annotazione di tipo :widget e l’env del documento.

Constant Summary collapse

TYPES =
{
  Raw::FPDF_FORMFIELD_UNKNOWN     => :unknown,
  Raw::FPDF_FORMFIELD_PUSHBUTTON  => :pushbutton,
  Raw::FPDF_FORMFIELD_CHECKBOX    => :checkbox,
  Raw::FPDF_FORMFIELD_RADIOBUTTON => :radiobutton,
  Raw::FPDF_FORMFIELD_COMBOBOX    => :combobox,
  Raw::FPDF_FORMFIELD_LISTBOX     => :listbox,
  Raw::FPDF_FORMFIELD_TEXTFIELD   => :textfield,
  Raw::FPDF_FORMFIELD_SIGNATURE   => :signature
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, annotation) ⇒ Field

Returns a new instance of Field.



67
68
69
70
# File 'lib/rpdfium/form/form.rb', line 67

def initialize(env, annotation)
  @env = env
  @annotation = annotation
end

Instance Attribute Details

#annotationObject (readonly)

Returns the value of attribute annotation.



65
66
67
# File 'lib/rpdfium/form/form.rb', line 65

def annotation
  @annotation
end

#envObject (readonly)

Returns the value of attribute env.



65
66
67
# File 'lib/rpdfium/form/form.rb', line 65

def env
  @env
end

Instance Method Details

#checked?Boolean

Per checkbox e radio

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/rpdfium/form/form.rb', line 93

def checked?
  return false unless %i[checkbox radiobutton].include?(type)

  Raw.FPDFAnnot_IsChecked(@env.handle, @annotation.handle) == 1
end

#flagsObject



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

def flags
  Raw.FPDFAnnot_GetFormFieldFlags(@env.handle, @annotation.handle)
end

#nameObject



76
77
78
# File 'lib/rpdfium/form/form.rb', line 76

def name
  Raw.read_utf16_string(:FPDFAnnot_GetFormFieldName, @env.handle, @annotation.handle)
end

#optionsObject

Per combobox/listbox



100
101
102
103
104
105
106
107
108
# File 'lib/rpdfium/form/form.rb', line 100

def options
  n = Raw.FPDFAnnot_GetOptionCount(@env.handle, @annotation.handle)
  return [] if n <= 0

  Array.new(n) do |i|
    Raw.read_utf16_string(:FPDFAnnot_GetOptionLabel,
                          @env.handle, @annotation.handle, i)
  end
end

#readonly?Boolean

PDF spec §12.7.4.1: bit 1=read-only, bit 2=required, bit 3=no-export

Returns:

  • (Boolean)


89
# File 'lib/rpdfium/form/form.rb', line 89

def readonly?; (flags & (1 << 0)).positive?; end

#required?Boolean

Returns:

  • (Boolean)


90
# File 'lib/rpdfium/form/form.rb', line 90

def required?; (flags & (1 << 1)).positive?; end

#to_hObject



110
111
112
113
114
115
116
117
118
# File 'lib/rpdfium/form/form.rb', line 110

def to_h
  {
    name: name, type: type, value: value,
    readonly: readonly?, required: required?,
    checked: (%i[checkbox radiobutton].include?(type) ? checked? : nil),
    options: (%i[combobox listbox].include?(type) ? options : nil),
    bbox: @annotation.bbox
  }.compact
end

#typeObject



72
73
74
# File 'lib/rpdfium/form/form.rb', line 72

def type
  TYPES[Raw.FPDFAnnot_GetFormFieldType(@env.handle, @annotation.handle)] || :unknown
end

#valueObject



80
81
82
# File 'lib/rpdfium/form/form.rb', line 80

def value
  Raw.read_utf16_string(:FPDFAnnot_GetFormFieldValue, @env.handle, @annotation.handle)
end