Header and Footer is not present in text file when converted from doc to .txt format

Hi,
Iam unble to see header and footer in the text file when converted from word document(doc format).
I have attached word document(input) and text file(output) for your reference.
Can you please advise me on this.
Below is the sample code

Document asposeDocument = new Document(@"MyWordDocument.doc");
asposeDocument.Save("test.txt", SaveFormat.Text); 

Thanks,
Vinay

Hi Vinay,

Thanks for your inquiry. It is hard to meaningfully output headers and footers to plain text because it is not paginated. 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 TxtSaveOptions.ExportHeadersFooters property to false.

In your document, the header/footer are of type HeaderFirst and FooterFirst. Please use the following code example to copy the contents of HeaderFirst and FooterFirst into HeaderPrimary and FooterPrimary. In this case, you will get the header/footer in output text file. Hope this helps you. Please let us know if you have any more queries.

public static void CopyHeaderFooter(Section section, HeaderFooterType source, HeaderFooterType dest)
{
    NodeCollection hfNodes = section.HeadersFooters[source].ChildNodes;
    HeaderFooter headerfooter;
    if (section.HeadersFooters[dest] == null)
    {
        headerfooter = new HeaderFooter(section.Document, dest);
        section.HeadersFooters.Add(headerfooter);
    }
    section.HeadersFooters[dest].ChildNodes.Clear();
    foreach (Node node in hfNodes)
        section.HeadersFooters[dest].ChildNodes.Add(node.Clone(true));
}
Document doc = new Document(MyDir + "My_Doc.doc");
CopyHeaderFooter(doc.FirstSection, HeaderFooterType.HeaderFirst, HeaderFooterType.HeaderPrimary);
CopyHeaderFooter(doc.FirstSection, HeaderFooterType.FooterFirst, HeaderFooterType.FooterPrimary);
doc.Save(MyDir + "Out.txt", SaveFormat.Text);

Hi,

I have just tried with the code it works fine when there is header and footer in the document.

But it throws object reference is not set exception when there is no header and footer in the document. Even if the header and footer is hidden it throws same exception.

Thanks,
Vinay

Hi Vinay,

Thanks
for your inquiry. Please use the following code example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "My_Haigh.doc");
foreach (Section section in doc.Sections)
{
    if (section.HeadersFooters[HeaderFooterType.HeaderPrimary] == null && section.HeadersFooters[HeaderFooterType.HeaderFirst] != null)
        CopyHeaderFooter(doc.FirstSection, HeaderFooterType.HeaderFirst, HeaderFooterType.HeaderPrimary);
    if (section.HeadersFooters[HeaderFooterType.FooterPrimary] == null && section.HeadersFooters[HeaderFooterType.FooterFirst] != null)
        CopyHeaderFooter(doc.FirstSection, HeaderFooterType.FooterFirst, HeaderFooterType.FooterPrimary);
}
doc.Save(MyDir + "Out.txt", SaveFormat.Text);

Moreover, HeaderFooter class represents a container for the header or footer text of a section. HeaderFooter can contain Paragraph and Table child nodes.

HeaderFooter is a section-level node and can only be a child of Section. There can only be one HeaderFooter or each HeaderFooterType in a Section.

If Section does not have a HeaderFooter of a specific type or the HeaderFooter has no child nodes, this header/footer is considered linked to the header/footer of the same type of the previous section in Microsoft Word.

When HeaderFooter contains at least one Paragraph, it is no longer considered linked to previous in Microsoft Word.