Format cell text as bold using C#

hi

i have used follwing code to loop table

foreach (Aspose.Words.Tables.Row row in myTable.Rows)

{

foreach (Aspose.Words.Tables.Cell cell in row)

{

//Here i need to make cell to be bold

}

}

In the above code how to make cell value bold

Hi Ajeesh,

Thanks for your query. Please use the following code snippet to bold the text of table’s cells.

Document doc = new Document(MyDir + "table.docx");
Node[] table = doc.GetChildNodes(NodeType.Table, true).ToArray();
foreach (Row row in ((Table)table[0]).Rows)
{
foreach (Cell cell in row.Cells)
{
// Get Runs in this cell.
NodeCollection runs = cell.GetChildNodes(NodeType.Run, true);
// Loop through all runs and make them bold.
foreach (Run run in runs)
{
run.Font.Bold = true;
}
}
}

doc.Save(MyDir + “out.docx”);

Hi Thahir

Thanks for your reply.The above code is working.Now i need to know in the above code how to merge two cells in a row

Hi Ajeesh,

Thanks for your inquiry. You can merge cells either Horizontally or Vertically. Also, please use the following code snippet for merging existing table cells:

Document doc = new Document(@“c:\temp\MergeCells.docx”);

Table tbl = doc.FirstSection.Body.Tables[0];

HorizontallyMergeCells(tbl.Rows[0].Cells[1],
tbl.Rows[0].Cells[2]);

VerticallyMergeCells(tbl.Rows[0].Cells[0],
tbl.Rows[1].Cells[0]);

doc.Save(@“c:\temp\out.docx”);

private static void HorizontallyMergeCells(Cell c1, Cell c2)

{

c1.CellFormat.HorizontalMerge = CellMerge.First;

//Move all content from next cell to previous

foreach (Node child in c2.ChildNodes)

c1.AppendChild(child);

c2.CellFormat.HorizontalMerge = CellMerge.Previous;

}

private static void VerticallyMergeCells(Cell c1, Cell c2)

{

c1.CellFormat.VerticalMerge = CellMerge.First;

//Move all content from bottom cell to top

foreach (Node child in c2.ChildNodes)

c1.AppendChild(child);

c2.CellFormat.VerticalMerge = CellMerge.Previous;

}

I hope, this will help.

Hi

the above code is working for horizontal merging.i tried for that only.But in the merged row one space is created below to the value.Here by am attaching the output document and in the document all "subtotal" rows i made merged that is cells[0] and cells[1] in that row i made merged

Hi Ajeesh,

Thanks for your inquiry. In your case, please update the code snippet as following:

private void HorizontallyMergeCells(Cell c1, Cell c2)
{

c1.CellFormat.HorizontalMerge = CellMerge.First;

//Move all content from next cell to previous

foreach (Node child in c2.GetChildNodes(NodeType.Run,true))

c1.LastParagraph.AppendChild(child);

c2.CellFormat.HorizontalMerge = CellMerge.Previous;

}