Free Support Forum - aspose.com

How to do individual cell formatting in table such as font - size, italics, bold, family, color?


#1

I was testing the current library for Aspose words to create multiple tables in my document. However I’m unable to achieve the individual cell level formatting, the CellFormat class does not provide options to set - font size, bold, italics, family. and If I try to do it with the help of DocumentBuilder it applies to all the consecutive elements(even if I use ClearFormatting() in the CellFormat class).

Can this be achieved? can you provide a code sample?


#2

@biswas2019

Thanks for your inquiry. Please use the following code to get desired results. Hope,this helps.

Document doc = new Document();

Table table = new Table(doc);
// Add the table to the document.
doc.FirstSection.Body.AppendChild(table);

Row row = new Row(doc);
row.RowFormat.AllowBreakAcrossPages = true;
table.AppendChild(row);

// We can now apply any auto fit settings.
table.AutoFit(AutoFitBehavior.FixedColumnWidths);

// Create a cell and add it to the row
Cell cell = new Cell(doc);
cell.CellFormat.Width = 80;

// Add a paragraph to the cell as well as a new run with some text.
cell.AppendChild(new Paragraph(doc));

Run cellRun1 = new Run(doc, "Row 1, Cell 1 Text");
cellRun1.Font.Bold = true;
cellRun1.Font.Color = Color.Red;

cell.FirstParagraph.AppendChild(cellRun1);

// Add the cell to the row.
row.AppendChild(cell);


// Create a cell and add it to the row
Cell cell2 = new Cell(doc);
cell2.CellFormat.Width = 80;

// Add a paragraph to the cell as well as a new run with some text.
cell2.AppendChild(new Paragraph(doc));

Run cellRun2 = new Run(doc, "Row 1, Cell 2 Text");
cellRun2.Font.Italic = true;
cellRun2.Font.Color = Color.Blue;
cellRun2.Font.Name = "Arial";

cell2.FirstParagraph.AppendChild(cellRun2);

// Add the cell to the row.
row.AppendChild(cell2);

doc.Save("D:\\temp\\output_19.1.docx");

Please check attached document for your kind reference. output_19.1.zip (5.1 KB)