Aspose word- Merge header and footer within the document


Could you please help me to merge the header and footer content into the main content and delete the header and footer. This will help me to preserve the format and print the same without any issue.


Hi Suresh,

Thanks for your inquiry. Could you please share your input and expected output documents here for our reference? We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Hi Tahir,

Thanks for your prompt reply. As requested, I have attached the rtf file and the output I created using aspose word.
I would prefer to have the exported text without losing the format and with the appropriate header and footer. I have used “Different First Page” header footer but the first page header footer is not reflecting in the exported text file. Please provide a solution as soon as possible.


Hi Suresh,

Thanks for sharing the detail. The TxtSaveOptions.ExportHeadersFooters property specifies whether to output headers and footers when exporting in plain text format. Default value is true.

It is hard to meaningfully output headers and footers to plain text because it is not paginated. When this property is true, Aspose.Words exports only primary headers and footers at the beginning and end of each section. You can disable export of headers and footers altogether by setting this property to false.

In your case, we suggest you please export the document to text using following code example. Hope this helps you.

Document doc = new Document(MyDir + "HeaderFooterTest.rtf");
TxtSaveOptions options = new TxtSaveOptions();
options.ExportHeadersFooters = false;
String strText = "";
foreach (Section section in doc.Sections)
    if (section.PageSetup.DifferentFirstPageHeaderFooter)
        strText += section.HeadersFooters[HeaderFooterType.HeaderFirst].ToString(options);
        strText += section.HeadersFooters[HeaderFooterType.HeaderPrimary].ToString(options);
    strText += section.Body.ToString(options);
    if (section.PageSetup.DifferentFirstPageHeaderFooter)
        strText += section.HeadersFooters[HeaderFooterType.FooterFirst].ToString(options);
        strText += section.HeadersFooters[HeaderFooterType.FooterPrimary].ToString(options);
File.WriteAllText(MyDir + "output.txt", strText);