Saving memoryStream as text leaves out first page header


#1

In a document, the option for “Different First Page” is checked for the header.
When running the following for that document:

MemoryStream txt = new MemoryStream();
AsposeManager.SaveAsText(txt);

string text = this.GetStringFromMemoryStream(txt);

The document’s first page header is not included into the string text, the saved string includes only the second page’s header.

Have I forgotten something? Any support that you can provide would be greatly appreciated. Thank you.


#2

@mtomlinson,

Thanks for your inquiry. 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 + "input.docx");

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);
    else
        strText += section.HeadersFooters[HeaderFooterType.HeaderPrimary].ToString(options);

    strText += section.Body.ToString(options);

    if (section.PageSetup.DifferentFirstPageHeaderFooter)
        strText += section.HeadersFooters[HeaderFooterType.FooterFirst].ToString(options);
    else
        strText += section.HeadersFooters[HeaderFooterType.FooterPrimary].ToString(options);
}

File.WriteAllText(MyDir + "output.txt", strText);

Best Regards,
Tahir Manzoor


#3

Tahir, Thank you for your timely response.

Please excuse my delay in getting back to you.
Is there an explanation as to why the SaveAsText() method does not included different first page headers?
Is that a feature that Aspose is considering? Adding both the headerFirst and headerPrimary to SaveAsText()?

Thank you.


#4

@mtomlinson,

Thanks for your inquiry. Could you please share the code of SaveAsText method here for our reference? We will then provide you more information about your query.


#5

Sorry, I meant when you call Save. More specifically:

public SaveOutputParameters Save(Stream stream, SaveFormat saveFormat);

in the form of

public void SaveAsText(MemoryStream ms) {
		AsposeDocument.Save(ms, SaveFormat.Text);
}

#6

@mtomlinson,

Thanks for sharing the detail. Please note that Aspose.Words exports only primary headers and footers at the beginning and end of each section when you save Word document to TXT file. This is the default behavior. As shared earlier, it is hard to meaningfully output headers and footers to plain text because it is not paginated. Please use the code example shared in my previous post to get the desired output.