Duplicating Section breaks when copying nodes

Hi Guys and Ladies,

I have an issue which I’m hoping you can help me resolve. In the first instance I’ll explain what I am doing (with attached document) but if the explanation is not clear then I will craft some code that exemplifies the issue (In our system the code is embedded in a lot of other code so I can’t simply send it.)

The crux of the issue is as follows:

  1. I open a word document
  2. I search that document for a bit of text ( <LAYOUT_SECTION> )
  3. I then add each node I find after that to a collection until I find a node containing </LAYOUT_SECTION>. If I reach the end of a section without finding the closing text I move to the next section’s first paragraph and continue adding to the collection.
  4. Once I have this collection I find a different bit of text ( <TRAILER_SECTION> ) and then working through the collection insert the nodes before it.

The issue is that I also want to be able to copy the change of section and so add a new section where it should have been in the order.

I have tried adding an empty clone of the section (without children) to the node list and this has worked; the trouble is when I insert it (at step 4 above) it does not cause the current section to split but rather adds a new section at the end of the document (after the end of the <TRAILER_SECTION>).

So the crux of the question is how do I split a section at a specific node please?

Many Thanks,

Stan

Hi Stan,


Thanks for your inquiry. As per my understanding, you want to extract content with section break from the document and insert it into another position. We suggest you please move the cursor to desired location while inserting the nodes into the document and insert the section break. Hope this helps you.

If you still face problem, please share your output document that shows the undesired behavior along with some more detail about your query. We will then provide you more information on this along with code.

Hi Tahir,

The issue is slightly more specific than just copying and pasting nodes.

Yes I make a list of nodes to clone, and yes I then do insert those nodes elsewhere in the same document, but the issue I have may be best described as "How do I split an existing section into two sections at a given point?"

Thank you,

Stan

Hi Stan,

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

  • Do you want to split the contents between tags and or split a document's Section into two Sections?
  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the undesired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to 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.

Hi Tahir,

In response to your question; I would like to take an existing section and split it in two at a particular node, keeping all content before an after it in what would be the correct sections.

Thank you,

Stan

@modern.gov,

Thanks for your inquiry. Unfortunately, your question isn’t clear enough therefore we request you to please share the requested detail in my previous. This will help us to understand your scenario, and we will be in a better position to address your concerns accordingly.

Best Regards,
Tahir Manzoor

@tahir.manzoor

It will take me a while to create an example program because it forms part of a much larger program, in the interim please see this explanation which I hope will explain it clearer:

I have a document with one section and 4 paragraphs:

Source.png (26.4 KB)

At a given node (in this example paragraph 3) I want to end the section it is in and start a new section (this can be any type of section (new page, continuous, odd, even) and keep all the paragraphs after that in the new section.

Output.png (36.1 KB)

In effect, splitting the section in to two sections at a given node (para3 in this example).

Is that clearer?

Thank you,

Stan

@modern.gov,

Thanks for sharing additional information. As suggested above you can use DocumentBuilder class to move cursor to a particular paragraph in Document and then simply insert section break as following. It will help you to accomplish the task.

documentBuilder.InsertBreak(BreakType.SectionBreakNewPage);

Best Regards,

Hi Tilal,

I was not able to implement the above suggestion in a way that worked with the existing code. As such I have done as required by Tahir and produced a working example of the way we are using the code and the issue we are having, this is attached.

Please note: I have removed the Aspose DLL as it was too large when zipped, however the referenced version is Aspose.Words.dll version 16.7.

MergeDocumentDemo.zip (82.5 KB)

Thank you,

Stan

@modern.gov

Thanks for sharing the additional information. You can achieve your requirement using following workflow. It will help you to accomplish the task. If you face any issue then please let us know for further assistance.

  1. Add bookmark after section break in source document.
  2. Extract contents between nodes and create a new document(your existing code).
  3. Insert Section breaks at the places of bookmarks in final document.

Hi Tilal,

Thanks for this, however there are two bits I’d need further assistance with:

  1. The type of section break could vary (user defined in the document) so as per the explanatory comments in the example we would need to be able to store the information about that section (margins, type (new page, continuous, even page, odd page), etc.) and ensure we insert the same type again. Please can you clarify within the frame of this example how this could be done.

  2. On the assumption that point 1 does not change you belief that the above workflow holds the solution; Ideally I don’t want to parse the document in advance, but instead would like to add a marker to my ‘nodes’ variable, this would ideally be done at line 84 (FrmDemo.cs) which is where we have moved to a new section in ExtractContent. I tried creating a Bookmark object but I cannot discern what the constructor would be to add it to my nodes list. Of course this may all be circumvented by an alternative solution as per point 1.

Thank you,

Stan

Hi Tilal,

Further to this I have tried another option; where I switch to the next section I create a clone of that section (without children); and was able to add it to my ‘nodes’ list. This preserves the details of that section, however I cannot simply insert it using InsertAfter as this throws and exception. If I used Builder.InsertBreak to insert a break could I then copy the properties of the one I’ve stored to that new one?

Attached is a modified version of the project.MergeDocumentDemo.zip (140.9 KB)

@modern.gov

Thanks for your additional information. We are looking into the issue and will share our findings soon.

@modern.gov,

Thanks for your patience. You want to extract the contents from the document that contains the section break. Please use the following method to extract the contents with section break. We have modified the code example and attached it with this post for your kind reference. Please also check the output document.

Output.zip (6.9 KB)
MergeDocumentDemo_modified.zip (84.2 KB)

public Document ExtractContentBetweenNodes(Node startNode, Node endNode)
{
    // Check whether start and end nodes are children of boby
    if (startNode.ParentNode.NodeType != NodeType.Body || endNode.ParentNode.NodeType != NodeType.Body)
        throw new Exception("Start and end nodes should be children of main story(body)");

    // Clone the original document,
    // this is needed to preserve styles of the original document
    Document srcDoc = (Document)startNode.Document;

    Document dstDoc = srcDoc.Clone();
    dstDoc.RemoveAllChildren();

    // Now we should copy parent nodes of the start node to the destination document
    // these will Section, Body etc.
    // First we should get list of parents of the start node
    Node firstSect = dstDoc.ImportNode(startNode.GetAncestor(NodeType.Section), true, ImportFormatMode.UseDestinationStyles);
    dstDoc.AppendChild(firstSect);

    //Remove content from the section, except headers/footers
    dstDoc.LastSection.Body.RemoveAllChildren();

    Node currNode = startNode;
    Node dstNode;

    // Copy content
    while (!currNode.Equals(endNode))
    {
        //Import node
        dstNode = dstDoc.ImportNode(currNode, true, ImportFormatMode.UseDestinationStyles);
        dstDoc.LastSection.Body.AppendChild(dstNode);

        //move to the next node
        if (currNode.NextSibling != null)
            currNode = currNode.NextSibling;

        //Move to the next section
        else
        {
            Node sect = currNode.GetAncestor(NodeType.Section);
            if (sect.NextSibling != null)
            {
                dstNode = dstDoc.ImportNode(sect.NextSibling, true, ImportFormatMode.UseDestinationStyles);
                dstDoc.AppendChild(dstNode);
                dstDoc.LastSection.Body.RemoveAllChildren();
                currNode = ((Section)sect.NextSibling).Body.FirstChild;
            }
            else
            {
                break;
            }
        }
    }
    return dstDoc;
}

Hi Tahir,

Thank you for your example; I have now been able to integrate it in to our method in our main product and the results are good.

Unfortunately in testing one document has been causing us a great deal of issues; specifically it causes an null exception during the Aspose InsertDocument function. I’ve reattached the project; now modified to use the document in question. Please can you have a look? I have tried all three ImportFormatMode options with no luck; and because this is a customisable option in our system it needs to be able to work for all three of the modes.

Thank you and with kind regards,

Stan
MergeDocumentDemo_modified2.zip (332.6 KB)

@modern.gov,

Thanks for your inquiry. Please call Document.UpdatePageLayout method before inserting the content as shown below. Please let us know if you have any more queries.

Document newDoc = ExtractContentBetweenNodes(m_oStartNode, m_oEndNode);
            
newDoc.UpdatePageLayout();
// Reinsert the cloned nodes in m_oContent but above the trailer section.
InsertContent(newDoc);

Hi Tahir,

That seems to have done the job, thank you for your help.

Stan