Setting a table font issue

I found that if I just did:

  Table table1 = builder.StartTable();
  builder.InsertCell().CellFormat.Borders.LineStyle = LineStyle.None;
  //table1.Style = document.Styles["tableStyle"];
  builder.InsertParagraph();
  builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(60);
  builder.Font.Size = 12;
  builder.Write("Rooms");
  //builder.InsertHtml("Rooms");

  builder.InsertCell();
  builder.InsertParagraph();
  builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(40);
  builder.Font.Size = 12;
  builder.Write("Arrival");
  builder.EndRow();
  builder.EndTable();

It applies the font if I do:

foreach (Paragraph item in table1.GetChildNodes(NodeType.Paragraph, true))
{
    item.ParagraphFormat.Style.Font.Name = "Calibri";
    //item.ParagraphFormat.Style.Font.Size = 12;
    item.ParagraphBreakFont.Size = 1;
}

If I was to apply a Table Style, it doesn’t work if I am using InsertHtml(). So I got rid of that. I just use builder.Write() and builder.Writeln() now. Don’t do paragraph styles, either - it didn’t seem to work inside a table cell. Or at least when using InsertHtml().

I was racking my brain about this for forever until I figured out the solution. Just do NOT use InsertHtml() or table or paragraph styles. Just do the InsertParagraph() and iterate the table for the paragraph nodes and apply the font within that. I did notice that the ParagraphFormat.Style.Font.Size did not work within the foreach, though, and so I applied it within the cells, themselves, and this worked.

@navyjax2 Unfortunately, it is not quite clear what the problem is. Could you please elaborate and if possible provide your problematic output and expected output?

The problem I was trying to solve is the same as the OP - to apply a font face and size to the values in the table cells. If you do it through a table style (as indicated, commented out) or do it through iterating paragraph nodes, in the loop, it DOES NOT WORK if you are adding text using .InsertHtml(). That includes even when adding the builder.InsertParagraph() lines. And even if you use builder.Write() or .Writeln, it will not apply the font size when iterating paragraph nodes, in the loop. I had to apply it directly, within the cells, themselves.

So it takes a very specific set of lines to make this work, which is what I was indicating in my post. You must:

  • Use builder.Write() or Writeln()
  • builder.InsertParagraph() for each cell, after the .InsertCell()
  • Iterate the table’s child paragraph nodes to apply the font face
  • Apply the builder.Font.Size within each cell (doing it within the loop did not work!)

So the code I posted in my other post will work.

The problem I was attempting to resolve is that, if you don’t apply a font to your table, it defaults to Times New Roman, and since we had applied a font earlier in the document using

 Aspose.Words.Font font = builder.Font;
 font.Name = "Calibri";
 font.Size = 12;

… not having the ability to change the font in the table to match that was maddening.

Even if you do this:

builder.InsertStyleSeparator();

Style tableStyle= builder.Document.Styles.Add(StyleType.Table, "tableStyle");
tableStyle.Font.Bold = false;
tableStyle.Font.Size = 12;
tableStyle.Font.Name = "Calibri";

And they try to apply that paragraph style like this:

table1.Style = document.Styles["tableStyle"];

It still does not work!

The only way I could get it to work was through the code in my last post. I hope this is now clear enough, and that the Aspose team can take another look at how font faces and sizes can be applied to table cells, appropriately, without so much hassle and specific ways to attribute it.

@navyjax2 There are several ways to apply font formatting.

  1. Set default font across the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Apply default font across the document.
doc.getStyles().getDefaultFont().setName("Arial");

// build the table. Default font will be used.
Table t = builder.startTable();
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 3; j++)
    {
        builder.insertCell();
        builder.write("Test Text");
    }
    builder.endRow();
}
builder.endTable();

doc.save("C:\\Temp\\out_default_font.docx");

out_default_font.docx (7.3 KB)

As you can see default font is applied to the text in the table.

  1. Specify explicit font formatting in DocumentBuilder:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Explicitly set formatting in DocumentBuilder.
builder.getFont().setName("Calibri");
builder.getFont().setBold(true);

// Build the table.
Table t = builder.startTable();
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 3; j++)
    {
        builder.insertCell();
        builder.write("Test Text");
    }
    builder.endRow();
}
builder.endTable();

doc.save("C:\\Temp\\out_explicit_font.docx");

out_explicit_font.docx (7.4 KB)

  1. Font formatting applied from HTML when insertHtml method is used to insert content:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Explicitly set formatting in DocumentBuilder.
builder.getFont().setName("Calibri");
builder.getFont().setBold(true);

// Build the table.
Table t = builder.startTable();
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 3; j++)
    {
        builder.insertCell();
        // Font set in the HTML will override formatting applied in the DocumentBuilder above.
        builder.insertHtml("<span style='color:blue;font-family:Arial'>Test text</span>");
    }
    builder.endRow();
}
builder.endTable();

doc.save("C:\\Temp\\out_html_font.docx");

out_html_font.docx (7.5 KB)

  1. Configure DocumentBuilder to use builder’s formatting while inserting HTML by setting HtmlInsertOptions.USE_BUILDER_FORMATTING
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Explicitly set formatting in DocumentBuilder.
builder.getFont().setName("Calibri");
builder.getFont().setBold(true);

// Build the table.
Table t = builder.startTable();
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 3; j++)
    {
        builder.insertCell();
        // Font set in DocumentBuilder will be used
        builder.insertHtml("<span>Test text</span>", HtmlInsertOptions.USE_BUILDER_FORMATTING);
    }
    builder.endRow();
}
builder.endTable();

doc.save("C:\\Temp\\out_builder_formatting_html_font.docx");
  1. Apply table formatting using table style. Note, to apply formatting to the table, explicit formatting of content should be reset. Aspose.Words does this automatically, when you apply table style. But you should apply table style after building the table, otherwise explicit formatting will override table style formatting:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

// Explicitly set formatting in DocumentBuilder.
builder.getFont().setName("Calibri");
builder.getFont().setBold(true);

// Build the table.
Table t = builder.startTable();
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 3; j++)
    {
        builder.insertCell();
        builder.insertHtml("<span>Test text</span>");
    }
    builder.endRow();
}
builder.endTable();

// Apply table style after finishing building the table.
t.setStyleIdentifier(StyleIdentifier.TABLE_COLORFUL_1);

doc.save("C:\\Temp\\out_table_style.docx");

out_table_style.docx (7.6 KB)