Free Support Forum - aspose.com

Doc to html - Header/footer problem

Hi Aspose,

I convert a doc/docx file(a lot of pages) to html. I want to display header at the beginning and footer at the end of html file.
I used the following code:
LoadOptions loadOptions = new LoadOptions();
loadOptions.LoadFormat = LoadFormat.Doc;
Document doc = new Document(@“C:\Documents and Settings\Huy Phan\Desktop\HR_PA_hireemp.doc”, loadOptions);

HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions();
htmlSaveOptions.PrettyFormat = true;
htmlSaveOptions.SaveFormat = SaveFormat.Html;
htmlSaveOptions.AllowNegativeLeftIndent = true;
htmlSaveOptions.ImagesFolder = @“C:\Documents and Settings\Huy Phan\Desktop\aspose”;
htmlSaveOptions.ImagesFolderAlias = “aspose”;
htmlSaveOptions.ExportDocumentProperties = true;
htmlSaveOptions.ExportXhtmlTransitional = true;
htmlSaveOptions.ExportHeadersFootersMode = ExportHeadersFootersMode.FirstSectionHeaderLastSectionFooter;

// Save output
doc.Save(@“C:\Documents and Settings\Huy Phan\Desktop\aspose.html”, htmlSaveOptions);

Header & footer are generated, but footer is the one of the first page. Not last page in word doc. So, page number is displayed as “Page 1 / 69”. Can Aspose word has any option to get footer of last page.

Thanks and regards,

Hi Vuong,

Thanks for your inquiry.

The point here is, you will get only a single page HTML output (Long Page) no matter how many pages you have in your input Word document. Upon opening, Microsoft Word dynamically updates {PAGE} and {NUMPAGES} fields inside primary footer and finally this primary footer is rendered for each page.

In order to display Page N of N in the primary footer of document, please see the following code snippet:

Document srcDoc = new Document(@"C:\inputDoc.docx");

// Get the primary footer
HeaderFooterCollection headerFooters = srcDoc.FirstSection.HeadersFooters;
HeaderFooter footer = headerFooters[HeaderFooterType.FooterPrimary];

// Update any fields inside footer e.g. {PAGE}; this ensures page 69 of 69
footer.Range.UpdateFields();

// Call ConvertFieldsToStaticText to convert form fields to static text
FieldsHelper.ConvertFieldsToStaticText(footer, FieldType.FieldPage);

Moreover, to get description for ConvertFieldsToStaticText function, please see the following link:
http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-replace-fields-with-static-text.html

I hope, this will help

Best Regards,