HeaderFooter not append to a specific word file

All Files.zip (106.3 KB)

A header footer file is not appended with a word file whereas it is working file with other files, please find attached zip file containing “file with issue”, “file without issue” and “header footer file” as well.

@amit.tripathi

If you want to append the content of header and footer of “Header Footer File.docx” to another document, you can use the following code example.

Document srcDoc = new Document(MyDir + @"Header Footer File.docx");
Document dstDoc = new Document(MyDir + @"File With Issue.docx");

MergeHeaderFooter(srcDoc, dstDoc, HeaderFooterType.HeaderPrimary);
MergeHeaderFooter(srcDoc, dstDoc, HeaderFooterType.FooterPrimary);
dstDoc.Save(MyDir + @"19.11.docx");

public static void MergeHeaderFooter(Document srcDoc, Document dstDoc, HeaderFooterType headerType)
{
    foreach (Section section in dstDoc.Sections)
    {
        HeaderFooter header = section.HeadersFooters[headerType];
        if (header == null)
        {
            // There is no header of the specified type in the current section, create it.
            header = new HeaderFooter(section.Document, headerType);
            section.HeadersFooters.Add(header);
        }

        foreach (Node srcNode in srcDoc.FirstSection.HeadersFooters[headerType].ChildNodes)
        {
            Node dstNode = dstDoc.ImportNode(srcNode, true, ImportFormatMode.KeepSourceFormatting);
            header.AppendChild(dstNode);
        }
    }
}

If this does not help you, please share the document to which you want to append header and footer along with some more detail of your query. We will then share the code example with you according to your requirement.