We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Header/Footer when Combining Documents

I’m using Aspose.Words to combine multiple word files together. When I combine the documents everything works great except there are some documents that need to go into the center that has content that takes up the entire page and when the documents are combined it forces a blank header to show up which causes content to flow onto the next page.


The code I’m using:

Document dstDoc = null;

foreach (string file in wordFiles)
{

if (dstDoc == null)
{
dstDoc = new Document(file);
continue;
}

Document srcDoc = new Document(file);

srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
srcDoc.FirstSection.HeadersFooters.LinkToPrevious(false);

foreach (Section srcSection in srcDoc)
{
Section dstSection = (Section)dstDoc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
dstDoc.AppendChild(dstSection);
}
}


Is there a way to get around this or is this a limitation of Word?

Hi

Thanks for your request. Could you please attach your input and output documents here for testing. I will check the problem on my side and provide you more information.

Best regards,

I’ve attached the documents

Thanks

Hi

Thank you for additional information. These empty Headers/Footers are created when you call LinkToPreviouse method:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.headerfootercollection.linktoprevious_overload_1.html

This method creates headers/footers automatically, If any of the headers or footers do not exist. This is needed to prevent inheriting from the previous section. On the other hand, if there are no headers/footers in section, it will inherit headers/footers from the previous section. So removing headers/footers is not an option.

As a workaround, you can try setting font of the paragraph in the newly created headers/footers to very small values, this must decrease influence to the document’s layout:

string[] wordFiles = {@"Test001\Document1.docx", @"Test001\Document2.docx", @"Test001\Document3.docx"};

Document dstDoc = null;

foreach (string file in wordFiles)

{

if (dstDoc == null)

{

dstDoc = new Document(file);

continue;

}

Document srcDoc = new Document(file);

srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;

srcDoc.FirstSection.HeadersFooters.LinkToPrevious(false);

foreach (HeaderFooter headersFooter in srcDoc.FirstSection.HeadersFooters)

{

if(string.IsNullOrEmpty(headersFooter.GetText().Trim()))

headersFooter.FirstParagraph.ParagraphBreakFont.Size = 1;

}

dstDoc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);

}

dstDoc.Save(@"Test001\out.docx");

Hope this could help you.

Best regards,

Your solution did not seem to make any difference on the space that the Header/Footers took. I came up with another solution that prevents the header/footer from showing up on the documents that don’t have any headers/footers. I just run the source document through this code before I append the document.

private static void ProcessDocument(Document doc)
{
    doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
    bool containsHeaderFooter = false;

    foreach (Section sec in doc)
    {
        if (sec.HeadersFooters.Count > 0)
        {
            containsHeaderFooter = true;
            break;
        }
    }

    if (containsHeaderFooter == false)
    {
        foreach (Section sec in doc)
        {
            sec.PageSetup.DifferentFirstPageHeaderFooter = true;
        }
    }
}

Hi

It is perfect that you managed to implement what you need. Please feel free to ask in case of any issues, we will be glad to help you.

Best regards,