Forum: Ruby on Rails List all field names on a PDF using iText from Rails

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Dan S. (Guest)
on 2009-05-25 10:59
Hello everyone,

Has anyone used iText to list all fields on a PDF AcroForm?
There are plenty of java examples to do it, but I do not know how to
translate that into ruby.  I'm using Rjb.

The Java example I've found is:

AcroFields form = reader.getAcroFields();
HashMap fields = form.getFields();
String key;
for (Iterator i = fields.keySet().iterator(); i.hasNext(); ) {
  key = (String);
  System.out.print(key + ": ");
  switch(form.getFieldType(key)) {
    case AcroFields.FIELD_TYPE_CHECKBOX:
    case... (other types)

Benjamin C. (Guest)
on 2009-05-26 23:30
(Received via mailing list)
Check out this code:
I haven't used it, as it wasn't around when I needed it, so I wrote my
but this code looks very similar to what I wrote, so it may work well
what you need.

The highlighted line in this link shows how the fields can be listed:

Benjamin C. - Ready-made Rails code - Team-powered recruiting - Personal blog

On Sun, May 24, 2009 at 11:59 PM, Dan S. <
Dan S. (Guest)
on 2009-05-27 05:39
Attachment: listfields_controller.rb (0 Bytes)

Thanks for the tip.

Here is the complete code.  The big hangup was the .to_string.  I kept
trying to use .to_s thinking Rjb would translate for me. NOT!

Note that the output can be cut and pasted into a db migration file.
class ListfieldsController < ApplicationController
#  require 'lib/pdfstamper'
  protect_from_forgery :only => [:create, :update, :destroy]

  def index

  def output
    template = params[:pdffile]"Here in listfields.output with PDFfile =>#{template}")
    filestream   = Rjb::import('')
    acrofields   = Rjb::import('com.lowagie.text.pdf.AcroFields')
    pdfreader    = Rjb::import('com.lowagie.text.pdf.PdfReader')
    treemap       = Rjb::import('java.util.TreeMap')

    reader = template )
    @form = reader.getAcroFields()
    @fields = @form.getFields()
    fs =
    @k = fs.keySet()  # @k gets sorted list of field names

and the view:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

Field Name|Type
  <% itr = @k.iterator()
       while itr.hasNext()
  <%= "t.string : #{}" -%>
  <% end %>

This topic is locked and can not be replied to.