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

Free Support Forum - aspose.com

TxtSaveOptions does not output text of header first page

Two header/footer bugs when save word documents to text (code below)

  1. When opening a word document containing a “first page header/footer” this is not saved when saving to text.
  2. The header/footer definition for the second page is exported, even when there is only one page.
    (ok the standard Ifilter from microsoft also has that bug)

I added the word document and resulting text file as att.first page test.zip (72.0 KB)

keep me posted when this will/is fixed

Used version: Aspose.words 18.5.0.0

code to reproduce problem:
string outputFile = Path.Combine(wordFileI.Directory.FullName, String.Format(“output-{0}.txt”, wordFileI.Name));
Document doc = new Document(wordFile);
TxtSaveOptions txtOps = new TxtSaveOptions();
txtOps.ExportHeadersFooters = true;
using (FileStream fs = File.Open(outputFile, FileMode.Create, FileAccess.ReadWrite))
{
doc.Save(fs, txtOps);
fs.Flush();
}

@kris_de_cree,

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

You can achieve your requirement by exporting content of header/footer and section’s body to text as shown below. Hope this helps you.

Document doc = new Document(MyDir + "first page header test.docx");
String text = "";
foreach (Section section in doc.Sections)
{
    if (section.PageSetup.DifferentFirstPageHeaderFooter)
    {
        if (section.HeadersFooters[HeaderFooterType.HeaderFirst] != null)
            text += section.HeadersFooters[HeaderFooterType.HeaderFirst].ToString(SaveFormat.Text);
        else
            text += section.HeadersFooters[HeaderFooterType.HeaderPrimary].ToString(SaveFormat.Text);

    }

    text += section.Body.ToString(SaveFormat.Text);

    if (section.PageSetup.DifferentFirstPageHeaderFooter)
    {
        if (section.HeadersFooters[HeaderFooterType.FooterFirst] != null)
            text += section.HeadersFooters[HeaderFooterType.FooterFirst].ToString(SaveFormat.Text);
        else
            text += section.HeadersFooters[HeaderFooterType.FooterPrimary].ToString(SaveFormat.Text);
    }
}

Console.WriteLine(text);

@tahir.manzoor
Dear Tahir,
Thanks for the quick and clear response. Thumbs up!
I’ll implement your solution into my code.

Only outputting header/footer at begin and end of each section is a very good solution.
Why is that not implemented in the SaveToText fct (Save with TxtSaveOptions.ExportHeadersFooters property is true)???

@kris_de_cree,

Thanks for your inquiry. It is hard to meaningfully output headers and footers to plain text because it is not paginated. You can use Node.ToString method to export the content of any node of Document into text.