Table.LastRow.Clone doesn't clone font formatting?

Hi,

It seems the “.Clone” method, when used on a row inside a table, doesn’t copy the font formatting (bold, underlined, …) if the row contains no text. If the row contains formatted text the “.Clone” method will copy the text with its formatting intact, but if the row contains font formatting but no text the formatting will be lost. Is there a fast way to include the font formatting, which could differ for each cell inside the row, into the newly cloned row?

I’ll add a part of my code to clarify my question:

// Loop through rows
for (int i = 0; i <rows.Length; i++)
{
    string row = rows[i];

    // Split row into cells
    string[] cells = row.Split(new char[]
    {
        '\
        t'
    });

    Row NewRow = (Row) parentTable.LastRow.Clone(true);
    for (int j = 0; j <cells.Length; j++)
    {
        run = new Run(doc, "This will be replaced in code");
        if (j == (cells.Length - 1)) cells[j] = cells[j].Substring(0, cells[j].Length - 1);
        run.Text = (string) cells[j];
        parentTable.Rows[totalRows].Cells[j].Paragraphs[0].Runs.Add(run);
    }
    parentTable.Rows.Add(NewRow);
    totalRows += 1;
}
parentTable.LastRow.Remove();

This code basicly fills a table with data.

Thanks in advance,

Aleix

Hi Aleix,
Thanks for your request. Actually, font formatting of text is defined in Run. Since in your code you are creating a Run of text from scratch, it will use default formatting. I think, in your case, you can try using DocumentBuilder to insert text. DocumentBuilder will inherit formatting what you move it into the cell. See the following code:

DocumentBuilder builder = new DocumentBuilder(doc);
// Loop through rows
for (int i = 0; i <rows.Length; i++)
{
    string row = rows[i];
    // Split row into cells
    string[] cells = row.Split(new char[]
    {
        '\t'
    });
    Row NewRow = (Row) parentTable.LastRow.Clone(true);
    parentTable.Rows.Add(NewRow);
    for (int j = 0; j <cells.Length; j++)
    {
        builder.MoveTo(parentTable.Rows[totalRows].Cells[j].Paragraphs[0]);
        builder.Writeln((string) cells[j]);
    }
    totalRows += 1;
}
parentTable.LastRow.Remove();

Also, I think in your case, you can use Mail Merge with regions:
https://docs.aspose.com/words/java/types-of-mail-merge-operations/
Best regards,

Hi Alexey,

The builder does exactly what I require! You really helped me out with this tip.
Keep up the good work.

Thanks a lot & take care.

Aleix