Esempio n. 1
0
        private static LEADDocument AddDocumentToCache(LEADDocument largestDocument, IOcrEngine ocrEngine, Uri documentUri, LoadDocumentOptions loadOptions, DocumentCacheOptions cacheOptions)
        {
            // Adds the document to the cache. Note that a new cache entry is created for each different maximumImagePixelSize.

            var document = DocumentFactory.LoadFromUri(documentUri, loadOptions);

            try
            {
                if (document == null)
                {
                    throw new InvalidOperationException("Failed to load URI: " + documentUri);
                }

                // We will modify this document...
                bool wasReadOnly = document.IsReadOnly;
                document.IsReadOnly = false;

                if (document.Text.TextExtractionMode != DocumentTextExtractionMode.OcrOnly && !document.Images.IsSvgSupported && ocrEngine != null)
                {
                    document.Text.OcrEngine = ocrEngine;
                }

                // Set in the cache options that we want
                document.CacheOptions = cacheOptions;

                // prepare the document, caching as much as possible.
                if (document.IsStructureSupported && !document.Structure.IsParsed)
                {
                    document.Structure.Parse();
                }

                // Need to cache the SVG with and without getting the back image
                var loadSvgOptions = new CodecsLoadSvgOptions();

                foreach (var page in document.Pages)
                {
                    // If we have a previous largest document, use the same
                    // SVG and text instead of recreating them (they do not change based on image size)
                    DocumentPage largestDocumentPage = null;

                    if (largestDocument != null)
                    {
                        largestDocumentPage = largestDocument.Pages[page.PageNumber - 1];
                    }

                    if (cacheOptions == DocumentCacheOptions.None)
                    {
                        // We are done, do not cache the images, svg or text
                        continue;
                    }

                    if ((cacheOptions & DocumentCacheOptions.PageSvg) == DocumentCacheOptions.PageSvg)
                    {
                        // SVG, this does not depend on the image size
                        using (var svg = page.GetSvg(null))
                        {
                        }

                        using (var svg = page.GetSvg(loadSvgOptions))
                        {
                        }
                    }

                    if ((cacheOptions & DocumentCacheOptions.PageSvgBackImage) == DocumentCacheOptions.PageSvgBackImage)
                    {
                        // SVG back image, this is different based on the image size
                        using (var svgBack = page.GetSvgBackImage(RasterColor.FromKnownColor(RasterKnownColor.White)))
                        {
                        }
                    }

                    if ((cacheOptions & DocumentCacheOptions.PageImage) == DocumentCacheOptions.PageImage)
                    {
                        // Image, this is different based on the image size
                        using (var image = page.GetImage())
                        {
                        }
                    }

                    if ((cacheOptions & DocumentCacheOptions.PageThumbnailImage) == DocumentCacheOptions.PageThumbnailImage)
                    {
                        // Thumbnail, this does not depend on the image size but there is no set thumbnail method
                        using (var thumbnailImage = page.GetThumbnailImage())
                        {
                        }
                    }

                    if ((cacheOptions & DocumentCacheOptions.PageText) == DocumentCacheOptions.PageText)
                    {
                        // Text, this does not depend on the image size
                        if (largestDocumentPage == null)
                        {
                            page.GetText();
                        }
                        else
                        {
                            var pageText = largestDocumentPage.GetText();
                            page.SetText(pageText);
                        }
                    }
                }

                document.AutoDeleteFromCache  = false;
                document.AutoDisposeDocuments = true;
                document.AutoSaveToCache      = false;
                // Stop caching
                document.CacheOptions = DocumentCacheOptions.None;
                document.IsReadOnly   = wasReadOnly;

                // save it to the regular cache
                document.SaveToCache();

                return(document);
            }
            catch (Exception)
            {
                if (document != null)
                {
                    document.Dispose();
                }
                throw;
            }
        }