Insert some buildingblock will lost feature

i want to insert a tableofcontents into document, the easy way is directly use DocumentBuilder.InsertTableOfContents api, but i want to some special feature just like word neature function.

toc inserted by word

this toc is a built-in buildingblock with the name “Automatic Table 1” which contains in file “C:\Users\xxx\AppData\Roaming\Microsoft\Document Building Blocks\1033\15\Built-In Building Blocks.dotx”,

so i want to use below code to make the same result:

Document docBuildingBlocks = new Document(@"C:\Users\xx\AppData\Roaming\Microsoft\Document Building Blocks\1033\15\Built-In Building Blocks.dotx");

ADoc.BuildingBlocks.GlossaryDocument glossaryDocument = docBuildingBlocks.GlossaryDocument;
foreach(ADoc.BuildingBlocks.BuildingBlock buildingBlock in glossaryDocument.BuildingBlocks)
{
    if (buildingBlock.Gallery.ToString().StartsWith("TableOfContents"))
    {
        if (buildingBlock.Name == "Automatic Table 1")
        {
            ADoc.Section sec = (ADoc.Section) buildingBlock.FirstChild;
            // builder.InsertNode(docx.ImportNode(sec, true));
            docx.InsertBefore(docx.ImportNode(sec, true), docx.FirstChild);
            break;
        }
    }
}

this code can be work, but the toc has some diff from which insert by word, insert by aspose
the diff is: the toc insert by word is hosting in a ContentControl, but this ContentControl has disappeared when use aspose api.

however if i use almost same code to insert a cover page will be ok. all ContentControl has reserved.
can you please check it?

and then, there is another unexpected exception, in here if use DocumentBuilder api, the insert operation will throw an exception.

please look below picture.

unexpected exception

maybe a bug. please check it.

Hi Pyntia,

Thanks for your inquiry.

*sendreams:
the diff is: the toc insert by word is hosting in a ContentControl, but this ContentControl has disappeared when use aspose api.

however if i use almost same code to insert a cover page will be ok. all ContentControl has reserved.
can you please check it?*

I have tested the scenario and have managed to reproduce the same issue at my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-9912. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

*sendreams:

and then, there is another unexpected exception, in here if use DocumentBuilder api, the insert operation will throw an exception.

please look below picture.

unexpected exception

maybe a bug. please check it.*

I have tested the scenario and have not found the shared issue. Could you please share your input document here for testing? I will investigate the issue on my side and provide you more information.

hi, Tahir

please see the attachment.

Hi Pyntia,

Thanks for sharing the document. I have worked again with your code and have noticed that you are incorrectly inserting the Section node. The Section node can not be inserted after calling MoveToDocumentStart method using DocumentBuilder.InsertNode. The DocumentBuilder.MoveToDocumentStart method move the cursor to the beginning of the document.

In your case, I suggest you please use the CompositeNode.InsertBefore method as shown below.

Document docBuildingBlocks = new Document(@"C:\Built-In Building Blocks.dotx");
Document docx = new Document(MyDir + "test.docx");
Aspose.Words.BuildingBlocks.GlossaryDocument glossaryDocument = docBuildingBlocks.GlossaryDocument;
foreach(Aspose.Words.BuildingBlocks.BuildingBlock buildingBlock in glossaryDocument.BuildingBlocks)
{
    if (buildingBlock.Gallery.ToString().StartsWith("CoverPage"))
    {
        if (buildingBlock.Name == "Whisp")
        {
            Section sec = (Section) buildingBlock.FirstChild;
            docx.InsertBefore(docx.ImportNode(sec, true), docx.FirstChild);
            break;
        }
    }
}

hi, Tahir

thank you very much. i know it