I'm making a table with iText 7.2.0 (Java 8, Windows Server 2016).
The table needs to not overflow (be wider than the page), but it is OK to break cells event in the middle of a word if necessary.
Some of the cell's text does not render and I get this warning:
[main] WARN com.itextpdf.layout.renderer.RootRenderer - Element does not fit current area.
If I change the text just a bit (like replace "i" with "I") it works.
How can I fix it? Is it a bug in my code or in iText?
import java.io.FileNotFoundException;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.properties.UnitValue;
public class Main {
private static void makeCell(Table table, Style style, String text) {
Cell c = new Cell();
Paragraph p = new Paragraph();
Text t = new Text(text);
c.addStyle(style);
p.add(t);
c.add(p);
table.addCell(c);
}
public static void main(String[] args) throws FileNotFoundException {
PdfWriter writer = new PdfWriter("C:\\Temp\\test.pdf");
PdfDocument pdf = new PdfDocument(writer);
try (Document document = new Document(pdf)) {
Style style = new Style().setBold();
Table table = new Table(UnitValue.createPercentArray(9));
table.useAllAvailableWidth();
table.setFixedLayout();
makeCell(table, style, "Description");
makeCell(table, style, "DescrIption"); /* Capital I */
makeCell(table, style, "Description");
makeCell(table, style, "Description");
makeCell(table, style, "Description");
makeCell(table, style, "DescrIption"); /* Capital I */
makeCell(table, style, "Description");
makeCell(table, style, "Description");
makeCell(table, style, "Description");
document.add(table);
}
}
}
Result - The cells with "Description" do not render, the cell with "DescrIption" render.
This does seem to be a bug in iText.
As a workaround you can provide a proper bold font instead of using the bold simulation with setBold()
. In fact, providing a proper bold font is the recommended way of making your text look bold since bold simulation produces inferior result in terms of output quality.
The only modification needs to be done in your Style
object since you already made use of the styles for proper reusage!
Style style = new Style().setFont(PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD));
Visual result: