Split Document into 2 based on Bookmark availability

Hi,
I have a document that has Bookmark in it and I would need to split that document into 2 documents if a specific bookmark exists in it.
Ex. -
Document1 has 6 pages and in 4th Page I have a bookmark (named - Bookmark1) now I will use the below code to find out Bookmark1 exists in the document and once I see Bookmark1 I will have to separate the Document1 into 2 documents (DocumentA - which is the document from Start of the document to Start of Bookmark1 and DocumentB - which is the document from Bookmark1 to End of the document).

//Code to find the particular bookmark exist - 
foreach (var bookmark in doc.Range.Bookmarks.ToList())
{
    if (bookmark.Name == "Bookmark1")
    {
        //Split document into 2 (documentA and documentB and stich it back after processing documentA
    }
}

After splitting the document into 2, I will do some processing of DocumentA and once the processing is done I will have to stich the processed DocumentA and DocumentB to single document Document1.

Could you please help with the snippet how we achieve this? Thanks!

Regards,
Chetan

@KCSR you can use the following code to achieve what you want:

Document doc = new Document("C:\\Temp\\input.docx");
var bookmark = doc.Range.Bookmarks.FirstOrDefault(b => b.Name.ToLower() == "split");
if (bookmark != null)
{
    var nodes = ExtractContent(doc.FirstSection.Body.FirstChild, bookmark.BookmarkStart, true);
    var doc1 = GenerateDocument(doc, nodes);

    nodes = ExtractContent(bookmark.BookmarkStart, doc.LastSection.Body.LastChild, true);
    var doc2 = GenerateDocument(doc, nodes);

    doc1.Save("C:\\Temp\\output1.docx");
    doc2.Save("C:\\Temp\\output2.docx");
}

Please notice that all the auxiliar methods are in the article how to extract selected content between nodes in a document.

You just need to add a modification to the method VerifyParameterNodes, the conditional:

if (startIndex == endIndex)

must be replaced by:

if (startIndex == endIndex && endNode.NodeType != NodeType.BookmarkStart && endNode.NodeType != NodeType.BookmarkEnd)