Need help in maintaining Header Footer across all pages

Hello Team,

I have one main document with specific header and footer.

Now I am appending one more document to this document, which also has different header footer. But I want my final document to have all pages with same header footer that main document has.

Let me know how can I achieve this. Thanks in advance.

Thanks
Hardik

@HardikS To achieve this you should remove headers and footers from the source document. In this can headers and footers will be inherited from the previous section. For example see the following code:

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

// Remove header and fooers from the source document
// and set the same header/footer options as in the last dst document section.
foreach (Section s in src.Sections)
{
    s.HeadersFooters.Clear();
    s.PageSetup.DifferentFirstPageHeaderFooter = dst.LastSection.PageSetup.DifferentFirstPageHeaderFooter;
    s.PageSetup.OddAndEvenPagesHeaderFooter = dst.LastSection.PageSetup.OddAndEvenPagesHeaderFooter;
}

dst.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
dst.Save(@"C:\Temp\out.docx");

Thanks, it is working fine.

Thanks
Hardik

1 Like