Render to HTML
When rendering to HTML, GroupDocs.Viewer renders each page of the source document as a separate HTML document.
GroupDocs.Viewer for Java provides two options to manage CSS, fonts, images, and other resources:
- HTML with external resources stores page resources as separate files. This allows reusing common resources and results in reducing page size and loading speed.
- HTML with embedded resources integrates page resources into HTML. This makes each document page self-sufficient but results in increasing page size and loading speed.
To render files to HTML, follow these steps:
- Create an instance of the Viewer class. Specify the source document path as a constructor parameter.
- Instantiate the HtmlViewOptions object. Specify a path to save the rendered pages.
- Call the View.view() method of the Viewer object. Specify the HtmlViewOptions object as the parameter.
The following code snippet shows how to render a .docx document to HTML with external resources:
import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
// ...
try (Viewer viewer = new Viewer("sample.docx")) {
// Create an HTML file for each page.
// Specify the HTML file names and location of external resources.
// {0} and {1} are replaced with the current page number and resource name, respectively.
HtmlViewOptions viewOptions = HtmlViewOptions.forExternalResources("page_{0}.html", "page_{0}/resource_{0}_{1}", "page_{0}/resource_{0}_{1}");
viewer.view(viewOptions);
}
The following code snippet shows how to render a .docx document to HTML with embedded resources:
import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
// ...
try (Viewer viewer = new Viewer("sample.docx")) {
// Create an HTML files.
// {0} is replaced with the current page number in the file name.
HtmlViewOptions viewOptions = HtmlViewOptions.forEmbeddedResources("page_{0}.html");
viewer.view(viewOptions);
}
For details, please refer to the following pages: