Import header using DocumentBuilder

Hi. We are trying to build a feature where we utilize DifferentFirstPageHeaderFooter to support different header for first page vs. the rest of the pages.

In this example (Working with Headers and Footers), I can see that it can be done by using DocumentBuilder and PageSetup. The example uses DocumentBuilder “write”, “writeLine”, InsertImage", etc. However, in our use case, we already have headers defined in separate openXml documents. So we would want to import/insert the headers from the “header” openXml to “template” openXml. It seems like “InsertDocument” or “InsertNode” might be the answer, but we haven’t been able to figure it out. Could you please help?

Thank you,
-Satoshi

@syamamoto If you you have headers/footers defined in another document and it is required to copy them into the target document, then you can simply import them and add to the target document. For example see the following code:

Document dst = new Document(@"C:\Temp\dst.docx");
Document src = new Document(@"C:\Temp\src.docx");

// Import primary header from the source document.
Node primaryHeader = dst.ImportNode(src.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary], true);

// check whether destination section already has primary header.
if (dst.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary] != null)
    dst.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary].Remove();
// Add imported header
dst.FirstSection.HeadersFooters.Add(primaryHeader);

dst.Save(@"C:\Temp\out.docx");

Thank you, Alexey. We were actually using the suggested Document.ImportNode approach (instead of the DocumentBuilder approach) before we started looking into how to support “different first page header”. After receiving your reply, I went back and tested some more, and I got it to work for “first header” and “primary header”.

What I didn’t realize previously is that if you want to import a “first page header” into a target, the header must be defined as “first page header” in the source document. In other words, when you author the source document, you must check the “Different First Page” checkbox in MS Word.

This may meet the requirement, but is there a way to import a primary header as a first page header, or vice versa? I tried importing a primary header as a first page header, and it did not work.

In other words, this works fine. (importing a first page header as a first page header)

templateDoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = true;

Node firstHeader = templateDoc.ImportNode(header1Doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst], true);
if (templateDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst] != null)
{
    templateDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst].Remove();
}
templateDoc.FirstSection.HeadersFooters.Add(firstHeader);

But the below doesn’t work…(importing a primary header as a first page header)

templateDoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = true;

Node firstHeader = templateDoc.ImportNode(header1Doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary], true);
if (templateDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst] != null)
{
    templateDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst].Remove();
}
templateDoc.FirstSection.HeadersFooters.Add(firstHeader);

@syamamoto In this case you can import only content of Header/Footer. For example see the following code:

Document dst = new Document(@"C:\Temp\dst.docx");
Document src = new Document(@"C:\Temp\src.docx");

// Get primary header from the source document.
HeaderFooter srcPrimaryHeader = src.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary];

// Create firsp page header in the destination document and copy content from the source header/footer.
HeaderFooter dstFirstPageHeader = new HeaderFooter(dst, HeaderFooterType.HeaderFirst);

// Copy content from the source header.
foreach (Node srcChild in srcPrimaryHeader.GetChildNodes(NodeType.Any, false))
    dstFirstPageHeader.AppendChild(dst.ImportNode(srcChild, true));

// check whether destination section already has first header.
if (dst.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst] != null)
    dst.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst].Remove();
// Add created header
dst.FirstSection.HeadersFooters.Add(dstFirstPageHeader);

dst.Save(@"C:\Temp\out.docx");

Thank you very much. It works great.

1 Like