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) i.next();
  System.out.print(key + ": ");
  switch(form.getFieldType(key)) {
    case AcroFields.FIELD_TYPE_CHECKBOX:
      System.out.println("Checkbox");
      break;
    case... (other types)
  }
}
TIA,
Dan
-- 
Posted via http://www.ruby-forum.com/.
Benjamin Curtis
2009-May-26  19:29 UTC
Re: List all field names on a PDF using iText from Rails
Check out this code: http://github.com/jaywhy/pdf-stamper/tree/master I haven''t used it, as it wasn''t around when I needed it, so I wrote my own, but this code looks very similar to what I wrote, so it may work well for what you need. The highlighted line in this link shows how the fields can be listed: http://github.com/jaywhy/pdf-stamper/blob/3f1f6bd7867994bd9124b8b1ffa613007903d22a/lib/pdf/stamper/rjb.rb#L35 -- Benjamin Curtis http://railskits.com/ - Ready-made Rails code http://catchthebest.com/ - Team-powered recruiting http://www.bencurtis.com/ - Personal blog On Sun, May 24, 2009 at 11:59 PM, Dan Sadaka < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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) i.next(); > System.out.print(key + ": "); > switch(form.getFieldType(key)) { > case AcroFields.FIELD_TYPE_CHECKBOX: > System.out.println("Checkbox"); > break; > case... (other types) > } > } > > TIA, > Dan > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Benjamin,
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
  end
  def output
    template = params[:pdffile]
    logger.info("Here in listfields.output with PDFfile
=>#{template}")
    filestream   = Rjb::import(''java.io.FileOutputStream'')
    acrofields   =
Rjb::import(''com.lowagie.text.pdf.AcroFields'')
    pdfreader    =
Rjb::import(''com.lowagie.text.pdf.PdfReader'')
    treemap       = Rjb::import(''java.util.TreeMap'')
    reader = pdfreader.new( template )
    @form = reader.getAcroFields()
    @fields = @form.getFields()
    fs = treemap.new(@fields)
    @k = fs.keySet()  # @k gets sorted list of field names
  end
end
*------------------------------------------------------------------------
and the view:
*------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
Field Name|Type
<p> </p>
<PRE>
  <% itr = @k.iterator()
       while itr.hasNext()
  %>
  <%= "t.string : #{itr.next().to_string}" -%>
  <% end %>
</PRE>
</body>
</html>
*------------------------------------------------------------------------
HTH,
Dan
Attachments:
http://www.ruby-forum.com/attachment/3737/listfields_controller.rb
-- 
Posted via http://www.ruby-forum.com/.