lib/hexapdf/cli/form.rb in hexapdf-0.16.0 vs lib/hexapdf/cli/form.rb in hexapdf-0.17.0
- old
+ new
@@ -2,11 +2,11 @@
#
#--
# This file is part of HexaPDF.
#
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
-# Copyright (C) 2014-2020 Thomas Leitner
+# Copyright (C) 2014-2021 Thomas Leitner
#
# HexaPDF is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3 as
# published by the Free Software Foundation with the addition of the
# following permission added to Section 15 as permitted in Section 7(a):
@@ -106,16 +106,14 @@
fill_form_with_template(doc)
else
fill_form(doc)
end
end
- if @flatten
- unless doc.acro_form.flatten.empty?
- $stderr.puts "Warning: Not all form fields could be flattened"
- doc.catalog.delete(:AcroForm)
- doc.delete(doc.acro_form)
- end
+ if @flatten && !doc.acro_form.flatten.empty?
+ $stderr.puts "Warning: Not all form fields could be flattened"
+ doc.catalog.delete(:AcroForm)
+ doc.delete(doc.acro_form)
end
else
list_form_fields(doc)
end
end
@@ -144,12 +142,12 @@
end
puts " └─ #{field.field_value.inspect}"
if command_parser.verbosity_info?
if field.field_type == :Ch
puts " └─ Options: #{field.option_items.map(&:inspect).join(', ')}"
- elsif concrete_field_type == :radio_button
- puts " └─ Options: #{field.radio_button_values.map(&:inspect).join(', ')}"
+ elsif concrete_field_type == :radio_button || concrete_field_type == :check_box
+ puts " └─ Options: #{([:Off] + field.allowed_values).map(&:to_s).join(', ')}"
end
end
end
end
@@ -168,15 +166,17 @@
puts " #{field_name}"
puts " └─ Current value: #{field.field_value.inspect}"
if field.field_type == :Ch
- puts " └─ Possible values: #{field.option_items.map(&:inspect).join(', ')}"
+ puts " └─ Possible values: #{field.option_items.map(&:to_s).join(', ')}"
elsif concrete_field_type == :radio_button
- puts " └─ Possible values: #{field.radio_button_values.map(&:inspect).join(', ')}"
+ puts " └─ Possible values: Off, #{field.allowed_values.map(&:to_s).join(', ')}"
elsif concrete_field_type == :check_box
- puts " └─ Possible values: y(es), t(rue); n(o), f(alse)"
+ av = field.allowed_values
+ puts " └─ Possible values: n(o), f(alse); #{av.size == 1 ? 'y(es), t(rue); ' : ''}" \
+ "#{av.map(&:to_s).join(', ')}"
end
begin
print " └─ New value: "
value = $stdin.readline.chomp
@@ -218,40 +218,41 @@
end
# Applies the given value to the field.
def apply_field_value(field, value)
case field.concrete_field_type
- when :single_line_text_field
+ when :single_line_text_field, :multiline_text_field, :comb_text_field, :combo_box,
+ :list_box, :editable_combo_box
field.field_value = value
- when :combo_box, :list_box
- field.field_value = value
- when :editable_combo_box
- field.field_value = value
when :check_box
- unless value.match?(/y(es)?|t(rue)?|f(alse)?|n(o)/)
- raise HexaPDF::Error, "Invalid input, use one of the possible values"
- end
- field.field_value = value.match?(/y(es)?|t(rue)?/)
+ field.field_value = case value
+ when /y(es)?|t(rue)?/
+ true
+ when /n(o)?|f(alse)?/
+ false
+ else
+ value
+ end
when :radio_button
- field.field_value = value.intern
+ field.field_value = value.to_sym
else
- raise "Field type not yet supported"
+ raise "Field type #{field.concrete_field_type} not yet supported"
end
end
# Iterates over all non-push button fields in page order. If a field appears on multiple
# pages, it is only yielded on the first page.
def each_field(doc) # :yields: page, page_index, field
seen = {}
doc.pages.each_with_index do |page, page_index|
- page[:Annots]&.each do |annotation|
+ page.each_annotation do |annotation|
next unless annotation[:Subtype] == :Widget
field = annotation.form_field
next if field.concrete_field_type == :push_button
- unless seen[field]
+ unless seen[field.full_field_name]
yield(page, page_index, field, annotation)
- seen[field] = true
+ seen[field.full_field_name] = true
end
end
end
end