I'm trying to set up a page where the text fills up in a text box and then overflows to another text box on the page. I set up a rectangle and will send it the text, but I can't figure out how to find the text that won't fit.
I remember seeing a function that adds words to a document (or table or something) and it had a "Returns remaining text" feature. I can't find that function anymore.
How can I get the text that won't fit returned so I can assign it to a variable to send to the next textbox?
You are looking for ColumnText
which can be used to render text inside a defined shape. The go()
method returns the constants NO_MORE_TEXT
(1) if the text fit inside the shape or NO_MORE_COLUMN
(2) if the text is overflowing. As far as I know there is no way to actually get the remaining text directly. However, you don't need the text for rendering it in the next textbox.
In case the text overflows, you can just assign a new column and call go()
again. Repeat this process until all the text has been drawn.
Here is an example with 3 simple rectangular boxes:
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("overflow.pdf"));
document.open();
// define text
PdfContentByte canvas = writer.getDirectContent();
ColumnText ct = new ColumnText(canvas);
ct.addElement(new Paragraph(LOREM_IPSUM)); // <- some filler text from constant
// fill first box
ct.setSimpleColumn(50, 735, 400, 800);
int result = ct.go();
// fill second box (if necessary)
if (result == ColumnText.NO_MORE_COLUMN) {
ct.setSimpleColumn(75, 635, 425, 700);
result = ct.go();
}
// fill third box (if necessary)
if (result == ColumnText.NO_MORE_COLUMN) {
ct.setSimpleColumn(100, 535, 450, 600);
result = ct.go();
}
document.close();
Result: