Rendering to HTML

Document viewer can operate in different rendering modes, HTML, Image and PDF (see Features Overview for more information). This article will describe on how to view documents in HTML mode with HTML Viewer.

In HTML rendering mode all pages of the source documents are rendered as separate HTML pages. 

For HTML rendering mode following HtmlViewOptions are available:

  • HtmlViewOptions.forEmbeddedResources() - all resources such as styles, fonts, and graphics are integrated into an HTML pages.

    • Pros: No external files which makes more convenient to save result to a stream.
    • Cons: Larger page size and as a result slower loading and rendering of an HTML document in a browser.
  • HtmlViewOptions.forExternalResources() - all the resources, such as styles, fonts, and graphics are external.

    • Pros: Smaller page size as a page includes only markup and links to external resources. Faster HTML document loading and rendering in a browser as browsers can load multiple external resources simultaneously.
    • Cons: External files, since all resources will be stored next to an HTML page or in a specific directory.

With GroupDocs.Viewer for Java API HTML rendering became simple and intuitive. Just follow these steps:

  • Create a new instance of the Viewer class and pass the source document path as a constructor parameter.
  • Instantiate the HtmlViewOptions object according to your requirements (for embedded or external HTML resources) and specify saving path format for rendered document pages.
  • Call view() method of Viewer class instance and pass HtmlViewOptions to it.

Rendering to HTML with embedded resources

The following code shows how to render document to HTML with embedded resources.

try (Viewer viewer = new Viewer("sample.docx")) {
    HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources();
    viewer.view(viewOptions);
}

Rendering to HTML with external resources

The following code shows how to render document to HTML with external resources.

try (Viewer viewer = new Viewer("sample.docx")) {
    HtmlViewOptions viewOptions = HtmlViewOptions.forExternalResources();
    viewer.view(viewOptions);
}