OK, I changed up the code.  This code does work.
 
I still may need to change the font size on the text field and what-not.
 
-------------
    public static final ByteArrayOutputStream generatePDFDocumentBytes()

        throws DocumentException, IOException {
                
        ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();

        final PdfReader reader  = new PdfReader(PATH);
        Rectangle psize = reader.getPageSizeWithRotation(1);
        float height = psize.height();
                
        PdfStamper stamp = new PdfStamper(reader, baosPDF);       
        AcroFields form = stamp.getAcroFields();
        HashMap formFields = form.getFields();
        System.out.println(formFields.size());
        
        for (Iterator it = formFields.entrySet().iterator();
it.hasNext(); ) {
            
            Map.Entry e = (Map.Entry) it.next();
            final String fieldName = (String) e.getKey();
            System.out.println("==>" + fieldName);
            System.out.println("==>" + e.getKey() + " // " +
e.getValue());
                        
            form.setField(fieldName, "New Fields");
        }
        
        stamp.setFormFlattening(true);
        stamp.close();
                
        return baosPDF;
    }

________________________________

From: Mark Storer [mailto:msto...@autonomy.com] 
Sent: Monday, October 19, 2009 4:20 PM
To: Post all your questions about iText here
Subject: Re: [iText-questions] Modifying the value of forms in a PDF
document, templating


>  I am trying to do some kind of "fill in" a predefined form but
apparently this doesn't exist with iText.
Incorrect.  There are several ways of filling in a form with iText.
 
myAcroFields.setField(Name, value)
            .setField(name, value, displayVal )
            .setFields( myFdfReader )
            .setFields( myXFdfReader )
 
And finally (and I wouldn't be at all surprised to find that this is the
one you'll need) 
 
myAcroFields.mergeXfaData( node ); // xml
 
XFA-based forms (forms built with LifeCycle Designer) are almost
completely different from those using the "normal" AcroForm).  There are
quite a few things that are all-but-impossible for iText given an
XFA-based form.
 
You can change whatever you like about existing fields with iText,
though some tasks are Much More Difficult than others.  Setting values
is easy, there's direct support for that.  Changing border/fill/font
settings:  Not so easy.  You need to directly manipulate the low-level
PdfDictionary/PdfArray/etc objects to get the results you want.  And to
know what you need to change, you need to be pretty familiar with the
PDF Spec.
 
I'm not sure what you're trying to accomplish with your code however.
PdfStamper doesn't play well with Document.add().  "At all" would be
more accurate.  You can do whatever you like to that document & writer,
but getting those changes into your stamper instance requires you to
save out the PDF, and re-open it with a new reader.  Yuck.
 
Also, you said you got an error when you closed the document, but didn't
tell us what the error was.  Help us help you.  We're not psychic (just
mind bogglingly talented, good-looking, and modest... did I mention
"talented"?).
 
--Mark Storer 
  Senior Software Engineer 
  Cardiff.com

#include <disclaimer> 
typedef std::Disclaimer<Cardiff> DisCard; 

        -----Original Message-----
        From: Brown, Berlin [GCG-PFS]
[mailto:berlin.br...@primerica.com]
        Sent: Monday, October 19, 2009 12:02 PM
        To: itext-questions@lists.sourceforge.net
        Cc: Berlin Brown
        Subject: [iText-questions] Modifying the value of forms in a PDF
document,templating
        
        
         I am trying to do some kind of "fill in" a predefined form but
apparently this doesn't exist with iText.
         
        So, it seems like a pretty popular approach is to add form
fields/text boxes with in the PDF document read in the document and set
those values.  I have two questions.  First question, what is the best
way to do this.  Also, can I change the visual aspects of the text box.
Can I increase the font, possibly disable the border around the edit
box.
         
        Here is the code I have.  I am able to read the document and
write it back out.  But I get an error when I resave the document.
         
            public static final ByteArrayOutputStream
generatePDFDocumentBytes() 
                throws DocumentException, IOException {         
                
                Document document = null;
                PdfWriter writer = null;
                        
                ByteArrayOutputStream baosPDF = new
ByteArrayOutputStream();
                
                final PdfReader reader  = new PdfReader(PATH);
                Rectangle psize = reader.getPageSizeWithRotation(1);
                float height = psize.height();
                int rotation = reader.getPageRotation(1);
                
                document = new Document(psize, 0,0, 0,0);
                writer = PdfWriter.getInstance(document, baosPDF);
                document.open();
                
                document.newPage();
                PdfImportedPage page = writer.getImportedPage(reader,1);
                PdfContentByte cb = writer.getDirectContent();
                cb.addTemplate(page, 0, -1f, 1f, 0, 0, height);
               
                PdfStamper stamp = new PdfStamper(reader, baosPDF);
               
                AcroFields form = stamp.getAcroFields();
                HashMap formFields = form.getFields();
                System.out.println(formFields.size());
                
                for (Iterator it = formFields.entrySet().iterator();
it.hasNext(); ) {
                    
                    Map.Entry e = (Map.Entry) it.next();
                    final String fieldName = (String) e.getKey();
                    System.out.println("==>" + fieldName);
                    System.out.println("==>" + e.getKey() + " // " +
e.getValue());
                                
                    //form.setField(fieldName, "New");
                }
                
                stamp.setFormFlattening(true);
                stamp.close();
                
                document.close();
                return baosPDF;
            }
                

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to