简体   繁体   中英

Some rows not rendered in Single column iText table

I'm attempting to create 2 nested tables, one with captions, and the other with values (some of them blank), like so:

所需的渲染

But it renders as follows, with the value for row 4 appearing in row 3:

实际渲染

suggesting row 3 was somehow skipped. What am I doing wrong?

Below is the code:

private PdfPTable getOrderHeaderTable() {
    PdfPTable table = new PdfPTable(new float[]{25f, 25f, 20f, 20f});

    table.addCell(getCaptionCell());
    table.addCell(getBillingAddressCaptionCell());
    table.addCell(getShippingAddressCaptionCell());
    addBillingAddress(table);
    addShippingAddress(table);

    // These are the nested tables I'm referring to:
    addOrderInfoCaptions(table);
    addOrderInfo(table);

    return table;
}

private void  addOrderInfoCaptions(PdfPTable table) {
    PdfPCell cell = new PdfPCell(getOrderInfoCaptionTable());
    cell.setBorder(0);
    table.addCell(cell);
}

private void addOrderInfo(PdfPTable table) {
    PdfPCell cell = new PdfPCell(getOrderInfoTable());
    cell.setBorder(0);
    table.addCell(cell);
}

private PdfPTable getOrderInfoCaptionTable() {
    PdfPTable table = new PdfPTable(1);

    table.addCell(getTextCell(bold, BaseColor.WHITE, 0, 0, "Order Date:"));
    table.addCell(getTextCell(bold, BaseColor.WHITE, 0, 0, "Order Number:"));
    table.addCell(getTextCell(bold, BaseColor.WHITE, 0, 0, "Reservation Number:"));
    table.addCell(getTextCell(bold, BaseColor.WHITE, 0, 0, "PO Number:"));
    table.addCell(getTextCell(bold, BaseColor.WHITE, 0, 0, "Gift Registry:"));

    return table;
}

private PdfPTable getOrderInfoTable() {
    PdfPTable table = new PdfPTable(1);
    table.addCell(getTextCell(regular, BaseColor.WHITE, 0, 0, getOrderDateString()));
    table.addCell(getTextCell(regular, BaseColor.WHITE, 0, 0, order.getCommerceHubId()));
    table.addCell(getTextCell(regular, BaseColor.WHITE, 0, 0, order.getReservationNumber()));
    table.addCell(getTextCell(regular, BaseColor.WHITE, 0, 0, order.getPartnerPurchaseOrderNumber()));
    table.addCell(getTextCell(regular, BaseColor.WHITE, 0, 0, getGiftRegistryID()));

    return table;
}

private String getOrderDateString() {
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    return sdf.format(order.getOrderDate());
}

protected PdfPCell getTextCell(Font font, BaseColor color, int border, int horizontalAlignment, String value) {
    PdfPCell cell = new PdfPCell(new Phrase(value, font));
    cell.setHorizontalAlignment(horizontalAlignment);
    cell.setBorder(border);
    cell.setBackgroundColor(color);
    return cell;
}

If anyone can help me, I would greatly appreciate it; this is holding up an important release.

Seems the problem was that the blank values. In other words,

 new PdfPCell(new Phrase(""))

does not work.

But if I insert any character, such as a space:

new PdfPCell(new Phrase(" "))

the problem disappears.

I don't know why this happens. I guess an empty Phrase = empty Cell = takes up no space? In any case, it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM