How to assign value to Cell in table row

Hi
am using the following code to append value to cell

Aspose.Words.Tables.Table table = objWordDoc.FirstSection.Body.Tables[0];
Aspose.Words.Tables.Table NewTable = (Aspose.Words.Tables.Table) table.Clone(true);
Section section = new Section(objWordDoc);
objWordDoc.AppendChild(section);
if (flag == false)
{
    objWordDoc.Sections[0].PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
    section.PageSetup.SectionStart = SectionStart.Continuous;
    flag = true;
}
else
{
    section.PageSetup.SectionStart = SectionStart.NewPage;
}
section.PageSetup.Orientation = Aspose.Words.Orientation.Landscape;
section.PageSetup.LeftMargin = objWordDoc.FirstSection.PageSetup.LeftMargin;
Body body = new Body(objWordDoc);
section.AppendChild(body);
body.AppendChild(NewTable);

for (int rowCnt = 0; rowCnt <= 10; rowCnt++)
{

    for (int cellCnt = 0; cellCnt <counter; cellCnt++)
    {
        NewTable.Rows[rowCnt].Cells[cellCnt].FirstParagraph.AppendChild((new Run(objWordDoc, "newValue")));
    }
}

Its working fine,But my problem is affter doing this,font is not retaining the parent table cell font format to child table cells.
Am attaching the output and i have 2 tables one is parent and second in child,In output you can see the child table not retaining the font formats like FONT BOLD,FONT SIZE…

Hi Ajeesh,

Thanks for your inquiry. I have modified your code, please see the highlighted section of code. Hope this helps you. Please let us know if you have any more queries.

Document objWordDoc = new Document(MyDir + "AppendOutPut.docx");

DocumentBuilder builder = new DocumentBuilder(objWordDoc);


body.AppendChild(NewTable);
for (int rowCnt = 0; rowCnt <= 5; rowCnt++)
{
    for (int cellCnt = 0; cellCnt <4; cellCnt++)
    {
        if (NewTable.Rows[rowCnt].Cells[cellCnt].FirstParagraph.Runs.Count == 0)
        {
            builder.MoveTo(NewTable.Rows[rowCnt].Cells[cellCnt].FirstParagraph);
            Aspose.Words.Font font = builder.Font;
            Run run = new Run(objWordDoc, "newValue");
            run.Font.Name = font.Name;
            run.Font.Size = font.Size;
            NewTable.Rows[rowCnt].Cells[cellCnt].FirstParagraph.AppendChild(run);
        }
        else
        {
            Aspose.Words.Font font = NewTable.Rows[rowCnt].Cells[cellCnt].FirstParagraph.Runs[0].Font;
            Run run = new Run(objWordDoc, "newValue");
            run.Font.Name = font.Name;
            run.Font.Size = font.Size;
            run.Font.Bold = font.Bold;
            NewTable.Rows[rowCnt].Cells[cellCnt].FirstParagraph.AppendChild(run);
        }
    }
}
objWordDoc.Save(MyDir + "out.docx");

Hi Tahir Manzoor
Thanks for your reply and your solution working fine.
I have another problem like ,if you see my output template,there is different in left margin of my first table and second table,that is my Template table and Output table.How to assign the same left align to my output table
Regards
Ajeesh M J

Hi Ajeesh,

Thanks for your inquiry. Your document has two sections with different margins and gutter setting. See the attachment. Please use the following code snippet to set the margins of document’s sections. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document(MyDir + "in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);
foreach(Section section in doc.Sections)
{
    if (doc.FirstSection != section)
    {
        section.PageSetup.LeftMargin = doc.FirstSection.PageSetup.LeftMargin;
        section.PageSetup.TopMargin = doc.FirstSection.PageSetup.TopMargin;
        section.PageSetup.RightMargin = doc.FirstSection.PageSetup.RightMargin;
        section.PageSetup.BottomMargin = doc.FirstSection.PageSetup.BottomMargin;
        section.PageSetup.Gutter = doc.FirstSection.PageSetup.Gutter;
    }
}
doc.Save(MyDir + "out.docx");