Setting the font of an empty table cell

Hi,

I have the need to set the font of the text in a cell. It is possible that the text will be empty. In the case where it is empty, the font ends up being the normal text default (Times New Roman, 12) instead of the font I specify. The code below demonstrates the problem.

public static void main(String[] args)
{
    Document doc = new Document();
    doc.removeAllChildren();

    // This cell will have text.
    Run run1 = new Run(doc, "Not Empty");
    run1.getFont().setName("Verdana");
    run1.getFont().setSize(20);

    Paragraph paragraph1 = new Paragraph(doc);
    paragraph1.getRuns().add(run1);

    Cell cell1 = new Cell(doc);
    cell1.getParagraphs().add(paragraph1);

    Row row1 = new Row(doc);
    row1.getCells().add(cell1);

    // This cell will be empty, but should have the font I specify. It has
    // the default Word font, Times New Roman size 12 instead.
    Run run2 = new Run(doc, "");
    run2.getFont().setName("Arial");
    run2.getFont().setSize(10);

    // Setting the text to something and then to blank after the font 
    // modification has no perceivable affect.
    //Run run2 = new Run(doc,"SOMETHING");
    // run2.getFont().setName("Verdana");
    // run2.getFont().setSize(20);
    // .setText("");

    Paragraph para2 = new Paragraph(doc);
    para2.getRuns().add(run2);

    Cell cell2 = new Cell(doc);
    cell2.getParagraphs().add(para2);

    Row row2 = new Row(doc);
    row2.getCells().add(cell2);

    // Add both cells to the table.
    Table table = new Table(doc);
    table.getRows().add(row1);
    table.getRows().add(row2);

    // Add the table to the document.
    Body body = new Body(doc);
    body.getParagraphs().add(table);

    Section section = new Section(doc);
    section.appendChild(body);

    doc.getSections().add(section);
    doc.save("C:/EMPTY_CELL_TEST.doc");
}

Simply adding a space in the case where the string is empty is not an option for me. I can do it in Word by hand, so I’m assuming I’m just missing something (order of operations?)

Any help would be appreciated!

Thanks,

awp2513

Hi

Thanks for your inquiry. You should just set paragraph break font. Please see the modified code:

Document doc = new Document();
doc.removeAllChildren();
// This cell will have text.
Run run1 = new Run(doc, "Not Empty");
run1.getFont().setName("Verdana");
run1.getFont().setSize(20);
Paragraph paragraph1 = new Paragraph(doc);
paragraph1.getParagraphBreakFont().setName("Verdana");
paragraph1.getParagraphBreakFont().setSize(20);
paragraph1.getRuns().add(run1);
Cell cell1 = new Cell(doc);
cell1.getParagraphs().add(paragraph1);
Row row1 = new Row(doc);
row1.getCells().add(cell1);
// This cell will be empty, but should have the font I specify. It has
// the default Word font, Times New Roman size 12 instead.
Run run2 = new Run(doc, "");
run2.getFont().setName("Arial");
run2.getFont().setSize(10);
Paragraph para2 = new Paragraph(doc);
para2.getParagraphBreakFont().setName("Arial");
para2.getParagraphBreakFont().setSize(10);
para2.getRuns().add(run2);
Cell cell2 = new Cell(doc);
cell2.getParagraphs().add(para2);
Row row2 = new Row(doc);
row2.getCells().add(cell2);
// Add both cells to the table.
Table table = new Table(doc);
table.getRows().add(row1);
table.getRows().add(row2);
// Add the table to the document.
Body body = new Body(doc);
body.getParagraphs().add(table);
Section section = new Section(doc);
section.appendChild(body);
doc.getSections().add(section);
doc.save("C:\\Temp\\out.doc");

Best regards,

Setting the paragraph break font did exactly what I needed. Thanks!

awp2513