How to insert a building block in a header

Hi,
is it possible to insert a building block in the header?

My next question is how can i insert a building block without a section break.

Thank you in advance

Hi Dominik,

Thanks for your inquiry. We suggest you please read following article about Aspose.Words’ DOM.
Aspose.Words Document Object Model

The BuildingBlock can contain only Section nodes. BuildingBlock can only be a child of GlossaryDocument.

You can create new building blocks and insert them into a glossary document. You can modify or delete existing building blocks. You can copy or move building blocks between documents. You can insert content of a building block into a document.

Please note that Aspose.Words mimics the same behavior as MS Word does. Please check the following code example. Hope this helps you.

Document doc = new Document();
doc.GlossaryDocument = new GlossaryDocument();
// Create new BuildingBlock
BuildingBlock bb = new BuildingBlock(doc.GlossaryDocument);
bb.Name = "Aspose.Words";
bb.Gallery = BuildingBlockGallery.AutoText;
bb.Category = "General";
bb.Description = "description";
bb.Behavior = BuildingBlockBehavior.Paragraph;
Node node = doc.GlossaryDocument.AppendChild(bb);
// import first section of in.docx into first BuildingBlock of empty document.
Document doc1 = new Document(MyDir + "in.docx");
NodeImporter imp = new NodeImporter(doc1, bb.Document, ImportFormatMode.KeepSourceFormatting);
Node impNode = imp.ImportNode(doc1.FirstSection, true);
bb.AppendChild(impNode);
doc.Save(MyDir + "Out.dotx");

It would be great if you please share following documents here for our reference. We will then provide you more information about your query.

  • Your input document.
  • Your expected output document.
  • Your document that contains the building blocks.

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,

thank you for your quick response.

I attached the input document containing the building blocks (saved as autotext) and the output document.

If you do save a building block there is an option to only insert the content. When i use activex to create a document based on a .dotx with building blocks (with option content only) there is no section inserted for each building block. Is that possible with aspose.words?

Thank you in advance

Hi Dominik,

Thanks for sharing the additional information. We are looking into it and will update you soon.

Hi Dominik,

Thanks for your patience. Please use following code example to achieve your requirements. Hope this helps you.

Document doc = new Document();
HeaderFooter header = new HeaderFooter(doc, HeaderFooterType.HeaderPrimary);
doc.FirstSection.HeadersFooters.Add(header);
// Source document
Document docBB = new Document(MyDir + "In.dotx");
GlossaryDocument glossaryDocument = docBB.GlossaryDocument;
BuildingBlock bb = glossaryDocument.GetBuildingBlock(BuildingBlockGallery.AutoText, "Allgemein", "Header");
NodeImporter importer = new NodeImporter(glossaryDocument, doc, ImportFormatMode.KeepSourceFormatting);
foreach (Node node in bb.GetChildNodes(NodeType.Any, true))
{
    if (node.NodeType == NodeType.Section)
    {
        foreach (Node childnode in ((Section)node).Body.ChildNodes)
        {
            Node impNode = importer.ImportNode(childnode, true);
            doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary].AppendChild(impNode);
        }
    }
}
doc.Save(MyDir + "17.5.docx");

Hi Tahir,

thank you again for your quik response. It worked fine.

Now i want to insert the building block “Body” in the document body. Can you help me again?

Many thanks for your help

Hi Dominik,

Thanks for your inquiry. Please use the same approach to insert the building block “Body” in the document body. Please check the following highlighted code snippet below. Hope this helps you.

Document doc = new Document();
// Source document
Document docBB = new Document(MyDir + "In.dotx");
GlossaryDocument glossaryDocument = docBB.GlossaryDocument;
BuildingBlock bb = glossaryDocument.GetBuildingBlock(BuildingBlockGallery.AutoText, "Allgemein", "Header");
NodeImporter importer = new NodeImporter(glossaryDocument, doc, ImportFormatMode.KeepSourceFormatting);
foreach (Node node in bb.GetChildNodes(NodeType.Any, true))
{
    if (node.NodeType == NodeType.Section)
    {
        foreach (Node childnode in ((Section)node).Body.ChildNodes)
        {
            Node impNode = importer.ImportNode(childnode, true);
            doc.FirstSection.Body.AppendChild(impNode);
        }
    }
}
doc.Save(MyDir + "17.6.docx");