Clone Row lose the formatting

Hi Team
I am using Aspose Version 14.2. I have facing one problem. I have table which contains 4 rows. Now last 4th row cells contains the formattings eg:- font name: verdana and font-size: 2.
Problem is if I simply clone the last row and append into the table formatting auto inherit to new row. I use the following code for this: this work fine for me.

cloneOfRow = (AsposeWord.Tables.Row) tbl.Rows[4].Clone(true);
tbl.AppendChild(cloneOfRow);

But if I want to change some cell text before clone row it loose the formatting of that cell when it add this as new row. below is code of change text before adding to table:

cloneOfRow = (AsposeWord.Tables.Row) tbl.Rows[4].Clone(true);
cloneOfRow.Cells[0].FirstParagraph.Runs.Clear();
cloneOfRow.Cells[0].FirstParagraph.AppendChild(new Aspose.Words.Run(doc, "Hello"));
tbl.AppendChild(cloneOfRow);

How can I keep the formatting same for new row even if I change the text of new clone row.?
Thanks
Deepak

Hi Deepak,

Thanks for your inquiry. Please attach 1) your input Word document, 2) output document showing the undesired behavior, 3) expected document which shows the correct formatting and 4) complete code you’re using on your end here for testing. We will investigate the issue on our end and provide you more information.

Best regards,

Hi Awais,
Below are the details
Default Table

R1
****
Header1 Header2
R2 Header1 Header2

After Running Code (Note: Check the first cell text of last row “R21” formatting. It should like “R2” text)

R1 Header1 Header2
R2 Header1 Header2
R21 Header1 Header2

Actually Required

R1 Header1 Header2
R2 Header1 Header2
R21 Header1 Header2

below is code we are using for this:

protected void Button1_Click(object sender, EventArgs e)
{
    tbl = builder.StartTable();
    // 1st row
    builder.InsertCell();
    AddHiddenColumnData("R1", 20);
    builder.InsertCell();
    AddColumnData(false, true, "Header1", 120);
    builder.InsertCell();
    AddColumnData(false, true, "Header2", 100);

    builder.EndRow();
    ////2nd row
    builder.InsertCell();
    AddHiddenColumnData("R2", 40);
    builder.InsertCell();
    AddColumnData(false, true, "Header1", 120);
    builder.InsertCell();
    AddColumnData(false, true, "Header2", 100);
    builder.EndRow();
    builder.EndTable();
    tbl.AutoFit(AsposeWord.Tables.AutoFitBehavior.FixedColumnWidths);
    doc.Save(@"D:\DealMemo_Template.doc"); // it will overwrite if file exists
}
private void AddHiddenColumnData(string stext, double width)
{
    builder.Font.Name = "Tahoma";
    builder.Font.Color = System.Drawing.Color.Red;
    builder.Font.Bold = true;
    builder.Font.Size = 7;
    builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.White;
    builder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(width);
    builder.Font.Underline = AsposeWord.Underline.None;
    builder.Writeln(stext);
}
private void AddColumnData(bool isTableName, bool isSubHeader, string stext, double width)
{
    builder.Font.Name = "Tahoma";
    builder.Font.Color = System.Drawing.Color.Black;
    if (!isTableName)
    {
        builder.Font.Size = 8;
        builder.Font.Bold = false;
    }
    else
    {
        builder.Font.Size = 12;
        builder.Font.Bold = true;
    }
    builder.CellFormat.PreferredWidth = AsposeWord.Tables.PreferredWidth.FromPoints(width);
    AsposeWord.Tables.CellFormat cellFormat = builder.CellFormat;
    cellFormat.FitText = true;
    cellFormat.WrapText = true;
    builder.CellFormat.TopPadding = 0;
    builder.CellFormat.LeftPadding = 5;
    builder.CellFormat.RightPadding = 5;
    builder.CellFormat.BottomPadding = 0;
    builder.CellFormat.VerticalAlignment = AsposeWord.Tables.CellVerticalAlignment.Top;
    builder.ParagraphFormat.Alignment = AsposeWord.ParagraphAlignment.Left;
    if (isSubHeader)
    {
        builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Beige;
        builder.Font.Underline = AsposeWord.Underline.Single;
    }
    else
    {
        builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.White;
        builder.Font.Underline = AsposeWord.Underline.None;
    }
    builder.Writeln(stext);
}
protected void btnClone_Click(object sender, EventArgs e)
{
    doc = new AsposeWord.Document(@"D:\DealMemo_Template.doc");
    builder = new AsposeWord.DocumentBuilder(doc);
    tbl = doc.FirstSection.Body.Tables[0];
    Aspose.Words.Tables.Row cloneOfRow;
    cloneOfRow = (Aspose.Words.Tables.Row) tbl.Rows[tbl.Rows.Count - 1].Clone(true);
    cloneOfRow.Cells[0].FirstParagraph.Runs.Clear();
    cloneOfRow.Cells[0].FirstParagraph.AppendChild(new Aspose.Words.Run(doc, "R" + tbl.Rows.Count + 1));
    tbl.AppendChild(cloneOfRow); //ADD AT LAST ROW
    doc.Save(@"D:\DealMemo_TemplateOutput.doc");
}

Hi Deepak,

Thanks for the additional information. You have applied the formatting on the Run node itself. In this case, instead of removing that Run, you just need to replace its text with new text as follows:

builder = new Aspose.Words.DocumentBuilder(doc);
tbl = doc.FirstSection.Body.Tables[0];
Aspose.Words.Tables.Row cloneOfRow;
cloneOfRow = (Aspose.Words.Tables.Row) tbl.Rows[tbl.Rows.Count - 1].Clone(true);
// cloneOfRow.Cells[0].FirstParagraph.Runs.Clear();
cloneOfRow.Cells[0].FirstParagraph.Runs[0].Text = "R" + tbl.Rows.Count + 1;
// cloneOfRow.Cells[0].FirstParagraph.AppendChild(new Aspose.Words.Run(doc, "R" + tbl.Rows.Count + 1));
tbl.AppendChild(cloneOfRow); //ADD AT LAST ROW

I hope, this helps.

Best regards,