README.md in fillable-pdf-0.1.2 vs README.md in fillable-pdf-0.5

- old
+ new

@@ -20,33 +20,79 @@ gem install fillable-pdf If you are using this gem in a script, you need to require it manually: ```ruby -require 'fillable-xml' +require 'fillable-pdf' ``` ## Usage First of all, you should open a fillable PDF file: ```ruby -pdf = FillablePDF.new('input.pdf') +pdf = FillablePDF.new 'input.pdf' ``` An instance of `FillablePDF` has the following methods at its disposal: ```ruby -pdf.has_fields? # returns true if the form has any fillable fields +# return true if the form has any fillable fields +# output example: true +pdf.any_fields? -pdf.num_fields # get the total number of fillable form fields +# get the total number of fillable form fields +# output example: 10 +pdf.num_fields -pdf.get_field('full_name') # retrieve a single field value by field name +# retrieve a single field value by field name +# output example: 'Richard' +pdf.get_field(:full_name) -pdf.set_field('first_name', 'Richard') # set a single field +# retrieve a numeric field type by field value +# numeric types should +# output example: 4 +pdf.get_field_type(:football) -pdf.set_fields({first_name: 'Richard', last_name: 'Rahl'}) # set multiple fields +# list of all field types +Field::CHECKBOX +Field::COMBO +Field::LIST +Field::NONE +Field::PUSHBUTTON +Field::RADIOBUTTON +Field::SIGNATURE +Field::TEXT + +# retrieve a hash of field name and values +# output example: {:last_name=>"Rahl", :first_name=>"Richard"} +pdf.get_fields + +# set the value of a single field by field name +# result: changes the value of 'first_name' to 'Richard' +pdf.set_field(:first_name, 'Richard') + +# set the values of multiple fields by field names +# result: changes the values of 'first_name' and 'last_name' +pdf.set_fields(first_name: 'Richard', last_name: 'Rahl') + +# rename field (i.e. change the name of the field) +# this action also moves the field to the end of the hash +# result: renames field name 'last_name' to 'surname' +pdf.rename_field(:last_name, :surname) + +# remove field (i.e. delete field and its value) +# result: physically removes field 'last_name' from document +pdf.remove_field(:last_name) + +# get an array of all field names in the document +# output example: [:first_name, :last_name] +pdf.keys + +# get an array of all field values in the document +# output example: ["Rahl", "Richard"] +pdf.values ``` Once the PDF is filled out you can either overwrite it or save it as another file: ```ruby @@ -64,10 +110,95 @@ ``` ## Example -For a fuller usage example of this gem, please see the contents of the `test` directory. +The following example [test.rb](test/test.rb) and the input file [input.pdf](test/input.pdf) are located in the `test` directory. It uses all of the methods that are described above and generates the output file [output.pdf](test/output.pdf). + +```ruby +# opening a fillable PDF +pdf = FillablePDF.new('input.pdf') + +# total number of fields +if pdf.any_fields? + puts "The form has a total of #{pdf.num_fields} fields." +else + puts 'The form is not fillable.' +end + +puts + +# setting form fields +pdf.set_fields(first_name: 'Richard', last_name: 'Rahl') +pdf.set_fields(football: 'Yes', baseball: 'Yes', + basketball: 'Yes', nascar: 'Yes', hockey: 'Yes') +pdf.set_field(:date, Time.now.strftime('%B %e, %Y')) + +# list of fields +puts "Fields hash: #{pdf.get_fields}" + +puts + +# list of field keys +puts "Keys: #{pdf.keys}" + +puts + +# list of field values +puts "Values: #{pdf.values}" + +puts + +# Checking field type +if pdf.get_field_type(:football) == Field::CHECKBOX + puts "Field 'football' is of type CHECKBOX" +else + puts "Field 'football' is not of type CHECKBOX" +end + +puts + +# Renaming field +pdf.rename_field :last_name, :surname +puts "Renamed field 'last_name' to 'surname'" +puts "New keys: #{pdf.keys}" + +puts + +# Removing field +pdf.remove_field :nascar +puts "Removed field 'nascar'" +puts "New keys: #{pdf.keys}" + +puts + +# printing the name of the person used inside the PDF +puts "Signatory: #{pdf.get_field(:first_name)} #{pdf.get_field(:last_name)}" + +# saving the filled out PDF in another file and making it non-editable +pdf.save_as('output.pdf', true) + +``` + +The example above produces the following output and also generates the output file [output.pdf](test/output.pdf). + + The form has a total of 8 fields. + + Fields hash: {:last_name=>"Rahl", :first_name=>"Richard", :football=>"Yes", :baseball=>"Yes", :basketball=>"Yes", :nascar=>"Yes", :hockey=>"Yes", :date=>"July 19, 2017"} + + Keys: [:last_name, :first_name, :football, :baseball, :basketball, :nascar, :hockey, :date] + + Values: ["Rahl", "Richard", "Yes", "Yes", "Yes", "Yes", "Yes", "July 19, 2017"] + + Field 'football' is of type CHECKBOX + + Renamed field 'last_name' to 'surname' + New keys: [:first_name, :football, :baseball, :basketball, :nascar, :hockey, :date, :surname] + + Removed field 'nascar' + New keys: [:first_name, :football, :baseball, :basketball, :hockey, :date, :surname] + + Signatory: Richard ## Contributing 1. Fork it \ No newline at end of file