I have modified the loop which calls go() until the ColumnText
has no more text.
I am inserting a new page as needed with insertPage().
Afterward, I get a new cb by calling stamper.getOverContent(pageNo)
and call the ColumnText setCanvas() method with this cb.
However, within the loop, when I call ct.go() the value of biditext
never changes, so it becomes an endless loop.
Here is the code with the changes:
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];
int pageNo = 5;
PdfContentByte cb = stamper.getOverContent(pageNo);
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.
// You should insert a new page and get a new cb from that
page.
while ( ct.hasMoreText(status) ) {
stamper.insertPage(++pageNo, PageSize.LETTER);
cb = stamper.getOverContent(pageNo);
cb.addTemplate(sowPage,0,0); // add the static page as
background
ct.setCanvas(cb);
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();
}
-------------------------------------------------------------------------
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/