Gatewood, Paul (RSIS) wrote:
> I am trying to create a new pdf file, say "longText.pdf", with some
> arbitrarily long text.
> I have an existing single page template, say "longTextTemplate.pdf"
> which has the boilerplate text and form fields which should appear on
> every page of the output "longText.pdf".
> I add the text to the document, and as documented, the boilerplate text
> from "longTextTemplate.pdf" shows up fine on every page of the output,
> but no form fields.
That's normal. You are adding a PdfImportedPage from
a PDF with fields to a new PDF without fields.
With PdfImportedPage, fields are not copied.
You can however create new fields with the same
properties as the original ones and add them to
the new PDF.
> I understand that with PdfStamper the form fields would be included as
> well, but am not sure how to bring PdfStamper into the process.
Create a new PDF with PdfStamper using a PdfReader object
that reads the template. Then add your long text to the
Stamper.
> I am hoping to do as much in memory as possible, rather than writing too
> many temporary files.
That's not a problem. You can always write to a
ByteArrayOutputStream.
> 1) I have split the 5 page template into 2 templates --
> one with pages 1-4 ("mainTemplate.pdf")
> and the other with only page 5 ("longTextTemplate.pdf")
OK.
> 2) At runtime, I create a new document with the long text.
> Margins are defined big enough to accommodate the boilerplate text
> and form fields from the "longTextTemplate.pdf".
> On every page, add the contents (text and form fields)
> from "longTextTemplate.pdf"
I don't understand what you're saying here.
Don't you have a schema or a drawing that shows what
you're trying to do?
> 3) append (using PdfCopyFields) the output from step 2 "longText.pdf"
> to "mainTemplate.pdf" to produce "combinedTemplate.pdf".
> This will have the contents of "mainTemplate.pdf"
> followed by the contents of "longText.pdf"
You've lost me.
> 4) use PdfStamper to fill and flatten the fields
> in "combinedTemplate.pdf" with the field values
> in the xfdf data file to produce the final output.
Aha, maybe now I understand. Especially the
word 'flatten' sounds good.
So you have a PDF form with 4 normal pages
and 1 page that is sometimes sufficient,
sometimes you need 2 pages instead of one.
You also have an XFDF file containing the
data that needs to be filled in.
I would read the form with PdfReader.
Create a PdfStamper with this reader.
Import the static page and add it as
a background to every page (in a loop).
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.
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()
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.
You can do this in a loop, so that
multiple pages can be added if the
text doesn't fit on 6 but on 7 pages.
br,
Bruno
-------------------------------------------------------------------------
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/