boxable icon indicating copy to clipboard operation
boxable copied to clipboard

using both setText and setHeight leads to empty cells

Open ztNFny opened this issue 4 years ago • 1 comments

When using both setText and setHeight on a cell the cell is printed as empty in the resulting PDF. The order of those calls is not relevant to the result.

Minified testcase:

public static void main(String[] args) throws Exception {

    String inFile = "input.pdf";
    File inFileF = new File(inFile);
    String outFile = "output.pdf";

    PDDocument document = PDDocument.load(inFileF);

    BaseTable table = new BaseTable(100f, 100f, 0f, 0f, 100f, 100f, document, document.getPage(0), true, true);

    Row<PDPage> row = table.createRow(20f);

    Cell cell = row.createCell(100f, "Text");

    cell.setText("New Text");
    cell.setHeight(50f);

    table.draw();

    document.save(outFile);
    document.close();
}

Scenario where I want to do that (maybe you have a solution for me that doesn't run into this issue): I want to set a maximum height on a table cell (row actually). For that the plan was to loop over the text until the cells height is smaller than the specified max height:

while (cell.getHeight() > maxHeight) {
    cell.setText(text.substring(0, text.length()-1));
}

ztNFny avatar Oct 26 '20 14:10 ztNFny

Found a workaround! add after .setHeight: cell.setValign();

ztNFny avatar Oct 27 '20 13:10 ztNFny