Pre-caching documents

Normally the source document is converted to a special web-friendly format and cached after the document viewer is displayed in the page and when the document is viewed for the first time. With DocumentCache.PreCacheDocument method, you can do the conversion and caching beforehand so this way, when a user opens the document for the first time, there will be no waiting and the document will be loaded immediately from the cache. If the source document is already converted and cached, then this method will return without doing anything:

  • Accessing document cache in the same web application which uses DocumentViewer:

    C#
    //This DocumentCache instance is already filled with necessary information like cache location, license key etc.
    //So it will use the same settings as the DocumentViewer.
    var documentCache = DocumentUltimateWebConfiguration.Current.GetCache();

  • Accessing document cache in other applications:

    C#
    /*
    Make sure you set your license key in external application before calling PreCacheDocument method.
    Or you can create a DocumentUltimateConfiguration instance with your license key and pass that
    instance to the DocumentCache constructor.
    */
    DocumentUltimateConfiguration.Current.LicenseKey = "QQJDJLJP34...";
    
    /*
    Make sure you pass parameters (location, encryptionEnabled, keepVariations) to DocumentCache constructor
    which are the same values that are set in DocumentUltimateWebConfiguration.Current in your web application 
    that hosts the DocumentViewer. Usually you would only need to pass location parameter unless you 
    change default values of other parameters.
    
    Note that DocumentUltimateWebConfiguration.Current.CacheLocation accepts also virtual paths (e.g. "~/App_Data/DocumentCache")
    but in a non-web application e.g. in a Console application, when passing location parameter to DocumentCache constructor, 
    use the physical path (resolved path) because virtual paths can not be resolved in a non-web application.
    */
    var documentCache = new DocumentCache(@"C:\SomeFolder\DocumentCache");

    See Examples section on DocumentUltimateWebConfigurationCacheLocation property for possible Location values.

  • Pre-caching:

    C#
    /*
    Pass the document (FileProvider) to PreCacheDocument method which is the same that will passed to DocumentViewer.
    CacheSourceKey is generated according to combination of file extension, file modified date and file size info.
    
    Note that DocumentViewer.Document accepts also virtual paths (e.g. "~/SomeFolder/Document.docx")
    but in a non-web application e.g. in a Console application, when passing document parameter to PreCacheDocument method,
    use the physical path (resolved path) because virtual paths can not be resolved in a non-web application.
    */
    documentCache.PreCacheDocument(@"c:\SomeFolder\Document.docx");
    
    
    //Pass the same document options that will be set in DocumentViewer.DocumentOptions.
    //This is because some options can change CachePdfKey and/or CacheXpzKey.
    documentCache.PreCacheDocument(
        @"c:\SomeFolder\Document.docx",
        new DocumentOptions
        {
            Watermarks = new Collection<Watermark>
            {
                new TextWatermark
                {
                    Text = "Sample watermark"
                }
            }
        }
    );

    See Examples section on DocumentViewerDocument property for possible FileProvider values.

    See Examples section on DocumentUltimateWebConfigurationCacheLocation property for possible Location values.