Joining and Splitting Word Documents

Hi,
I have a requirement to combine a number of documents.
The user will be editing the joined document (one or more pages) and will close the document.
I want to split the combined document back to the individual documents so that I can persist any changes made to the combined document.
I sae the code for combining the documents to a single one. (https://docs.aspose.com/words/net/insert-and-append-documents/)
But I couldn’t find a way to split the combined document to its original documents.
Is it possible to achive it using Aspose Words?
Thanks!

Hi
Thanks for your request. To split a document back into parts, you need to have a marker of the subdocument start in your master document. For instance, here you can find a code example how you can split document into parts by user defined strings:
https://forum.aspose.com/t/99344
The delimiter can be anything – user defined string, bookmark, paragraphs with some specific style, section break (if your source documents consist of only one section) etc.
Best regards,

Thanks for the link.
I need to use a separator between the documents that isn ot visible when combined.
A bookmark might be OK, but I need to name them as Bookmark1, Bookmark2 etc as the bookmark name has to be unique.
What is the best way to achieve this so that the combined document has got the same look as the original and also that makes the splitting easier.
Please advise.
Thanks!

Hi Sudheer,
Thanks for your request. I think you should use bookmarks in your case. Before concatenating documents you should insert a bookmark at the beginning of each document. Then you can use these bookmarks as delimiters to split the document back into parts. For example I created a simple code example for you:

[Test]
public void Test001()
    {
        Document doc = CombineDocuments(@"Test001\indocs");
        doc.Save(@"Test001\out.doc");
    }
    [Test]
public void Test002()
{
    Document doc = new Document(@"Test001\out.doc");
    SplitDocument(doc, @"Test001\outdocs");
}
///
/// Combines documents into one document.
///
/// Folder that contains subdocuments.
private static Document CombineDocuments(string inputFolder)
{
    int docIdx = 0;
    // Get collection of files in the input folder.
    // It is supposed that folder contains document in supported formats.
    string[] docs = Directory.GetFiles(inputFolder);
    // Create a master document.
    Document doc = new Document();
    doc.RemoveAllChildren();
    foreach(string docFileName in docs)
    {
        Document subDocument = new Document(docFileName);
        // Create a DocumentBuilder and insert a bookmarkat the beggining of the document.
        DocumentBuilder builder = new DocumentBuilder(subDocument);
        string bookmarkName = string.Format("SumDocumentMarker_{0}", docIdx++);
        builder.StartBookmark(bookmarkName);
        builder.EndBookmark(bookmarkName);
        // Append a sub document to the main document.
        doc.AppendDocument(subDocument, ImportFormatMode.UseDestinationStyles);
    }
    return doc;
}
///
/// Splits the document into parts.
///
/// Document that should be split.
/// Folder where sub document shoudl be saved.
private void SplitDocument(Document doc, string outputFolder)
{
    int docIdx = 0;
    Document subDocument = new Document();
    subDocument.RemoveAllChildren();
    NodeImporter importer = new NodeImporter(doc, subDocument, ImportFormatMode.UseDestinationStyles);
    while (doc.HasChildNodes)
    {
        // Copy the last section of the main document to the sub document
        // And check whether this sectioncontains a sub document marker (bookmark).
        subDocument.PrependChild(importer.ImportNode(doc.LastSection, true));
        doc.LastSection.Remove();
        if (ContainsSubDocumentMarker(subDocument))
        {
            // Save sub document.
            subDocument.Save(Path.Combine(outputFolder, string.Format("out_{0}.doc", docIdx++)));
            // Create new Document and importer.
            subDocument = new Document();
            subDocument.RemoveAllChildren();
            importer = new NodeImporter(doc, subDocument, ImportFormatMode.UseDestinationStyles);
        }
    }
}
private static bool ContainsSubDocumentMarker(Document doc)
{
    foreach(Bookmark bookmark in doc.Range.Bookmarks)
    {
        if (bookmark.Name.StartsWith("SumDocumentMarker_"))
        {
            bookmark.Remove();
            return true;
        }
    }
    return false;
}

Hope this helps.
Best regards,