Font size in cell

Hi support,

Please take a look over the following code, and note that, if the text in 2nd cell is missing, the font size is not applied within 2nd cell :

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.startTable();

builder.insertCell();
builder.getCellFormat().clearFormatting();

builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(1);

builder.getParagraphFormat().clearFormatting();

builder.getFont().setSize(8);
builder.write("cell1");
builder.getFont().clearFormatting();

builder.insertCell();
builder.getCellFormat().clearFormatting();

builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(1);

builder.getParagraphFormat().clearFormatting();

builder.getFont().setSize(8);
//builder.write("cell2"); 
builder.endRow();

builder.endTable();

doc.save("C:\Temp\docFontSizeInEmptyCell.doc");

Is there any way to solve this, other than inserting a blank space in that 2nd cell ?

I’m using Aspose.Word 2.5.0 version for java.

Thank you,
Milan

Hi Milan,

Thanks for your inquiry. The problem occurs because builder.getFont represents fonts of the current run, but since you do not insert any text into the cell – there is no Runs and font settings of Run have no effect.

In this case, you can also apply font of paragraph break. Please see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startTable();
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(1);
builder.getParagraphFormat().clearFormatting();
builder.getFont().setSize(8); // Applies font of Runs
builder.getCurrentParagraph().getParagraphBreakFont().setSize(8); // Applies font of paragraph break.
builder.write("cell1");
builder.getFont().clearFormatting();
builder.insertCell();
builder.getCellFormat().clearFormatting();
builder.getCellFormat().getBorders().setLineStyle(LineStyle.SINGLE);
builder.getCellFormat().getBorders().setLineWidth(1);
builder.getParagraphFormat().clearFormatting();
builder.getFont().setSize(8);
builder.getCurrentParagraph().getParagraphBreakFont().setSize(8); // Applies font of paragraph break.
builder.endRow();
builder.endTable();
doc.save("C:\\Temp\\out.doc");

Hope this helps.

Best regards.

Hi Support,

So we can use the same way to set the font family, font color, bold, italic, etc, right?

Regards,

Hi

Thanks for your inquiry. Sure, you can specify all required settings of the font.

Best regards,