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

Free Support Forum - aspose.com

Missing Header and Footer for each section while converting DOCX to HTML using Aspose.Words

Hi,

I am facing issue with header and footer for each section while converting word document to html.
Here I used the below code:

Aspose.Words.License lic = new Aspose.Words.License();
lic.SetLicense("Aspose.Words.lic");
Document doc = new Document(HttpContext.Current.Server.MapPath(MyDir + filename));
string html = "";

HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.ExportImagesAsBase64 = true;
saveOptions.PrettyFormat = true;
saveOptions.ExportPageSetup = true;
saveOptions.ExportTocPageNumbers = true;
saveOptions.CssStyleSheetType = CssStyleSheetType.Embedded;

saveOptions.ExportHeadersFootersMode = Aspose.Words.Saving.ExportHeadersFootersMode.FirstSectionHeaderLastSectionFooter;

string dynamichtmlfile = "dynamicfile_" + Guid.NewGuid() + ".html";
doc.Save(HttpContext.Current.Server.MapPath(MyDir + dynamichtmlfile), saveOptions);

using (StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath(MyDir + dynamichtmlfile)))
    html = reader.ReadToEnd();

I am attaching the sample doc as well as html file.

Converted_Sample_doc_to_html.zip (15.9 KB)
Sample_doc.zip (48.4 KB)

Please help.

@parimalak,

What I have found is that the logo image at the top-left corner of HTML is getting partially truncated (see screenshot.png (18.4 KB)). But, you can workaround this issue by enabling the HtmlSaveOptions.ExportPageMargins option:

Document doc = new Document("C:\\Temp\\Sample_Doc\\Sample_doc.docx");

HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.ExportImagesAsBase64 = true;
saveOptions.PrettyFormat = true;
saveOptions.ExportPageSetup = true;
saveOptions.ExportTocPageNumbers = true;
saveOptions.CssStyleSheetType = CssStyleSheetType.Embedded;

saveOptions.ExportPageMargins = true;
saveOptions.ExportHeadersFootersMode = Aspose.Words.Saving.ExportHeadersFootersMode.FirstSectionHeaderLastSectionFooter;

doc.Save("C:\\Temp\\Sample_Doc\\21.5.html", saveOptions);

@awais.hafeez,
Thanks for your response, still I am facing the issue with header and footer.

I have one header and footer format for First page and another header and footer format for others pages in the word document.

After converting the word document to html, here I am getting header for First page and also getting footer for last page in the word document
and also not getting page number for each page in the word document but I am getting page number for last page in the word document as Page 1 of 4.

Here I am attaching the converted html file.
Convert_sample_doc_to_html.zip (15.9 KB)

Please help me to get this.

@parimalak,

Generally, there are three types of headers/footers in Word documents e.g. FooterFirst, FooterPrimary and FooterEven. Similarly, we have three types of headers such as HeaderFirst, HeaderPrimary and HeaderEven. Here are a few details of these header/footer types:

Member Name Description
HeaderEven Header for even numbered pages.
HeaderPrimary Primary header, also used for odd numbered pages.
FooterEven Footer for even numbered pages.
FooterPrimary Primary footer, also used for odd numbered pages.
HeaderFirst Header for the first page of the section.
FooterFirst Footer for the first page of the section.

So, if you want to preserve header/footer across all pages in Word document, then you can try saving to HtmlFixed format as follows:

Document doc = new Document("C:\\Temp\\Sample_Doc\\Sample_doc.docx");

HtmlFixedSaveOptions HtmlFixedSaveOptions = new HtmlFixedSaveOptions();
HtmlFixedSaveOptions.ExportEmbeddedImages = true;
HtmlFixedSaveOptions.PrettyFormat = true;
HtmlFixedSaveOptions.ExportEmbeddedCss = true;
// there are some other options too in HtmlFixedSaveOptions class

doc.Save("C:\\Temp\\Sample_Doc\\21.5.html", HtmlFixedSaveOptions);

However, it is hard to meaningfully output headers and footers to normal/plain HTML format because HTML is not paginated. When Export Headers Footers Mode property is PerSection, Aspose.Words exports only primary headers and footers at the beginning and the end of each section. When it is FirstSectionHeaderLastSectionFooter, primary header of the first section is exported at the beginning of the document and primary footer is at the end. You can disable export of headers and footers altogether by setting this property to None.