Modifying the value of forms in a PDF document, templating

View: New views
9 Messages — Rating Filter:   Alert me  

Modifying the value of forms in a PDF document, templating

by berlinbrown :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

 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@...
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/

Re: Modifying the value of forms in a PDF document, templating

by Mark Storer-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>  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.Brown@...]
Sent: Monday, October 19, 2009 12:02 PM
To: itext-questions@...
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@...
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/

Re: Modifying the value of forms in a PDF document, templating

by berlinbrown :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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:MStorer@...]
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.Brown@...]
Sent: Monday, October 19, 2009 12:02 PM
To: itext-questions@...
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@...
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/

Re: Modifying the value of forms in a PDF document, update!

by berlinbrown :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

OK, I am able to open a PDF document, find the form/text fields that I need and then set those values.  Now I want to be able to change the font size and alignment of that text.  And additional additional text.  I used some code 'addText' that I found online.  I am able to add the new text but the position is off.
 
I used the get field position call to get the positions but they don't seem to lie on top of the text field.  Essentially, with the code below, my new text is not even close to where I want it.  If you look at the hard coded values,
 
ct.setSimpleColumn(f[1], f[2]-300, f[3]+ 100, f[4]-300);
I don't mean to substract some arbitrary value 300 from the height. 
 
Here is the original code that I modified.
--------------- Code: 
 
     @SuppressWarnings("unchecked")
    public static boolean addText(String s, PdfContentByte canvas, float[] f, float size, boolean simulate) throws DocumentException, IOException {
       
        // PdfContentByte is an object containing the user positioned text and graphic contents of a page.
        // It knows how to apply the proper font encoding.        
        StyleSheet styles = new StyleSheet();
        /*
        styles.loadTagStyle("p", "size", size + "px");
        styles.loadTagStyle("p", "align", "justify");
        styles.loadTagStyle("p", "hyphenation", "en_us");
        */
        ArrayList<Element> objects = HTMLWorker.parseToList(new StringReader(s), styles);
        ColumnText ct = new ColumnText(canvas);
        ct.setAlignment(Element.ALIGN_CENTER);
        ct.setLeading(size * 1.2f);
        ///////////////////////////////
        /// __NOTE__:2
        /// THIS IS THE CODE I AM HAVING AN ISSUE WITH
        /// THE TEXT SHOULD! DISPLAY BY THE TEXT BOX
        ///////////////////////////////
        //ct.setSimpleColumn(f[1] + 2, f[2] + 2, f[3] - 2, f[4]); 
        //float llx, float lly, float urx, float ury, int alignment)
        ct.setSimpleColumn(f[1], f[2]-300, f[3]+ 100, f[4]-300);
        for (Element element : objects) {           
            final Chunk cz = new Chunk("abc!!!!!", F1);
            cz.setFont(F1);
            final Phrase jj = new Phrase(cz);
            ct.addElement(jj);
        }
        ct.go(simulate);
        return true;
    }
 
   
    public static final ByteArrayOutputStream generatePDFDocumentBytes()
        throws DocumentException, IOException {
               
        ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();       
        final PdfReader reader  = new PdfReader(CERT_PATH);
               
        PdfStamper stamp = new PdfStamper(reader, baosPDF);      
        AcroFields form = stamp.getAcroFields();
        HashMap formFields = form.getFields();
        System.out.println(formFields.size());
       
        // PdfContentByte is an object containing the user positioned text and graphic contents of a page.
        // It knows how to apply the proper font encoding.
        PdfContentByte canvas = stamp.getOverContent(1);       
       
        float[] f = form.getFieldPositions("Text2");       
        System.out.println("===>" + f.length + " // " + f[0] + ", " + f[1] + ", " + f[2] + ", " + f[3] + ", " + f[4]);       
        addText("<p>__Test__ Again!!</p>", canvas, f, 8, false);
       
        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());
            /// __NOTE__:3 HERE THE TEXT IS CHANGED ON THE FORM (that is OK)
            form.setField(fieldName, "simple text");
        }
                       
        stamp.setFormFlattening(true);
        stamp.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@...
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/

Re: Modifying the value of forms in a PDFdocument, update!

by Mark Storer-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can we see your source PDF file?
 

--Mark Storer
  Senior Software Engineer
  Cardiff.com

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

-----Original Message-----
From: Brown, Berlin [GCG-PFS] [mailto:Berlin.Brown@...]
Sent: Tuesday, October 20, 2009 1:35 PM
To: itext-questions@...
Cc: Berlin Brown
Subject: Re: [iText-questions] Modifying the value of forms in a PDFdocument, update!

OK, I am able to open a PDF document, find the form/text fields that I need and then set those values.  Now I want to be able to change the font size and alignment of that text.  And additional additional text.  I used some code 'addText' that I found online.  I am able to add the new text but the position is off.
 
I used the get field position call to get the positions but they don't seem to lie on top of the text field.  Essentially, with the code below, my new text is not even close to where I want it.  If you look at the hard coded values,
 
ct.setSimpleColumn(f[1], f[2]-300, f[3]+ 100, f[4]-300);
I don't mean to substract some arbitrary value 300 from the height. 
 
Here is the original code that I modified.
--------------- Code: 
 
     @SuppressWarnings("unchecked")
    public static boolean addText(String s, PdfContentByte canvas, float[] f, float size, boolean simulate) throws DocumentException, IOException {
       
        // PdfContentByte is an object containing the user positioned text and graphic contents of a page.
        // It knows how to apply the proper font encoding.        
        StyleSheet styles = new StyleSheet();
        /*
        styles.loadTagStyle("p", "size", size + "px");
        styles.loadTagStyle("p", "align", "justify");
        styles.loadTagStyle("p", "hyphenation", "en_us");
        */
        ArrayList<Element> objects = HTMLWorker.parseToList(new StringReader(s), styles);
        ColumnText ct = new ColumnText(canvas);
        ct.setAlignment(Element.ALIGN_CENTER);
        ct.setLeading(size * 1.2f);
        ///////////////////////////////
        /// __NOTE__:2
        /// THIS IS THE CODE I AM HAVING AN ISSUE WITH
        /// THE TEXT SHOULD! DISPLAY BY THE TEXT BOX
        ///////////////////////////////
        //ct.setSimpleColumn(f[1] + 2, f[2] + 2, f[3] - 2, f[4]); 
        //float llx, float lly, float urx, float ury, int alignment)
        ct.setSimpleColumn(f[1], f[2]-300, f[3]+ 100, f[4]-300);
        for (Element element : objects) {           
            final Chunk cz = new Chunk("abc!!!!!", F1);
            cz.setFont(F1);
            final Phrase jj = new Phrase(cz);
            ct.addElement(jj);
        }
        ct.go(simulate);
        return true;
    }
 
   
    public static final ByteArrayOutputStream generatePDFDocumentBytes()
        throws DocumentException, IOException {
               
        ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();       
        final PdfReader reader  = new PdfReader(CERT_PATH);
               
        PdfStamper stamp = new PdfStamper(reader, baosPDF);      
        AcroFields form = stamp.getAcroFields();
        HashMap formFields = form.getFields();
        System.out.println(formFields.size());
       
        // PdfContentByte is an object containing the user positioned text and graphic contents of a page.
        // It knows how to apply the proper font encoding.
        PdfContentByte canvas = stamp.getOverContent(1);       
       
        float[] f = form.getFieldPositions("Text2");       
        System.out.println("===>" + f.length + " // " + f[0] + ", " + f[1] + ", " + f[2] + ", " + f[3] + ", " + f[4]);       
        addText("<p>__Test__ Again!!</p>", canvas, f, 8, false);
       
        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());
            /// __NOTE__:3 HERE THE TEXT IS CHANGED ON THE FORM (that is OK)
            form.setField(fieldName, "simple text");
        }
                       
        stamp.setFormFlattening(true);
        stamp.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@...
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/

Re: Modifying the value of forms in a PDF document, update!

by berlinbrown :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I am able to read the PDF document and all of that works fine.  I am NOW having a problem trying to position NEW text near an existing form field.
 
ColumnText ct = new ColumnText(canvas);
float[] f = form.getFieldPositions("Text2");
ct.setSimpleColumn(f[1], f[2], f[3], f[4]);
 
For example, with this code, the setSimpleColumn doesn't seem to be in a position I want.
 
It could be that I am using 'ColumnText'


From: Brown, Berlin [GCG-PFS]
Sent: Tuesday, October 20, 2009 4:35 PM
To: 'itext-questions@...'
Cc: 'Berlin Brown'
Subject: RE: Modifying the value of forms in a PDF document, update!

OK, I am able to open a PDF document, find the form/text fields that I need and then set those values.  Now I want to be able to change the font size and alignment of that text.  And additional additional text.  I used some code 'addText' that I found online.  I am able to add the new text but the position is off.
 
I used the get field position call to get the positions but they don't seem to lie on top of the text field.  Essentially, with the code below, my new text is not even close to where I want it.  If you look at the hard coded values,
 
ct.setSimpleColumn(f[1], f[2]-300, f[3]+ 100, f[4]-300);
I don't mean to substract some arbitrary value 300 from the height. 
 
Here is the original code that I modified.
--------------- Code: 
 
     @SuppressWarnings("unchecked")
    public static boolean addText(String s, PdfContentByte canvas, float[] f, float size, boolean simulate) throws DocumentException, IOException {
       
        // PdfContentByte is an object containing the user positioned text and graphic contents of a page.
        // It knows how to apply the proper font encoding.        
        StyleSheet styles = new StyleSheet();
        /*
        styles.loadTagStyle("p", "size", size + "px");
        styles.loadTagStyle("p", "align", "justify");
        styles.loadTagStyle("p", "hyphenation", "en_us");
        */
        ArrayList<Element> objects = HTMLWorker.parseToList(new StringReader(s), styles);
        ColumnText ct = new ColumnText(canvas);
        ct.setAlignment(Element.ALIGN_CENTER);
        ct.setLeading(size * 1.2f);
        ///////////////////////////////
        /// __NOTE__:2
        /// THIS IS THE CODE I AM HAVING AN ISSUE WITH
        /// THE TEXT SHOULD! DISPLAY BY THE TEXT BOX
        ///////////////////////////////
        //ct.setSimpleColumn(f[1] + 2, f[2] + 2, f[3] - 2, f[4]); 
        //float llx, float lly, float urx, float ury, int alignment)
        ct.setSimpleColumn(f[1], f[2]-300, f[3]+ 100, f[4]-300);
        for (Element element : objects) {           
            final Chunk cz = new Chunk("abc!!!!!", F1);
            cz.setFont(F1);
            final Phrase jj = new Phrase(cz);
            ct.addElement(jj);
        }
        ct.go(simulate);
        return true;
    }
 
   
    public static final ByteArrayOutputStream generatePDFDocumentBytes()
        throws DocumentException, IOException {
               
        ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();       
        final PdfReader reader  = new PdfReader(CERT_PATH);
               
        PdfStamper stamp = new PdfStamper(reader, baosPDF);      
        AcroFields form = stamp.getAcroFields();
        HashMap formFields = form.getFields();
        System.out.println(formFields.size());
       
        // PdfContentByte is an object containing the user positioned text and graphic contents of a page.
        // It knows how to apply the proper font encoding.
        PdfContentByte canvas = stamp.getOverContent(1);       
       
        float[] f = form.getFieldPositions("Text2");       
        System.out.println("===>" + f.length + " // " + f[0] + ", " + f[1] + ", " + f[2] + ", " + f[3] + ", " + f[4]);       
        addText("<p>__Test__ Again!!</p>", canvas, f, 8, false);
       
        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());
            /// __NOTE__:3 HERE THE TEXT IS CHANGED ON THE FORM (that is OK)
            form.setField(fieldName, "simple text");
        }
                       
        stamp.setFormFlattening(true);
        stamp.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@...
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/

Re: Modifying the value of forms in a PDF document, update!

by 1T3XT info :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Brown, Berlin [GCG-PFS] wrote:
> For example, with this code, the setSimpleColumn doesn't seem to be in a
> position I want.

Your questions up till now didn't get much response because they are
phrased in a rather autistic way (I know because I'm an autist myself).

You say the setSimpleColumn isn't in the position you want, but only you
know the position you want.

The sequence of your mails show a learning process. Most people go
through this learning process in silence. In your case, we're getting an
update every time you discover something new. This is fascinating, but
it sounds like you're "thinking out loud", and we wouldn't want to
disturb your thinking process with answers that probably don't match the
unspoken questions in your head.
--
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info

------------------------------------------------------------------------------
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@...
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/

Re: Modifying the value of forms in a PDF document, update!

by berlinbrown :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


From: Brown, Berlin [GCG-PFS]
Sent: Wednesday, October 21, 2009 10:22 AM
To: Brown, Berlin [GCG-PFS]; 'itext-questions@...'
Cc: 'Berlin Brown'
Subject: RE: Modifying the value of forms in a PDF document, update!
I am able to read the PDF document and all of that works fine.  I am NOW having a problem trying to position NEW text near an existing form field.
 
ColumnText ct = new ColumnText(canvas);
float[] f = form.getFieldPositions("Text2");
ct.setSimpleColumn(f[1], f[2], f[3], f[4]);
 
For example, with this code, the setSimpleColumn doesn't seem to be in a position I want.
 
It could be that I am using 'ColumnText' 
 
------------
 
 
"Your questions up till now didn't get much response because they are
phrased in a rather autistic way (I know because I'm an autist myself). "
 
I thought presenting the code, the snippets, "close enough" wording.
I thought that might help.
 
-----------------
 
OK, I figured out the problem.  This code was causing the problem where
the new text I wanted to display would not appear on the document.
 
ColumnText ct = new ColumnText(canvas);
float[] f = form.getFieldPositions("Text2");
ct.setSimpleColumn(f[1], f[2], f[3], f[4]);
 
The result of "getFieldPositions" was off because apparently
the PDF document I read or something made the document rotate
so the absolute x/y positions I wanted to use were off.  Sometimes
the text would not display on the document.
 
I changed the code above to code below:
 
ColumnText ct = new ColumnText(canvas);
float[] f = form.getFieldPositions("Text2");
ct.setSimpleColumn(f[2], rotHeight-f[1], f[4], rotHeight-f[3]);
 
And that positions my text exactly where I want it to.
 
Next Problem:  I have another problem, hopefully I will solve
it myself but I will ask anyway in case the problem comes up
for someone else.
 
Now, using the 'ColumnText' object, how can I get my alignment to center.
Here are two variantions of my code.  Neither work, "align my text"
within the columntext bounding box.
 
------------
 
Variation 1:
StyleSheet styles = new StyleSheet();
       
styles.loadTagStyle("p", "size", size + "px");
styles.loadTagStyle("p", "align", "center");
styles.loadTagStyle("p", "hyphenation", "en_us");
styles.loadTagStyle("p", "width", "100%");
ArrayList<Element> objects = HTMLWorker.parseToList(new StringReader(s), styles);
ColumnText ct = new ColumnText(canvas);  
ct.setSimpleColumn(f[2], rotHeight-f[1], f[4], rotHeight-f[3]);
ct.setAlignment(Element.ALIGN_CENTER);
 
for (Element element : objects) { 
 final Chunk cz = new Chunk("___ABC_____", F1);           
 cz.setFont(F1);
 final Phrase jj = new Phrase(cz);        
 ct.addElement(jj);
}
ct.go();  
 
------------
Variation 2:
 
StyleSheet styles = new StyleSheet();
       
styles.loadTagStyle("p", "size", size + "px");
styles.loadTagStyle("p", "align", "center");
styles.loadTagStyle("p", "hyphenation", "en_us");
styles.loadTagStyle("p", "width", "100%");
ArrayList<Element> objects = HTMLWorker.parseToList(new StringReader(s), styles);
ColumnText ct = new ColumnText(canvas);  
ct.setSimpleColumn(f[2], rotHeight-f[1], f[4], rotHeight-f[3]);
ct.setAlignment(Element.ALIGN_CENTER);
 
for (Element element : objects) { 
      final Paragraph p1 = new Paragraph( "__Test__", F1);
            final PdfPTable table = new PdfPTable(2);
            final PdfPCell cell = new PdfPCell(p1);
            cell.setBorder(0);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);           
            table.addCell(cell);
            ct.addElement(table);
}
ct.go(); 
 
In variation 1 code, the text displays, the text is left aligned.  I want to display the code center aligned
within the bounding box used by setSimplecolumn.
 
In variation 2 code, the text does not display at all.
 

------------------------------------------------------------------------------
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@...
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/

Re: Modifying the value of forms in a PDF document, update!

by 1T3XT info :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Brown, Berlin [GCG-PFS] wrote:

> Now, using the 'ColumnText' object, how can I get my alignment to center.
> Here are two variantions of my code.  Neither work, "align my text"
> within the columntext bounding box.
>  
> ------------
>  
> Variation 1:
> StyleSheet styles = new StyleSheet();
>        
> styles.loadTagStyle("p", "size", size + "px");
> styles.loadTagStyle("p", "align", "center");
> styles.loadTagStyle("p", "hyphenation", "en_us");
> styles.loadTagStyle("p", "width", "100%");
> ArrayList<Element> objects = HTMLWorker.parseToList(new StringReader(s),
> styles);
> ColumnText ct = new ColumnText(canvas);  
> ct.setSimpleColumn(f[2], rotHeight-f[1], f[4], rotHeight-f[3]);
> ct.setAlignment(Element.ALIGN_CENTER);

Aha, you're working in text mode!

> for (Element element : objects) {
>  final Chunk cz = new Chunk("___ABC_____", F1);          
>  cz.setFont(F1);
>  final Phrase jj = new Phrase(cz);        
>  ct.addElement(jj);

O, now you're working in composite mode!

> }
> ct.go();  

DO NOT MIX TEXT MODE WITH COMPOSITE MODE.
Read chapter 7 of the book for more info.
--
This answer is provided by 1T3XT BVBA
http://www.1t3xt.com/ - http://www.1t3xt.info

------------------------------------------------------------------------------
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@...
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/