Class: PDFRavager::Strategies::XFA

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf_ravager/strategies/xfa.rb

Instance Method Summary collapse

Constructor Details

#initialize(stamper) ⇒ XFA

Returns a new instance of XFA.



4
5
6
# File 'lib/pdf_ravager/strategies/xfa.rb', line 4

def initialize(stamper)
  @xfa = stamper.getAcroFields.getXfa
end

Instance Method Details

#set_field_values(template) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pdf_ravager/strategies/xfa.rb', line 8

def set_field_values(template)
  doc = to_nokogiri_xml
  template.fields.select{|f| f.respond_to?(:set_xfa_value)}.each do |f|
    # first, assume the user-provided field name is an xpath and use it directly:
    strict_match =
      begin
        doc.xpath(f.xfa_name)
      rescue Nokogiri::XML::XPath::SyntaxError
        []
      end
    if strict_match.any?
      strict_match.each{|node| f.set_xfa_value(node) }
    else
      # otherwise, we'll loosely match the field name anywhere in the document:
      loose_match = doc.xpath("//*[local-name()='field'][@name='#{f.xfa_name}']")
      loose_match.each{|node| f.set_xfa_value(node) }
    end
  end
  @xfa.setDomDocument(doc.to_java)
  @xfa.setChanged(true)
end

#set_read_onlyObject



30
31
32
33
34
35
36
37
# File 'lib/pdf_ravager/strategies/xfa.rb', line 30

def set_read_only
  doc = to_nokogiri_xml
  doc.xpath("//*[local-name()='field']").each do |node|
    node["access"] = "readOnly"
  end
  @xfa.setDomDocument(doc.to_java)
  @xfa.setChanged(true)
end