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:

  • Instantiate desired cache object (for example FileCache will store document rendering results at the local drive)
  • Instantiate ViewerSettings object with cache object;
  • Pass ViewerSettings object to constructor of a Viewer class.
  • Call View method of Viewer class.

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

			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);

            using (Viewer viewer = new Viewer(@"C:\sample.docx", settings))
            {
                HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);

                Stopwatch stopWatch = Stopwatch.StartNew();
                viewer.View(options);
                stopWatch.Stop();
                Console.WriteLine("Time taken on first call to View method {0} (ms).", stopWatch.ElapsedMilliseconds);


                stopWatch.Restart();
                viewer.View(options);
                stopWatch.Stop();
                Console.WriteLine("Time taken on second call to View method {0} (ms).", stopWatch.ElapsedMilliseconds);
            }

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 .NET 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.