Cannot append text to tables in a Building Block

An System.InvalidOperationException is thrown when attempting to save a document after inserting text to the cells of a table that is part of a BuildingBlock within a GlossaryDocument. The exception message is: 'This operation requires the node to be inside the main document.'

The code block runs without error if the lines appending text are commented out.

The same exception is triggered if an existing table from the main document is copied and inserted to a BuildingBlock in the GlossaryDocument.

Code to Reproduce

static void BuildingBlockBug()
{
    var doc = new Document();
    var glossaryDoc = new GlossaryDocument();
    doc.GlossaryDocument = glossaryDoc;

    BuildingBlock block = new BuildingBlock(glossaryDoc)
    {
        Name = "ProductList",
        Gallery = BuildingBlockGallery.QuickParts,
        Category = "General",
        Behavior = BuildingBlockBehavior.Paragraph,
    };
    glossaryDoc.AppendChild(block);

    Section section = new Section(glossaryDoc);
    block.Sections.Add(section);

    Body body = new Body(glossaryDoc);
    section.AppendChild(body);

    body.AppendParagraph("AAA");

    var newTable = new Table(glossaryDoc);
    body.AppendChild(newTable);
    newTable.EnsureMinimum();

    var headerCell1 = newTable.FirstRow.FirstCell;
    headerCell1.EnsureMinimum();

    var headerCell2 = new Cell(glossaryDoc);
    newTable.FirstRow.Cells.Add(headerCell2);
    headerCell2.EnsureMinimum();

    newTable.StyleIdentifier = StyleIdentifier.ListTable4Accent1;

    // Uncomment the lines below to see the exception
    //headerCell1.FirstParagraph.AppendChild(new Run(glossaryDoc, "Header 1"));
    //headerCell2.FirstParagraph.AppendChild(new Run(glossaryDoc, "Header 2"));

    doc.Save(@"Template.dotx");
}

@bingxie

Cause

The System.InvalidOperationException you are encountering occurs because the table cells you are trying to modify are not part of the main document. In Aspose.Words, certain operations require nodes to be within the main document structure, and attempting to append text to cells in a BuildingBlock within a GlossaryDocument violates this requirement.

Solution

To resolve this issue, you can create a new Run object and append it to the Paragraph of the cell after ensuring that the cell is part of the main document. One effective approach is to first create the table in the main document and then copy it into the BuildingBlock. This ensures that the cells will be valid for modification.

Code

Here’s an updated version of your code that demonstrates how to create the table in the main document first, then copy it to the BuildingBlock:

static void BuildingBlockBug()
{
    var doc = new Document();
    var glossaryDoc = new GlossaryDocument();
    doc.GlossaryDocument = glossaryDoc;

    BuildingBlock block = new BuildingBlock(glossaryDoc)
    {
        Name = "ProductList",
        Gallery = BuildingBlockGallery.QuickParts,
        Category = "General",
        Behavior = BuildingBlockBehavior.Paragraph,
    };
    glossaryDoc.AppendChild(block);

    Section section = new Section(glossaryDoc);
    block.Sections.Add(section);

    Body body = new Body(glossaryDoc);
    section.AppendChild(body);

    body.AppendParagraph("AAA");

    // Create the table in the main document
    var newTable = new Table(doc);
    body.AppendChild(newTable);
    newTable.EnsureMinimum();

    var headerCell1 = newTable.FirstRow.FirstCell;
    headerCell1.EnsureMinimum();

    var headerCell2 = new Cell(doc);
    newTable.FirstRow.Cells.Add(headerCell2);
    headerCell2.EnsureMinimum();

    newTable.StyleIdentifier = StyleIdentifier.ListTable4Accent1;

    // Append text to the header cells
    headerCell1.FirstParagraph.AppendChild(new Run(doc, "Header 1"));
    headerCell2.FirstParagraph.AppendChild(new Run(doc, "Header 2"));

    // Now copy the table to the BuildingBlock
    var copiedTable = newTable.Clone(true);
    section.Body.AppendChild(copiedTable);

    doc.Save(@"Template.dotx");
}

Next Steps

  1. Run the updated code to verify that it resolves the exception.
  2. If you continue to encounter issues, ensure that the table structure is correctly maintained when copying.
  3. Review the Aspose.Words documentation for any additional constraints regarding BuildingBlock and GlossaryDocument usage.

Please let me know if you need further assistance!

Doesn’t work. The suggested code throws exception at body.AppendChild(newTable);

System.ArgumentException: 'The newChild was created from a different document than the one that created this node.'

@bingxie
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-28455

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

The issues you have found earlier (filed as WORDSNET-28455) have been fixed in this Aspose.Words for .NET 25.8 update also available on NuGet.