We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding a new rows to a existing table in .NET Aspose Word

Hi All,
We are using Aspose Word for .Net of version 5.2.2.0
Our requirement is that, if a bookmark is present in a table row, I need to create create a N number of rows under that bookmark and later that bookmark should be removed.
Like this there may be N number of bookmark, but all will be they present in a same row in a Table…
So, I am looping through each of the bookmark in table… and create as many rows I need need depending on some specification and populate values for the cells in …
(see attachment)
I am using the below code to achieve this… I am able to populate values…but boder is not appering for the newly created rows and cells… Am I missing anything here

int numberOfCells = 0;
int parentCellIndex = 0;
for (int index = 0; index < numberOfRows; index++)
{
    string instanceText = string.Empty;
    if (subEntityBases[index].DataFields.Contains(fieldDefinition.Id))
    {
        instanceText = GetText()..some logic is written here to obtain the cell text
    }
    Aspose.Words.DocumentBuilder documentBuilder = new Aspose.Words.DocumentBuilder(_asposeDocument);
    Aspose.Words.Tables.Cell parentCell = (Aspose.Words.Tables.Cell)_currentBookMark.BookmarkStart.GetAncestor(Aspose.Words.NodeType.Cell);
    if (parentCell != null)
    {
        Aspose.Words.Tables.Row parentRow = parentCell.ParentRow;
        if (index > 1)
        {
            parentRow = (Aspose.Words.Tables.Row)parentRow.NextSibling;
        }
        Aspose.Words.Tables.Table parnetTable = parentRow.ParentTable;
        //set the flag to indicate bookmark text is replaced
        _isBookMarkTextReplaced = true;
        if (index == 0)
        {
            _currentBookMark.Text = instanceText;
            numberOfCells = parentRow.Cells.Count;
            parentCellIndex = parentRow.IndexOf(parentCell);
        }
        else
        {
            if (parentRow.NextSibling != null)
            {
                Aspose.Words.Tables.Row nextRow = (Aspose.Words.Tables.Row)parentRow.NextSibling;
                // row already exsists. No need to create it...
                Aspose.Words.Tables.Cell cell = nextRow.Cells[parentCellIndex];
                documentBuilder.MoveTo(cell.FirstParagraph);
                documentBuilder.Write(instanceText);
            }
            else
            {
                // row not exits .. create the row
                Aspose.Words.Tables.Row newRow = new Aspose.Words.Tables.Row(parnetTable.Document);
                for (int cellIndex = 0; cellIndex < numberOfCells; cellIndex++)
                {
                    Aspose.Words.Tables.Cell cell = new Aspose.Words.Tables.Cell(parnetTable.Document);
                    Aspose.Words.Paragraph paragraph = new Aspose.Words.Paragraph(parnetTable.Document);
                    cell.AppendChild(paragraph);
                    newRow.AppendChild(cell);
                }
                parnetTable.AppendChild(newRow);
                documentBuilder.MoveTo(newRow.Cells[parentCellIndex].FirstParagraph);
                documentBuilder.Write(instanceText);
            }
        }

Thanks in advance
Ranganatha

Hi
Thanks for your inquiry. I think in your case the best solution is using mail merge with region as described here:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
Please let me know if you can’t use mail merge and we will try to find another solution for you.
Best regards.

Hi,
I think I can’t use mail merge here… because data for the each cell(in a collumn) under the bookmark is coming from different data source…
Thanks
Ranganatha

Hi,
Can I use mail merge for each column in the table…
Thanks
Ranganatha

Hi
Thanks for your inquiry. I think that you can build your own data source. For example you can use DataTable or IMailMergeDataSource. Please see the following link for more information.
https://reference.aspose.com/words/net/aspose.words.mailmerging/imailmergedatasource/
I hope this could help.
Best regards.