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

Free Support Forum - aspose.com

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);


<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0in; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>

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);


<!–[if gte mso 9]>
<w:WordDocument>
<w:View>Normal</w:View>
<w:Zoom>0</w:Zoom>
<w:TrackMoves/>
<w:TrackFormatting/>
<w:PunctuationKerning/>
<w:ValidateAgainstSchemas/>
<w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>
<w:IgnoreMixedContent>false</w:IgnoreMixedContent>
<w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>
<w:DoNotPromoteQF/>
<w:LidThemeOther>EN-US</w:LidThemeOther>
<w:LidThemeAsian>X-NONE</w:LidThemeAsian>
<w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript>
<w:Compatibility>
<w:BreakWrappedTables/>
<w:SnapToGridInCell/>
<w:WrapTextWithPunct/>
<w:UseAsianBreakRules/>
<w:DontGrowAutofit/>
<w:SplitPgBreakAndParaMark/>
<w:EnableOpenTypeKerning/>
<w:DontFlipMirrorIndents/>
<w:OverrideTableStyleHps/>
</w:Compatibility>
<m:mathPr>
<m:mathFont m:val=“Cambria Math”/>
<m:brkBin m:val=“before”/>
<m:brkBinSub m:val="–"/>
<m:smallFrac m:val=“off”/>
<m:dispDef/>
<m:lMargin m:val=“0”/>
<m:rMargin m:val=“0”/>
<m:defJc m:val=“centerGroup”/>
<m:wrapIndent m:val=“1440”/>
<m:intLim m:val=“subSup”/>
<m:naryLim m:val=“undOvr”/>
</m:mathPr></w:WordDocument>
<![endif]–><!–[if gte mso 10]>

/* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin-top:0in; mso-para-margin-right:0in; mso-para-margin-bottom:8.0pt; mso-para-margin-left:0in; line-height:107%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin;}

<![endif]–>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.