I have been doing some experiments with ColumnText to see if some
iText element fits in the page. I'm witting 38 time the word "Hello"
and 5 times the word "World". If the last 5 words does not fit in the
page I want them in a new page. Here is what I did

Document doc = new Document();
try {
  PdfWriter w = PdfWriter.getInstance(doc, new
FileOutputStream("/home/fsousa/test.pdf"));
  doc.open();

  ColumnText ct = new ColumnText(w.getDirectContent());
  ct.setSimpleColumn(doc.left(), doc.bottom(), doc.right(), doc.top());

  for (int i = 0; i < 38; ++i)
    ct.addElement(new Phrase(i + ". Hello"));

  int status = ct.go();
  while (ColumnText.hasMoreText(status)) {
    doc.newPage();
    ct.setYLine(doc.top());
    status = ct.go();
  }

  float y = ct.getYLine();

  for (int i = 0; i < 5; ++i)
    ct.addElement(new Phrase(i + ". World\n"));

  status = ct.go(true);
  if (ColumnText.hasMoreText(status))
    ct.setText(null);

  for (int i = 0; i < 5; ++i)
    ct.addElement(new Phrase(i + ". World\n"));

  if (ColumnText.hasMoreText(status)) {
    doc.newPage();
    ct.setYLine(doc.top());
  } else
    ct.setYLine(y);

  ct.go();

} catch (FileNotFoundException e) {
  e.printStackTrace();
} catch (DocumentException e) {
  e.printStackTrace();
}
doc.close();

It works but I have to call ct.setText(null) to clear the remaining
elements in ColumnText. I could use ct.addText() instead of
ct.addElement() and calling ct.clearChunks(). Is this the right thing
or is there a better way.
--
Filipe Sousa

------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
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