Convert a Document to HTML, MHTML or EPUB

Documents in HTML and MHTMLflow-layout formats are also very popular and can be used on any web platform. For that reason, converting documents to HTML and MHTML is an important feature of Aspose.Words.

EPUB (short for “Electronic Publication”) is an HTML-based format commonly used for electronic book distribution. This format is fully supported in Aspose.Words for exporting electronic books that are compatible with most reading devices.

Convert a Document

For simple conversion to HTML, MHTML, or EPUB,one of theSavemethod overloads is used. You can save the document to a file or stream and explicitly set the output documentsave format or define it from the file name extension.

The following example shows how to convert DOCX to HTML with specifying a save format:

To convert a document to MHTML or EPUB, use SaveFormat.MHTML or SaveFormat.EPUB respectively.

Convert a Document with Round-trip Information

The HTML format does not support many Microsoft Word features, and if we need to restore a document model as close to the original as possible, we need to save some extra information within the HTML file. Such information is also called “round-trip information”. For this purpose, Aspose.Wordsprovides an ability to export round-trip information when saving to HTML, MHTML, or EPUB using theExportRoundtripInformationproperty. Saving the round-trip information allows restoring document properties such as tabs, comments, headers, and footers during the loading documents of the listed formats back into aDocumentobject.

The default value istruefor HTML andfalsefor MHTML and EPUB:

  • Whentrue, the round-trip information is exported as - aw - * CSS properties of the corresponding HTMLelements
  • Whenfalse, there is no round-trip information to be output into produced files

The following code example shows how to export round-trip information when converting a document from DOCX into HTML:

Specify Save Options when Conversion to HTML

Aspose.Words allows to convert a Word document to HTML using default or custom save options.Few examples of custom save options are described below.

Specify a Folder for Saving Resources

Using Aspose.Words we can specify a physical folder where all resources, such as images, fonts, and external CSS, are saved when a document is converted to HTML. By default, this is an empty string.

Specifying theResourceFolderproperty is the simplest way to set the folder where all resources should be written. We can use individual properties, such asFontsFolderwhich saves fonts to the specified folder andImagesFolderwhich saves images to a specified folder.When a relative path is specified,FontsFolderandImagesFolderrefer to the folder where the code assembly is located,ResourceFolderandCssStyleSheetFileNamerefer to the output folder where the HTML document is located.

In this example,ResourceFolderspecifies the relative path. This path refers to the output folder where the HTML document is saved.The value of theResourceFolderAliasproperty is used to create URLs for all resources.

The following code example shows how to work with these properties:

Using theResourceFolderAliasproperty, we can also specify the name of the folder used to construct URIs of all resources written into an HTML document. This is the simplest way to specify how URIs should be generated for all resource files. The same information can be specified for images and fonts separately viaImagesFolderAliasandFontsFolderAliasproperties, respectively.

However, there is no individual property for CSS. The behavior of theFontsFolder,FontsFolderAlias,ImagesFolder,ImagesFolderAliasandCssStyleSheetFileNameproperties are not changed. Note that theCssStyleSheetFileNameproperty is used both for specifying folder name and file name.

  • ResourceFolder has lower priority than folders specified via FontsFolder, ImagesFolder, and CssStyleSheetFileName. If the folder specified in the ResourceFolder does not exist, it will be created automatically.
  • ResourceFolderAlias has a lower priority than FontsFolderAlias and ImagesFolderAlias. If ResourceFolderAlias is empty, the value of the ResourceFolder property will be used to create resource URIs. If ResourceFolderAlias is set to “.” (dot), resource URIs will only contain file names without specifying a path.

Export Base64 Encoding FontsResources

Aspose.Words provides an ability tospecify whether font resources should be embedded into HTML in Base64 encodings. To perform this, use theExportFontsAsBase64property– this is an extension of theExportFontResourcesproperty. By default,its value isfalse,and fonts are written into separate files.But ifthis option is set totrue, fonts will be embedded into the document’s CSS in Base64 encoding. TheExportFontsAsBase64property only affects HTML format and does not affect EPUB and MHTML.

The following code example shows how to export Base64-encoded fonts to HTML:

Specify Save Options when Conversion to EPUB

Aspose.Words allows converting a Word document into EPUB format using default or custom save options.You can specify a number of options by passing an instance ofHtmlSaveOptionsto theSavemethod.

The following code example shows how to converts a Word document to EPUB with specifying some custom save options:

See Also


FAQ

  1. Q: How can I convert a DOCX file to HTML using Aspose.Words for Java?
    A: Load the document with Document document = new Document("input.docx"); and call document.save("output.html", SaveFormat.HTML);. You can also pass an HtmlSaveOptions instance to customize the output.

  2. Q: How do I embed fonts directly into the generated HTML file?
    A: Set HtmlSaveOptions options = new HtmlSaveOptions(); options.setExportFontsAsBase64(true); before saving. This embeds the font files as Base64 strings inside the CSS, eliminating external font files.

  3. Q: Where can I control the folder where images and other resources are saved during HTML conversion?
    A: Use HtmlSaveOptions properties such as setResourceFolder("resources"), setImagesFolder("resources/images"), and setFontsFolder("resources/fonts"). These properties define the physical locations for the exported resources.

  4. Q: How can I preserve Word features like comments, headers, and footers when converting to HTML?
    A: Enable round‑trip information by setting options.setExportRoundtripInformation(true);. This adds special aw-* CSS attributes that allow the document to be re‑loaded with those features intact.

  5. Q: What is the simplest way to convert a document to EPUB format?
    A: Call document.save("output.epub", SaveFormat.EPUB);. For advanced control, pass an HtmlSaveOptions object to the save method and specify EPUB‑specific options.