Inserting Documents Dynamically not inserting the header and footer of the child document

Inserting Documents Dynamically not inserting the header and footer of the child document.
e.g., <<doc [test.docx] -build>>
When using the above doc tag in parent document it prints only the body content. The header and footer of the test document is ignored.
Let me know if this is expected behaviour and if there is a way to embed child document header and footer in parent doc template.

@vasanth18

When you use the <<doc [test.docx] -build>> tag in your parent document, it is expected behavior that only the body content of the child document is inserted, while the headers and footers are not included. This is a common limitation when dynamically inserting documents in Aspose.Words.

To include the headers and footers from the child document into the parent document, you will need to link the headers and footers manually. Here’s a general approach to achieve this:

  1. Load the Parent and Child Documents: First, load both the parent and child documents into Document objects.

  2. Link Headers and Footers: After loading the documents, you can link the headers and footers of the child document to the parent document. This can be done by setting the HeadersFooters collection of the sections in the parent document to reference those in the child document.

  3. Append the Child Document: Finally, append the child document to the parent document.

Here is a code example demonstrating how to link headers and footers:

// Load the parent and child documents
Document parentDoc = new Document("ParentDocument.docx");
Document childDoc = new Document("test.docx");

// Link headers and footers from the child document to the parent document
foreach (Section childSection in childDoc.Sections)
{
    Section newSection = (Section)parentDoc.ImportNode(childSection, true);
    parentDoc.AppendChild(newSection);

    // Link headers and footers
    newSection.HeadersFooters.LinkToPrevious();
}

// Save the modified parent document
parentDoc.Save("ModifiedParentDocument.docx");

In this example, the LinkToPrevious() method is used to link the headers and footers of the newly appended section to the previous section in the parent document, ensuring that the headers and footers from the child document are included.

For more detailed information on working with headers and footers in Aspose.Words, you can refer to the official documentation on linking headers and footers between documents.

If you have further questions or need additional assistance, feel free to ask!

Sources:
[1]: Link Headers Footers | Aspose.Words Document Processing API
[2]: Change or Replace Header and Footer|Aspose.Words for .NET

@vasanth18 When you use <<doc [test.docx] -build>> syntax it behaves the same way as DocumentBuilder.insertDocument method. When you use insertDocument method, section from the original document is not preserved, only content is inserted into the current section in the document. So in this case headers/footers are not preserved. To preserve headers/footers it would be more correct to use appendDocument method to preserve section from the source document including headers/footers.

1 Like

@alexey.noskov Got it thanks.

1 Like