I am still having trouble putting it all together.

I've been trying to get this working following your suggestions, and
going
over the sections out of the book, but here's where I am:

I'm unsure about 2 issues -- probably more waiting for me once I get
past those! :-)

1) When using the ColumnText object to display the long text, I am
obviously not
   adding a new page correctly.  Each time I call go(), as much of the
remaining
   text as possible is written, but always to the same page
(overprinting the
   previous text).

2) The pages added (of course I am not yet adding them successfully)
include form
   fields (from page 5 of the template).  These form fields appear on
several pages
   of the template.  What would be a good way to make sure the form
fields on the
   added pages get filled (and flattened) as well?  Would it make sense
to just
   defer filling/flattening initially, and after all the pages have been
(successfully!)
   added, create a new stamper to fill and flatten all the fields
(except for the one
   I am handling with the ColumnText object)?

By the way, although chapter 2 describes the use/purpose of the basic
manipulation classes,
and putting them together, e.g. in multiple passes, it is still
confusing sometimes
figuring out how to "pipe" the results from one pass to the next, even
though there
are examples for a few situations.  Maybe if I ever feel like I really
understand it
I will start an FAQ about this! :-)

Here is what I have so far (your earlier comments are included):

                try {
            // I would read the form with PdfReader.  Create a
PdfStamper with this reader.
            PdfReader  templateReader = new PdfReader(templateFileName);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            
            PdfStamper stamper        = new PdfStamper(templateReader,
baos);

            // Import the static page and add it as a background to
every page (in a loop).
            PdfImportedPage sowPage =
stamper.getImportedPage(templateReader,5); // 5th page is the one which
may need to overflow

            // Create an XfdfReader, get the fields one by one, and set
all of them except
            // one as described in the book. Set flattening to true.
            stamper.setFormFlattening(true);
            XfdfReader dataReader = new XfdfReader(dataFileName);
            AcroFields form = stamper.getAcroFields();
            String fieldName;
            for (Iterator i = form.getFields().keySet().iterator();
i.hasNext(); ) {
                fieldName = (String)i.next();
                if ( fieldName.equals("long_sow") ) {
                    sow = dataReader.getField(fieldName);
                } else {
 
form.setField(fieldName,dataReader.getField(fieldName));
                }
            }

            // The field you didn't set, is the one that may or may not
fit the last page.
            // Wrap the data in a ColumnText object.  Retrieve the
positions of the big
            // field on the last page. Use these coordinates to define
the ColumnText
            // rectangle.  Invoke go()
            float[] sowFieldPositions =
form.getFieldPositions("long_sow");
            if ( sowFieldPositions == null ) {
                System.err.println("no field positions found for
long_sow");
                System.exit(1);
            } else if (sowFieldPositions.length != 5) {
                System.err.println("unexpected number of values for
long_sow field positions: " + sowFieldPositions.length);
                System.exit(1);
            }
            float llx = sowFieldPositions[1];
            float lly = sowFieldPositions[2];
            float urx = sowFieldPositions[3];
            float ury = sowFieldPositions[4];
            PdfWriter writer = stamper.getWriter();
            // * following line fails with
            // * java.lang.UnsupportedOperationException: Use
PdfStamper.getUnderContent() or PdfStamper.getOverContent()
            // PdfContentByte cb = writer.getDirectContent();
            PdfContentByte cb = stamper.getOverContent(5);
            ColumnText ct = new ColumnText(cb);
            ct.setSimpleColumn(llx,lly,urx,ury);
            ct.setText(new Phrase(sow));
            int status = ct.go();

            // If there's no more text left in the ColumnText, you're
done. The extra page
            // is not needed. If there still is some text left, insert a
new page at the
            // end, add the static page as background, reset the
ColumnText rectangle and
            // add it to the newly created page.
            while ( ct.hasMoreText(status) ) {
                writer.newPage();             // insert a new page
                cb.addTemplate(sowPage,0,0);  // add the static page as
background
                ct.setSimpleColumn(llx,lly,urx,ury);
                status = ct.go();
            }

            stamper.close();

            // write output to file
            FileOutputStream fos = new FileOutputStream(outputFileName);
            baos.writeTo(fos);
            fos.close();
            System.out.println("done");

                } catch (Exception e) {
                        e.printStackTrace();
                }

| -----Original Message-----
| From: Gatewood, Paul (RSIS) 
| Sent: Thursday, January 11, 2007 11:07 AM
| To: '[email protected]'
| Subject: Re: creating new document where every page includes 
| contents (text and form fields) from pdf template file
| 
| 
| Thank you for the very quick feedback Bruno.
| Sorry I ended up making it confusing by trying not to make it 
| too long!
| 
| Anyway, in the second part, where I explain the overall 
| situation, it seems it became clear what I was trying to do, 
| and I will try to understand the approach you suggested, 
| using a ColumnText object.  It sounds like this may be what I 
| had hoped for innitially, an object I could use to see how 
| much space (pages) the long text will need.
| 

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to