Adding a Section to a Bookmark

Is it possible to insert a section object from a Aspose.Word document object into a specified bookmark of another Aspose.Word document? I tried the add section function, but this only appends the section to the end.

The problem is that a bookmark is usually at the inline (inside paragraph) level. The document is represented as a tree so the bookmark is sort of deep in the tree like this Section\Body\Paragraph\Bookmark. To insert a completely new section at that level requires some algorithm that will properly split the tree and move the nodes around. Such functionality is available in DocumentBuilder at the moment. You can do this:

DocumentBuilder.MoveToBookmark(bookmarkName);
DocumentBuilder.InsertBreak(sectionBreakType);

This creates a new Section object, adds it to the tree and moves the surrounding nodes appropriately.
You can access the newly created section using DocumentBuilder too. Ideally it should have DocumentBuilder.CurrentSection property (I will add it later), but at the moment use
DocumentBuilder.CurrentParagraph.ParentSection.

This works perfectly. Thanks for your help.

I cannot find the DocumentBuilder.InsertSection(sectionBreakType); method. I have downloaded the current version of Aspose.Words. What am I missing.
Basically I just want to insert a aspose doc into another Aspose doc
Cheers Bruce

I have corrected the previous post. Should be

DocumentBuilder.InsertBreak(BreakType);

And here is the code for appending one document to another:

private void AppendDoc(Document dstDoc, Document srcDoc)
{

    for (int i = 0; i < srcDoc.Sections.Count; i++)
    {
        // First need to import the section into the destination document,
        // this translates any document specific lists or styles.
        Section section = (Section)dstDoc.ImportNode(srcDoc.Sections, true);
        dstDoc.Sections.Add(section);
    }
}

Hi
You amy want to update your FAQ in the help file as it also refers to DocumentBuild.InsertSection incorrectly
Cheers Bruce

Thankyou for your reply
Unfortunatley this code inserts the sections from the srcDoc at the end of the dstDoc.
I understand that the document is a node structure and that all I have to do is insert my srcDoc Sections at the location where I inserted a Break using the DocumentBuilder.InsertBreak(BreakType); however how do I get a reference to the new break/node/section so that I can insert my srcDoc Sections at this new location inside the document rather than just stDoc.Sections.Add(section); which adds the sections to the end
Cheers Bruce

Good question. The solution is not very obvious, but possible due to several useful API functions:

// Open the apended document.
string filename = Application.StartupPath + @"\Doc2.doc";
Document srcDoc = new Document(filename);
// Open the destination document.
filename = Application.StartupPath + @"\Doc1.doc";
Document dstDoc = new Document(filename);
// Create document builder.
DocumentBuilder builder = new DocumentBuilder(dstDoc);
// Go to bookmark named 'b2'.
builder.MoveToBookmark("b2");
// Break to a new section at this point.
builder.InsertBreak(BreakType.SectionBreakContinuous);
// Find the index of the newly created section.
int idx = dstDoc.Sections.IndexOf(builder.CurrentParagraph.ParentSection);
// Insert all sections from appended document to the destination document.
foreach (Section section in srcDoc.Sections)
{

    // Import section from apended document retaining source formatting
    Section impSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
    dstDoc.Sections.Insert(idx++, impSection);
    // Set the inserted section to start immediately after 
    // the previous section end without page break.
    impSection.PageSetup.SectionStart = SectionStart.Continuous;
}
// Save the resulting document with a new name
dstDoc.Save(Application.StartupPath + @"\Doc1+2.doc");