Aspose Word - Document builder - Keep Header footer from merged document

Hi,

I am trying to merge 4 (can be any number of) word documents via Document builder. I want to retain the headers and footers on the original documents as it is on the final merged document. But, the final document is having the header only on the first page, the rest of the pages/documents do not have any headers set. some of these documents are multi pages and have “first page header only” set on them. I wonder if that is causing the issue.

Please find below the code I am using (simplified):

DocumentBuilder builder = new DocumentBuilder(); // top level document builder which will merge all these forms
foreach (var template_stream in documentsWithDocType)
{
    Document mergedDocument = new Document(template_stream);
    mergedDocument.FirstSection.PageSetup.DifferentFirstPageHeaderFooter = false;
    builder.InsertDocument(mergedDocument, ImportFormatMode.KeepSourceFormatting, new ImportFormatOptions() { IgnoreHeaderFooter = false });
    builder.InsertBreak(BreakType.PageBreak);
}

builder.Document.Save(output_stream, SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Docx));

@HealthcareInteractive I would suggest you to use Document.AppenDocument method instead of DocumentBuuder.InsertDocument to merge documents together. Page setup as well as headers/footers in MS Word document are defined per section. If use Document.AppenDocument whole sections from the source documents are copied into the destination documents. For example see the following simplified code:

string[] docs = new string[] { @"C:\Temp\in1.docx", @"C:\Temp\in2.docx", @"C:\Temp\in3.docx", @"C:\Temp\in4.docx" };

Document result = null;
foreach (string path in docs)
{
    Document doc = new Document(path);
    // Configure the first section of the source document to start from a new page,
    // so each appended document starts from a new page.
    doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

    if (result == null)
        result = doc;
    else
        result.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}

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

Hi Alexey,

The code you provided worked and respective documents’ headers were showing in the final merged document. I had an issue where if the ‘header’ on one of the document was set as ‘first page header only’, then its next pages were taking the previous section’s header. I was able to resolve that by adding the below line to your code.

string[] docs = new string[] { @"C:\Temp\in1.docx", @"C:\Temp\in2.docx", @"C:\Temp\in3.docx", @"C:\Temp\in4.docx" };
 
Document result = null;
foreach (string path in docs)
{
    Document doc = new Document(path);
    // Configure the first section of the source document to start from a new page,
    // so each appended document starts from a new page.
    doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
    doc.FirstSection.HeadersFooters.LinkToPrevious(false);
    if (result == null)
        result = doc;
    else
        result.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
result.Save(@"C:\Temp\out.docx");

My other requirement was to remove their respective footers and then add page numbers on footer like 1/15, 2/25, etc. I was able to remove the footers using the below code, however I need your help on how to add these page numbers after this. Thank you so much for your help.

foreach (Section section in finalDoc.Sections)
{
    HeaderFooter footer; // need to remove all the footers possible.
    footer = section.HeadersFooters[HeaderFooterType.FooterPrimary];
    if (footer != null)
        footer.Remove();
    footer = section.HeadersFooters[HeaderFooterType.FooterFirst];
    if (footer != null)
        footer.Remove();
    footer = section.HeadersFooters[HeaderFooterType.FooterEven];
    if (footer != null)
        footer.Remove();
}

@HealthcareInteractive You can use code like the following to remove existing footers from your document and create new footers that shows "page of pages":

HeaderFooterType[] footerTypes = new HeaderFooterType[] { HeaderFooterType.FooterPrimary, HeaderFooterType.FooterFirst, HeaderFooterType.FooterEven };

// Remove footers.
result.GetChildNodes(NodeType.HeaderFooter, true).Cast<HeaderFooter>().Where(hf => footerTypes.Contains(hf.HeaderFooterType))
    .ToList().ForEach(hf => hf.Remove());

// Create footers in the resulting document using DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(result);
for(int i=0; i< result.Sections.Count; i++)
{
    builder.MoveToSection(i);
    foreach (HeaderFooterType headerFooterType in footerTypes)
    {
        builder.MoveToHeaderFooter(headerFooterType);
        builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        builder.InsertField("PAGE");
        builder.Write("/");
        builder.InsertField("NUMPAGES");
    }
}

Please see our documentation to learn how to work with headers/footers:
https://docs.aspose.com/words/net/working-with-headers-and-footers/

thanks Alexey.

the problem with this code if that it is restarting the “PAGE” number on every document merged.
I think because it is moving section by section and each document merged is sort of a new section??

@HealthcareInteractive This occurs because PageSetup.RestartPageNumbering flag is set in your source document. Please try reset this flag before appending the documents:

doc.FirstSection.PageSetup.RestartPageNumbering = false;

Thanks Alexey. That did the trick.

However, I notice one thing about it that might be confusing to the end user. As soon as I open the document, then the first page shows (1/3). There are 15 pages in the document and the word is somehow not able to catch it. As and when I keep scrolling down the document, then the page numbers are updating in footer and word task bar as well.

Is there a way to fix this behaviour?

Appreciate your time and help today. Thanks.

@HealthcareInteractive Usually, fields in document’s header/footer are updated automatically by MS Word. It looks like in your case MS Word hangs a bit and does not update the NUMPPAGES on document load. Please try calling Document.UpdateFields and Document.UpdatePgaeLayout methods before saving the final document.