> -----Original Message-----
> From: natros [mailto:
natros@...]
> Sent: Thursday, July 02, 2009 12:32 PM
> To:
itext-questions@...
> Subject: [iText-questions] Test if some elements fits in a page
>
> 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