We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Table loses the formatting for empty cells

Hi,

i fall in this issue after creating a word table.
I set the font “Arial Narrow” on the run of the paragraphs of all the cells. I’ve noticed that the empty cells don’t have the right font, but keep the default (Calibri).
Is it a bug or is there a workaround?

here it is my code.

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”;”>public class TestTableEmptyRun {
public static void main(String[] args) throws Exception {
Document doc = new Document(“C:\TestTableWithEmptyRun.docx”);
Table wordsTable = new Table(doc);
Row firstRow = new Row(doc);
Row secondRow = new Row(doc);

com.aspose.words.Cell cell11 = new com.aspose.words.Cell(doc);
setContent(doc, cell11, “pippo”);
firstRow.appendChild(cell11);
com.aspose.words.Cell cell12 = new com.aspose.words.Cell(doc);
setContent(doc, cell12, “”);
firstRow.appendChild(cell12);
com.aspose.words.Cell cell21 = new com.aspose.words.Cell(doc);
setContent(doc, cell21,"");
secondRow.appendChild(cell21);
com.aspose.words.Cell cell22 = new com.aspose.words.Cell(doc);
setContent(doc,cell22, “pluto”);
secondRow.appendChild(cell22);
wordsTable.appendChild(firstRow);
wordsTable.appendChild(secondRow);

doc.getFirstSection().getBody().appendChild(wordsTable);
doc.save(“C:\TestTableWithEmptyRunOutput.docx”);
}

private static void setContent(Document doc, Cell wordsCell, String value) throws Exception {
Paragraph wordsParagraph = new Paragraph(doc);
setParagraphDeafultFormatting(wordsParagraph);
Run run = new Run(doc);
run.setText(value);
Font font = run.getFont();
font.setName(“Arial Narraow”);
font.setSize(11);
wordsParagraph.appendChild(run);
wordsCell.appendChild(wordsParagraph);
BorderCollection borders = wordsCell.getCellFormat().getBorders();
borders.getByBorderType(BorderType.BOTTOM).setLineStyle(LineStyle.SINGLE);
borders.getByBorderType(BorderType.LEFT).setLineStyle(LineStyle.SINGLE);
borders.getByBorderType(BorderType.RIGHT).setLineStyle(LineStyle.SINGLE);
borders.getByBorderType(BorderType.TOP).setLineStyle(LineStyle.SINGLE);
}

private static void setParagraphDeafultFormatting(Paragraph paragraph) throws Exception {
paragraph.getParagraphFormat().clearFormatting();
paragraph.getParagraphFormat().setLineSpacingRule(LineSpacingRule.MULTIPLE);
paragraph.getParagraphFormat().setLineSpacing(12);
paragraph.getParagraphFormat().setSpaceAfter(0);
paragraph.getParagraphFormat().setSpaceBefore(0);
}<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”;”>}

Hi Matteo,


Thanks for your inquiry. We are looking into the issue and will update you shortly.

Best Regards,

Hi Matteo,


Thanks for your patience. We have tested the scenario and noticed that the formatting issue of empty runs. We have logged a ticket WORDSNET-15238 in our issue tracking system for further investigation and rectification. We will notify you as soon as it is resolved.

However as a workaround you may change the font of Table cells using DocumentVisitor class as following.

public static void FontUpdate() throws Exception {<o:p></o:p>

Document doc = new Document("TestTableWithEmptyRunOutput.docx");

FontChanger changer = new FontChanger();

for (Table table : (Iterable

) doc.getChildNodes(NodeType.TABLE,

true)) {

table.accept(changer);

}

doc.save("Font_table.docx");

}

-----------------------

public class FontChanger extends DocumentVisitor {

/**

* Called when a Run node is encountered in the document.

*/

public int visitRun(Run run) throws Exception {

ResetFont(run.getFont());

// Let the visitor continue visiting other nodes.

return VisitorAction.CONTINUE;

}

/**

* Called when a FieldStart node is encountered in the document.

*/

public int visitFieldStart(FieldStart fieldStart) throws Exception {

ResetFont(fieldStart.getFont());

return VisitorAction.CONTINUE;

}

/**

* Called when a FieldSeparator node is encountered in the document.

*/

public int visitFieldSeparator(FieldSeparator fieldSeparator)

throws Exception {

ResetFont(fieldSeparator.getFont());

return VisitorAction.CONTINUE;

}

/**

* Called when a FieldEnd node is encountered in the document.

*/

public int visitFieldEnd(FieldEnd fieldEnd) throws Exception {

ResetFont(fieldEnd.getFont());

return VisitorAction.CONTINUE;

}

/**

* Called when visiting of a Paragraph node is ended in the document.

*/

public int visitParagraphEnd(Paragraph paragraph) throws Exception {

// When outputting to plain text we output Cr+Lf characters.

ResetFont(paragraph.getParagraphBreakFont());

return VisitorAction.CONTINUE;

}

/**

* Called when a HeaderFooter node is encountered in the document.

*/

public int visitHeaderFooterStart(HeaderFooter headerFooter)

throws Exception {

return VisitorAction.SKIP_THIS_NODE;

}

private void ResetFont(com.aspose.words.Font font) throws Exception {

font.setName(mNewFont);

}

// Font by default

private String mNewFont = "Arial Narraow";

}


Best Regards,
Hi Matteo,

Thanks for your patience. We have further investigated the issue and noticed that empty cell contains paragraph break. Please use Paragraph.ParagraphBreakFont property to set font formatting of paragraph break character. Please add following code lines in setFont() method, it will resolve the issue.

wordsParagraph.getParagraphBreakFont().setName("Arial Narraow");

wordsParagraph.getParagraphBreakFont().setSize(11);


Please feel free to contact us for any further assistance.

Best Regards,