Insert Document With Sections Issue

Hello,

I have an issue with Aspose.Words when I want to insert one document into another. Structure of the parent document is that it has only one content control (INCLUDE_DOCUMENTS) in front of which I want to insert another document which has 2 sections and for each section Header is different.
After the document is inserted last Header of inserted document is lost. Please check the code snippet below and suggest a solution if there is one.
I am using Aspose.Words 16.8.0 in production, but in this example same outcome is with version 18.3.0.
Please find attached documents for testing resource.zip (27.6 KB)

public class Program
{
    public static void Main(string[] args)
    {
        try
        {
            Document parentDocument = new Document("./resource/INCLUDED_MASTER.docx");
            Document documentToInsert = new Document("./resource/SectionHeaders.docx");
            StructuredDocumentTag nodeToInsertBefore = GetFirstChild(parentDocument.Document, @"^INCLUDE_DOCUMENTS$");
            if (nodeToInsertBefore != null)
            {
                DocumentBuilder builder = new DocumentBuilder(parentDocument);
                Paragraph newParagraph = new Paragraph(parentDocument);
                InsertNodeBefore(newParagraph, nodeToInsertBefore);
                builder.MoveTo(newParagraph);

                documentToInsert.EnsureMinimum();
                builder.InsertDocument(documentToInsert, ImportFormatMode.UseDestinationStyles);
                parentDocument.LastSection.PageSetup.SectionStart =
                    documentToInsert.LastSection.PageSetup.SectionStart;

                // Removes last empty paragraph added by Aspose on inserted content
                Paragraph lastInsertedParagraph =
                    nodeToInsertBefore.PreviousSibling as Paragraph;
                if (lastInsertedParagraph != null &&
                    string.IsNullOrEmpty(lastInsertedParagraph.GetText().Trim()))
                {
                    RemoveNodeSafely(lastInsertedParagraph);
                }

                // Removes empty paragraph placeholder and SDT insert placeholder
                RemoveNodeSafely(newParagraph);
                RemoveNodeSafely(nodeToInsertBefore);

                // Saves merged document
                builder.Document.Save("Output.docx");
            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

    /// <summary>
    /// Removes node from parent node
    /// </summary>
    /// <param name="node">Node to be removed</param>
    public static void RemoveNodeSafely(Node node)
    {
        if (node?.ParentNode != null)
        {
            node.Remove();
        }
    }

    /// <summary>
    /// Gets the first child node that matches search criteria
    /// </summary>
    /// <param name="compositeNode">Composite node</param>
    /// <param name="searchCriteria">Search criteria</param>
    /// <returns>Structured document tag or null</returns>
    public static StructuredDocumentTag GetFirstChild(CompositeNode compositeNode, string searchCriteria)
    {
        Regex regex = new Regex(searchCriteria);

        List<StructuredDocumentTag> allNodes = compositeNode.GetChildNodes(NodeType.StructuredDocumentTag, true)
            .Cast<StructuredDocumentTag>().ToList();

        return allNodes.FirstOrDefault(it => regex.IsMatch(it.Title));
    }

    /// <summary>
    /// Inserts node before referenced one
    /// </summary>
    /// <param name="newNode">Node to be inserted</param>
    /// <param name="referenceNode">References node</param>
    public static void InsertNodeBefore(Node newNode, Node referenceNode)
    {
        if (referenceNode?.ParentNode != null && newNode != null)
        {
            // In case when Paragraph has to be inserted into parent node that is also Paragraph, than
            // take next parent node - that wll be Body
            if (newNode.NodeType == NodeType.Paragraph &&
                newNode.NodeType == referenceNode.ParentNode.NodeType)
            {
                // Insert only in case if parent exists
                referenceNode.ParentNode.ParentNode?.InsertBefore(newNode, referenceNode.ParentNode);
            }
            else
            {
                referenceNode.ParentNode.InsertBefore(newNode, referenceNode);
            }

        }
    }
}

@knez032,

We are working on your query and will get back to you soon.

@knez032,

Thanks for being patient. We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-16591. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@knez032,

Regarding WORDSNET-16591, we have completed the analysis of this issue and has come to a conclusion that Aspose.Words mimics the behavior of MS Word. Please see the following details:

Please see attached OutputWord.docx (OutPutWord.zip (11.3 KB)). Aspose.Words and MS Word have following logic:

Source document has Section (let’s call it as section0) without headers and footers.
Inserted document has 2 Sections (let’s call them as section1 and section2) with different headers and footers.

After insertion the destination document has 2 sections - section1->section0.

While inserting section2 - logic copies section2 nodes only without section data and headers and footers.

Here is how Aspose.Words’ logic works when looping sections:

Inserts specified section before parent section of specified paragraph and moves sections content between sections in accordance with MS Word behavior:
If specified section is the last section, its content is moved to the point before specified paragraph.
Otherwise all nodes till specified paragraph are moved to the specified section.

The issues you have found earlier (filed as WORDSNET-16591) have been fixed in this Aspose.Words for .NET 18.7 update and this Aspose.Words for Java 18.7 update.