Delete current row from within a Field Merging Method

Hi,

I’m currently working on a report that has a table where i’d like to remove a row and swap in a different row when a certain field is null

Here is the code i’m currently using:

if (e.FieldName.Equals("SomeFieldName"))
{
    // string orgID = (string)e.FieldValue;
    if (e.FieldValue == DBNull.Value)
    {
        // this should be a blank row with a message
        Aspose.Words.Tables.Row row = (Aspose.Words.Tables.Row)e.Document.ParentNode.ParentNode;
        Table table = (Table)row.GetAncestor(NodeType.Table);

        // take out regular formatted row and clone the last row of the table, inserting it in here.
        if (row != null)
            row.Remove();

        table.Rows.Add(table.LastRow.Clone(true));
    }
}

This doesn’t seem to be working though. any advice?

Hi Brendan,

Thanks for your inquiry. I can successfully clone last row using the following code:

Document doc = new Document(MyDir + @"inp.docx");
Table tab = doc.FirstSection.Body.Tables[0];
// Clone last Row 10 times
for (int i = 0; i < 10; i++)
{
    Node newRow = tab.LastRow.Clone(true);
    tab.AppendChild(newRow);
}
doc.Save(MyDir + @"16.1.0.docx");

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input document
  • Aspose.Words generated output document showing the undesired behavior
  • Your expected document which shows the correct output. Please create this document using Microsoft Word application.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Thanks. One more thing your example doesn’t include. How do i get the current row? e.Document.ParentNode.ParentNode doesn’t work.

Does it make sense to do this kind of modification within the FieldMerging method? Is there a better way to do this?

Ok, I figured this out. I modified the table after the whole document is created, rather than modifying it in the FieldMerge method. I merged all the cells in the row instead of replacing a row with anther row, achieving the effect I was going for.

Hi Brendan,

Thanks for your inquiry. Yes, it is better to manipulate such Tables after the mail merge operation is completed. You may also use the following approach for getting parent Row:

Row row = (Row) node.GetAncestor(NodeType.Row);
if (row != null)
{
    // your logic
}

Best regards,

One last thing. How would i add text within that merged cell? I don’t really follow how DocumentBuilder is supposed to work in this scenario. Or would it be better to manipulate the DOM of the document?

Hi Brendan,

You can use DocumentBuilder.MoveToCell method if you need to move the cursor to a table cell. After moving the cursor to specified Cell, you can insert Document elements there. You can also get a reference to current Cell by using DocumentBuilder.CurrentNode, iterate through all Cells in current Row and change their DOM properties using the following code:

// Iterate through all cells in the row
foreach (Cell cell in row.Cells)
{
    int cellIndex = row.Cells.IndexOf(cell);
    // Do something here
}

Hope, this helps.

Best regards,

Hi,

Here is the code i’m using. I’m flagging the row to be merged with the text “remove”, then i’m merging the cells and trying to insert new text. Am i missing something?

private void ReplaceBlankRowsFromTableWithMessage(Document doc)
{
    NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true);
    foreach (Table table in allTables)
    {

        foreach (Aspose.Words.Tables.Row row in table)
        {
            foreach (Aspose.Words.Tables.Cell cell in row.Cells)
            {

                if (cell.GetText().Contains("REMOVE"))
                {
                    // drill down to actual cell
                    foreach (Aspose.Words.Tables.Table insideTable in cell.Tables)
                    {
                        foreach (Aspose.Words.Tables.Row insideRow in insideTable)
                        {
                            foreach (Aspose.Words.Tables.Cell insideCell in insideRow.Cells)
                            {
                                if (insideCell.GetText().Contains("REMOVE"))
                                {
                                    insideCell.CellFormat.Shading.BackgroundPatternColor = Color.AliceBlue;

                                    HorizontallyMergeCells(insideRow);

                                    DocumentBuilder builder = new DocumentBuilder(doc);
                                    builder.MoveTo(insideCell.FirstParagraph);
                                    builder.Write("TEST TEXT");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Hi Brendan,

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output document showing the undesired behavior
  • Your expected document which shows the correct output. Please create this document using Microsoft Word application.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Is there a way I can send you this code privately? i’d rather not post it online.

Figured it out:

foreach (Aspose.Words.Tables.Cell insideCell in insideRow.Cells)
{
    if (insideCell.GetText().Contains("REMOVE"))
    {
        HorizontallyMergeCells(insideRow);
        DocumentBuilder builder = new DocumentBuilder(doc);
        builder.MoveTo(insideRow.FirstCell.FirstParagraph);
        builder.Write("TEST TEXT");
    }
}

My problem was that I was inserting the text into insideCell.FirstParagraph, which no longer exists after the merge. I changed this to insideRow.FirstCell.FirstParagraph and it works.

Hi Brendan,

Thanks for your inquiry and sorry for the delay. It is great you were able to find what you were looking for. Please let us know any time you have any further queries.

Secondly, please note that it is safe to attach files in the forum. If you attach your documents/resources here, only you and Aspose staff members can download them. Also, you can zip your resources and send the file to my e-mail as described here:

How to send your license file to an Aspose staff member?

Best regards,