Caching

In some cases document rendering may be a time-consuming operation (dependent on source document content, structure and complexity). For such situations caching can be a solution - rendered document representation is stored into cache (for example at the local drive) and in a case of repetitive rendering of the document, GroupDocs.Viewer uses cached representation. This thing helps to avoid the processing of the same document again and again.
To enable caching you have to:

Here is a code that demonstrates how to enable caching for GroupDocs.Viewer for Java.

    String outputDirectory = "C:\\output";
    String cachePath = Path.combine(outputDirectory, "cache");
    String pageFilePathFormat = Path.combine(outputDirectory, "page_{0}.html");
    
    FileCache cache = new FileCache(cachePath);
    ViewerSettings settings = new ViewerSettings(cache);
    
    Viewer viewer = new Viewer("C:\\sample.docx", settings);
    
    HtmlViewOptions options = HtmlViewOptions.forEmbeddedResources(pageFilePathFormat);
    
    long stopWatch = System.currentTimeMillis();
    viewer.view(options);
    stopWatch = System.currentTimeMillis() - stopWatch;
    System.out.println("Time taken on first call to View method " + stopWatch + " (ms).");
    
    stopWatch = System.currentTimeMillis();
    viewer.view(options);
    stopWatch = System.currentTimeMillis() - stopWatch;
    System.out.println("Time taken on second call to View method " + stopWatch + " (ms).");
    viewer.close();

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured Java library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.