Hi! We have a problem where we don’t get the expected font when adding text to tables. Previously we were using Word Interop for integration with Word, and text added to the table would get the font set on the table in the word document. When we try to do the same with Aspose.Words, we get the default paragraph font instead which is not the expected font. One solution would be to hardcode the font for text added to the table, but it would be very good if we could avoid that.
To ensure that the text added to tables in Aspose.Words retains the expected font settings, you can utilize the formatting properties of the table, row, or cell. Unlike Word Interop, where the font settings are automatically inherited from the table, in Aspose.Words, you may need to explicitly set the font for the text you are adding.
Here’s how you can achieve this:
-
Set the Font for the Table: You can define a default font for the entire table, which will apply to all cells unless overridden.
-
Set the Font for Individual Cells: If you want specific cells to have different fonts, you can set the font properties directly on the cell or its paragraphs.
Here’s a code example demonstrating how to set the font for a table and its cells:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Start a table
Table table = builder.StartTable();
// Set default font for the table
builder.Font.Name = "Arial";
builder.Font.Size = 12;
// Insert a cell with text
builder.InsertCell();
builder.Write("Row 1, Cell 1");
// Change font for the next cell
builder.Font.Name = "Times New Roman";
builder.InsertCell();
builder.Write("Row 1, Cell 2");
builder.EndRow();
// Reset font to default for the next row
builder.Font.Name = "Arial";
builder.InsertCell();
builder.Write("Row 2, Cell 1");
builder.InsertCell();
builder.Write("Row 2, Cell 2");
builder.EndRow();
builder.EndTable();
doc.Save("TableWithFonts.docx");
In this example, the first cell uses “Arial” while the second cell uses “Times New Roman”. You can adjust the font settings as needed for each cell or row.
If you want to apply styles or formatting to the entire table, you can also create a TableStyle
and apply it to the table, which can help maintain consistency across your document.
For more advanced formatting options, consider using the CellFormat
and RowFormat
properties to set specific styles for cells and rows, including padding, borders, and shading, which can further enhance the appearance of your tables.
If you encounter any issues or need further assistance, feel free to ask!
@mattias.sundstrom Could you please attach your template and provide the code you use to fill the table with data? We will check the issue and provide you more information.