Class: PDF::Extract::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, reference_resoler) ⇒ Field

Returns a new instance of Field.



7
8
9
10
# File 'lib/pdf/extract/field.rb', line 7

def initialize(data, reference_resoler)
  @data = data
  @reference_resoler = reference_resoler
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/pdf/extract/field.rb', line 5

def data
  @data
end

#reference_resolerObject (readonly)

Returns the value of attribute reference_resoler.



5
6
7
# File 'lib/pdf/extract/field.rb', line 5

def reference_resoler
  @reference_resoler
end

Instance Method Details

#as_jsonObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/pdf/extract/field.rb', line 63

def as_json
  h = {
    "name" => name,
    "value" => value
  }

  image.tap { |i| h["image"] = i if i }

  h
end

#imageObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pdf/extract/field.rb', line 30

def image
  # PDF Reference 6th Edition, Version 1.7, November 2006 page 641
  # MK: An appearance characteristics dictionary (see Table 8.40) to be used in constructing
  # a dynamic appearance stream specifying the annotation’s visual presentation on the page.
  # The name MK for this entry is of historical significance only and has no direct meaning.
  #
  # PDF Reference 6th Edition, Version 1.7, November 2006 page 1118
  # Implementation Notes
  # If the MK entry is present in the field’s widget annotation dictionary (see Table 8.39),
  # Acrobat viewers regenerate the entire XObject appearance stream. If MK is not present,
  # the contents of the stream outside /Tx BMC ... EMC are preserved.
  mk = data[:MK] || {}

  mk = mk.is_a?(PDF::Reader::Reference) ? reference_resoler.lookup(mk) : mk

  # PDF Reference 6th Edition, Version 1.7, November 2006 page 642
  # I: A form XObject defining the widget annotation’s normal icon, displayed when it is not
  # interacting with the user.
  stream = reference_resoler.lookup(mk[:I])&.hash || {}

  # PDF Reference 6th Edition, Version 1.7, November 2006 page 358
  # form dictionary
  resources = reference_resoler.lookup(stream[:Resources]) || {}

  xobject = resources[:XObject] || {}

  stream = reference_resoler.lookup(xobject[:Im1])

  data = stream&.data

  data ? Base64.encode64(data) : nil
end

#nameObject

PDF Reference 6th Edition, Version 1.7, November 2006 page 675 The partial field name



14
15
16
# File 'lib/pdf/extract/field.rb', line 14

def name
  data[:T]
end

#typeObject

PDF Reference 6th Edition, Version 1.7, November 2006 page 675 The type of field that this dictionary describes.



20
21
22
# File 'lib/pdf/extract/field.rb', line 20

def type
  data[:FT]
end

#valueObject

PDF Reference 6th Edition, Version 1.7, November 2006 page 676 The field’s value, whose format varies depending on the field type.



26
27
28
# File 'lib/pdf/extract/field.rb', line 26

def value
  data[:V]
end