Reputation: 41
How can i handle really long dynamic text for a paragraph which is in a fixed rectangle in iText document ?
ColumnText ct = new ColumnText(canvas);
Font paragraphFont=new Font(baseFont,4.5f);
ct.setSimpleColumn(9, 70, 70, 95);
Paragraph paragraph=new Paragraph("REALLLLLLLLLLY LONGGGGGGGGGG text",paragraphFont);
ct.addElement(paragraph);
ct.go();
Upvotes: 3
Views: 2185
Reputation: 77528
I have copy/pasted your code snippet in an example which I named SimpleColumn:
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Rectangle rect = new Rectangle(100, 120);
Document document = new Document(rect);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
BaseFont baseFont = BaseFont.createFont();
ColumnText ct = new ColumnText(canvas);
Font paragraphFont=new Font(baseFont,4.5f);
ct.setSimpleColumn(9, 70, 70, 95);
Paragraph paragraph = new Paragraph("REALLLLLLLLLLY LONGGGGGGGGGG text",paragraphFont);
ct.addElement(paragraph);
ct.go();
// step 5
document.close();
}
This results in the file simple_column.pdf:
As you can see, the text is displayed correctly inside a rectangle of which the lower-left corner has the coordinates x = 9; y = 70
and the upper-right corner has the coordinates x = 70, y = 95
. The text didn't fit the width of this rectangle, so it was wrapped (split at the white space character and distributed over two lines).
This is how long paragraphs are handled when you want to render them in a fixed rectangle. If the paragraph doesn't fit the rectangle, the remainder of the paragraph is stored in the ColumnText
object. You can define a new simple column (using different coordinates) to render the rest of the paragraph.
Upvotes: 1