Test if some elements fits in a page

View: New views
2 Messages — Rating Filter:   Alert me  

Test if some elements fits in a page

by Filipe Sousa-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
iText-questions@...
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/

Re: Test if some elements fits in a page

by Paulo Soares-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The better way is to create a new ColumnText, the first one is "spent" anyway.

Paulo

> -----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

Aviso Legal:
Esta mensagem é destinada exclusivamente ao destinatário. Pode conter informação confidencial ou legalmente protegida. A incorrecta transmissão desta mensagem não significa a perca de confidencialidade. Se esta mensagem for recebida por engano, por favor envie-a de volta para o remetente e apague-a do seu sistema de imediato. É proibido a qualquer pessoa que não o destinatário de usar, revelar ou distribuir qualquer parte desta mensagem.

Disclaimer:
This message is destined exclusively to the intended receiver. It may contain confidential or legally protected information. The incorrect transmission of this message does not mean the loss of its confidentiality. If this message is received by mistake, please send it back to the sender and delete it from your system immediately. It is forbidden to any person who is not the intended receiver to use, distribute or copy any part of this message.



------------------------------------------------------------------------------

_______________________________________________
iText-questions mailing list
iText-questions@...
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/