12

Does anyone know, how to, in iText, add multiline text in bounding box (with coordinates specified).

I tried

cb.showTextAligned(
    PdfContentByte.ALIGN_LEFT,
    text,
    bounds.getLeft(),
    TOTAL_HEIGHT-bounds.getTop(),
    0 );

But it does not support newlines. I also tried

PdfContentByte cb = writer.getDirectContent();
cb.moveText(300,400);
document.add(new Paragraph("TEST paragraph\nNewline"));

This supports newlines but does not react to moveText, so I don't know how to put it at given position or better: bounding box.

I suspect chunks or PdfTemplate or maybe table might help, but i don't (yet) know how to put it together. TIA for help.

Aubin
  • 14,617
  • 9
  • 61
  • 84
KarolDepka
  • 8,318
  • 10
  • 45
  • 58

2 Answers2

25

Try this:

ColumnText ct = new ColumnText(cb);
Phrase myText = new Phrase("TEST paragraph\nAfter Newline");
ct.setSimpleColumn(myText, 34, 750, 580, 317, 15, Element.ALIGN_LEFT);
ct.go();

parameters of SetSimpleColumn are:

  1. the phrase
  2. the lower left x corner (left)
  3. the lower left y corner (bottom)
  4. the upper right x corner (right)
  5. the upper right y corner (top)
  6. line height (leading)
  7. alignment.
Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54
Yannick Smits
  • 875
  • 9
  • 14
  • Thanks. I actually wrote a mini-library for layouts and auto-fitting and tables, which incorporates things you have written in your response. – KarolDepka Jan 09 '10 at 20:42
  • What does the box width, box height mean? – Cocoa Dev Apr 03 '13 at 19:39
  • 3
    4. is upper right x corner, 5. is upper right y corner: http://www.openlogic.com/wazi/bid/188064/iText-Generate-PDFs-in-Java – Tillito Apr 09 '13 at 14:13
  • how do you get this text to go ontop of other elements? It's being hidden behind a table right now. – TWilly Dec 13 '16 at 03:37
2
ColumnText ct = new ColumnText(content);
ct.setSimpleColumn(
    new Phrase("Very Long Text"),
    left=20, bottom=100, right=500, top=500,
    fontSize=18, Element.ALIGN_JUSTIFIED);
ct.go(); // for drawing
Aubin
  • 14,617
  • 9
  • 61
  • 84
StepanM
  • 4,222
  • 1
  • 21
  • 25