Copy footer from one document to another document

Hi,

I want to ask, how to copy footer from one document to another document? How to implement it in coding?

Regards,
Ken Wan

Hi

Thanks for your request. If you need just copy header/footer from another document, you can use the following code:

// Get source HeaderFooter
HeaderFooter headerToCopy = (HeaderFooter)dstDoc.ImportNode(srcDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary], true, ImportFormatMode.UseDestinationStyles);
// Remove old HeaderFooter
if (dstDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary] != null)
    dstDoc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary].Remove();
// Insert another HeaderFooter
dstDoc.FirstSection.HeadersFooters.Add(headerToCopy);

Hope this helps.
Best regards.

Hi all,

We have tried the following code. But it can’t fix our problem. Let me tell you what problem we are facing first.

We try to duplicate a section in word, which section is a page in my word template. But we found that the duplicated word page doesn’t contain the footer. So what should we do? Below post our code to create a new section. And attached is the PDF we did.

s_id=s_id-count+addSectionCount;
for (int i = 0; i <xmlNode.ChildNodes.Count; i++)
{
    secDoc = new Document();
    Section newSection = null;
    secDoc.RemoveAllChildren();
    newSection = (Section) secDoc.ImportNode(doc.Sections[s_id], true);
    secDoc.Sections.Add(newSection);
    secDoc = executeGroupingMerge(xmlNode.ChildNodes[pageCount], xmlNode.ChildNodes[pageCount].Name, "");
    pageCount++;
    doc.Sections.Insert(s_id + pageCount, doc.Sections[0].Clone());
    doc.Sections[s_id + pageCount].ClearContent();
    doc.Sections[s_id + pageCount].AppendContent(secDoc.Sections[0]);
    addSectionCount++;
    secDoc = null;
    imgBase = new Hashtable();
}

And attached is the final report, the template and image to locate the problem. Please see whether there are any more information you needed.

Regards,
Ken Wan

Hi
Thank you for additional information. In your document, all section has Headers/Footer linked to previous section. So actually, only first section in your template has headers/footers and all the following section just inherits these headers/footers.
You can use DocumentExplorer (Aspose.Words demo application) toinspect structure of your document.
As a workaround, you can try using the following code:

private void CopyHeadersFooters(Document doc)
{
    // Loop through all sections
    foreach(Section sect in doc.Sections)
    {
        if (!sect.Equals(doc.FirstSection))
        {
            // Unlink headers/footers
            sect.HeadersFooters.LinkToPrevious(false);
            // Remove all headers footers
            sect.HeadersFooters.Clear();
            // Copy headers/footers from the first section
            foreach(HeaderFooter hf in doc.FirstSection.HeadersFooters)
            {
                sect.HeadersFooters.Add(hf.Clone(true));
            }
        }
    }
}

This code unlinks and removes headers/footers from all sections except the first one and then copy headers/footer from the first section to other section in the document.
Hope this helps.
Best regards.

Hi,

I have added this segment of code before mail merge the document, so that the program should be able to loop though the whole document and add the footer to each page right?

But I don’t know why it doesn’t work.

Regards,
Ken Wan

And added my source code for you to review

Hi

Thank you for additional information. Headers/Footers are not added to pages in the document. Headers/Footers are added to sections. There are three types of headers/footers – Primary, First Page and Even Page. In your document, you have DifferentFirstPageHeaderFooter option set to true, that is why footer is not displayed on the first page. The same behavior is inherited by other section in the document. So if some of section is spanned to three pages for example and you have DifferentFirstPageHeaderFooter set to true and only Primary footer defined – in the footer will not be displayed on the first page of this section.
So if you need to display footer on each page, try just disabling DifferentFirstPageHeaderFooter option.
Best regards.