How to migrate to GroupDocs.Viewer 19.8 or higher

This article contains information about migration from the legacy API to GroupDocs.Viewer 20.1 or higher.

Why To Migrate

Here are the key reasons to use the new updated API provided by GroupDocs.Viewer for Java since version 20.1:

  • Viewer class introduced as a single entry point to manage the document rendering process to any supported file format (instead of ViewerHtmlHander / ViewerImageHander classes from previous versions).
  • The overall rendering speed improved dramatically by saving rendered page as soon as it was rendered, not when all pages list were rendered.
  • Product architecture was redesigned from scratch in order to decreased memory usage (from 10% to 400% approx. depending on document type).
  • Document viewing options simplified so it’s easy to instantiate proper options class and control over document rendering and saving processes.

How To Migrate

Here is a brief comparison of how to display document into HTML form using old and new API.

Using legacy API

    //Get Configurations
    ViewerConfig config = Utilities.getConfigurations();

    // Create html handler
    ViewerHtmlHandler htmlHandler = new ViewerHtmlHandler(config);

    // Guid implies that unique document name 
    String guid = "sample.docx";

    // Document password
    String documentPassword = "password";

    //Instantiate the HtmlOptions object
    HtmlOptions options = new HtmlOptions();

    //to get html representations of pages with embedded resources
    options.setResourcesEmbedded(true);

    // Set password if document is password protected. 
    if (documentPassword != null && !documentPassword.isEmpty()) {
        options.setPassword(documentPassword);
    }

    //Get document pages in html form
    List<PageHtml> pages = htmlHandler.getPages(guid, options);

    for (PageHtml page : pages) {
        //Save each page at disk
        Utilities.saveAsHtml(page.getPageNumber() + "_" + documentName, page.getHtmlContent());
    }

Using new API

import com.groupdocs.viewer.Viewer;
import com.groupdocs.viewer.options.HtmlViewOptions;
import com.groupdocs.viewer.options.LoadOptions;
import com.groupdocs.viewer.options.ViewInfoOptions;
import com.groupdocs.viewer.results.Page;
import com.groupdocs.viewer.results.ViewInfo;
// ...

LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("password");

try (Viewer viewer = new Viewer("sample.docx", loadOptions)) {
    HtmlViewOptions options = HtmlViewOptions.forEmbeddedResources("page_{0}.html");
    viewer.view(options);

    final ViewInfo viewInfo = viewer.getViewInfo(ViewInfoOptions.forHtmlView());
    final List<Page> pages = viewInfo.getPages();

    for (Page page : pages) {
        final int pageNumber = page.getNumber();
        System.out.println("Page number: " + pageNumber + ", file saved as 'page_" + pageNumber + ".html'");
    }
}