예제 #1
0
        public PageDescriptionEntity LoadDocumentPage(PostedDataEntity postedData)
        {
            PageDescriptionEntity loadedPage = new PageDescriptionEntity();

            try
            {
                // get/set parameters
                string documentGuid = postedData.guid;
                int    pageNumber   = postedData.page;
                string password     = (string.IsNullOrEmpty(postedData.password)) ? null : postedData.password;

                using (Comparer comparer = new Comparer(documentGuid, GetLoadOptions(password)))
                {
                    IDocumentInfo info = comparer.Source.GetDocumentInfo();

                    string encodedImage = GetPageData(pageNumber - 1, documentGuid, password);
                    loadedPage.SetData(encodedImage);

                    loadedPage.height = info.PagesInfo[pageNumber - 1].Height;
                    loadedPage.width  = info.PagesInfo[pageNumber - 1].Width;
                    loadedPage.number = pageNumber;
                }
            }
            catch (Exception ex)
            {
                throw new FileLoadException("Exception occurred while loading result page", ex);
            }

            return(loadedPage);
        }
        private static LoadDocumentEntity GetLoadDocumentEntity(bool loadAllPages, string documentGuid, string fileCacheSubFolder, ICustomViewer customViewer, bool printVersion)
        {
            if (loadAllPages)
            {
                customViewer.CreateCache();
            }

            dynamic            viewInfo           = customViewer.GetViewer().GetViewInfo(ViewInfoOptions.ForHtmlView());
            LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();

            if (!Directory.Exists(cachePath))
            {
                Directory.CreateDirectory(cachePath);
            }

            string pagesInfoPath;

            TryCreatePagesInfoXml(fileCacheSubFolder, viewInfo, out pagesInfoPath);

            foreach (Page page in viewInfo.Pages)
            {
                PageDescriptionEntity pageData = GetPageInfo(page, pagesInfoPath);
                if (loadAllPages)
                {
                    pageData.SetData(GetPageContent(page.Number, documentGuid, cachePath, printVersion));
                }

                loadDocumentEntity.SetPages(pageData);
            }

            loadDocumentEntity.SetGuid(documentGuid);
            return(loadDocumentEntity);
        }
예제 #3
0
        public DocumentPreviewDto LoadDocument(PostedDataDto postedData)
        {
            string password        = string.IsNullOrEmpty(postedData.password) ? null : postedData.password;
            var    documentPreview = new DocumentPreviewDto();

            // set password for protected document
            var loadOptions = new LoadOptions
            {
                Password = password
            };


            bool completed = ExecuteWithTimeLimit(TimeSpan.FromMilliseconds(metadataConfiguration.GetPreviewTimeLimit()), cancelationToken =>
            {
                using (Stream fileStream = fileService.GetSourceFileStream(postedData.guid))
                    using (GroupDocs.Metadata.Metadata metadata = new GroupDocs.Metadata.Metadata(fileStream, loadOptions))
                    {
                        cancelationToken.ThrowIfCancellationRequested();
                        IReadOnlyList <PageInfo> pages = metadata.GetDocumentInfo().Pages;

                        using (MemoryStream stream = new MemoryStream())
                        {
                            PreviewOptions previewOptions = new PreviewOptions(pageNumber => stream, (pageNumber, pageStream) => { });
                            previewOptions.PreviewFormat  = PreviewOptions.PreviewFormats.PNG;

                            int pageCount = pages.Count;
                            if (metadataConfiguration.GetPreloadPageCount() > 0)
                            {
                                pageCount = metadataConfiguration.GetPreloadPageCount();
                            }

                            for (int i = 0; i < pageCount; i++)
                            {
                                cancelationToken.ThrowIfCancellationRequested();
                                previewOptions.PageNumbers = new[] { i + 1 };
                                try
                                {
                                    metadata.GeneratePreview(previewOptions);
                                }
                                catch (NotSupportedException)
                                {
                                    break;
                                }

                                PageDescriptionEntity pageData = GetPageDescriptionEntities(pages[i]);
                                string encodedImage            = Convert.ToBase64String(stream.ToArray());
                                pageData.SetData(encodedImage);
                                documentPreview.SetPages(pageData);
                                stream.SetLength(0);
                            }
                        }
                    }
            });

            documentPreview.SetTimeLimitExceeded(!completed);
            documentPreview.SetGuid(postedData.guid);

            // return document description
            return(documentPreview);
        }
        public LoadDocumentEntity LoadDocument(PostedDataDto postedData)
        {
            // get/set parameters
            string             filePath           = metadataConfiguration.GetAbsolutePath(postedData.guid);
            string             password           = string.IsNullOrEmpty(postedData.password) ? null : postedData.password;
            LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();

            // set password for protected document
            var loadOptions = new LoadOptions
            {
                Password = password
            };

            using (GroupDocs.Metadata.Metadata metadata = new GroupDocs.Metadata.Metadata(filePath, loadOptions))
            {
                GroupDocs.Metadata.Common.IReadOnlyList <PageInfo> pages = metadata.GetDocumentInfo().Pages;

                using (MemoryStream stream = new MemoryStream())
                {
                    PreviewOptions previewOptions = new PreviewOptions(pageNumber => stream, (pageNumber, pageStream) => { });
                    previewOptions.PreviewFormat = PreviewOptions.PreviewFormats.PNG;

                    int pageCount = pages.Count;
                    if (metadataConfiguration.GetPreloadPageCount() > 0)
                    {
                        pageCount = metadataConfiguration.GetPreloadPageCount();
                    }
                    for (int i = 0; i < pageCount; i++)
                    {
                        previewOptions.PageNumbers = new[] { i + 1 };
                        try
                        {
                            metadata.GeneratePreview(previewOptions);
                        }
                        catch (NotSupportedException)
                        {
                            continue;
                        }

                        PageDescriptionEntity pageData = GetPageDescriptionEntities(pages[i]);
                        string encodedImage            = Convert.ToBase64String(stream.ToArray());
                        pageData.SetData(encodedImage);
                        loadDocumentEntity.SetPages(pageData);
                        stream.SetLength(0);
                    }
                }
            }

            loadDocumentEntity.SetGuid(postedData.guid);

            // return document description
            return(loadDocumentEntity);
        }
예제 #5
0
        public static LoadDocumentEntity LoadDocumentPages(string documentGuid, string password, bool loadAllPages)
        {
            LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();

            using (Comparer comparer = new Comparer(documentGuid, GetLoadOptions(password)))
            {
                Dictionary <int, string> pagesContent = new Dictionary <int, string>();
                IDocumentInfo            documentInfo = comparer.Source.GetDocumentInfo();

                if (documentInfo.PagesInfo == null)
                {
                    throw new GroupDocs.Comparison.Common.Exceptions.ComparisonException("File is corrupted.");
                }

                if (loadAllPages)
                {
                    for (int i = 0; i < documentInfo.PageCount; i++)
                    {
                        string encodedImage = GetPageData(i, documentGuid, password);

                        pagesContent.Add(i, encodedImage);
                    }
                }

                for (int i = 0; i < documentInfo.PageCount; i++)
                {
                    PageDescriptionEntity pageData = new PageDescriptionEntity
                    {
                        height = documentInfo.PagesInfo[i].Height,
                        width  = documentInfo.PagesInfo[i].Width,
                        number = i + 1
                    };

                    if (pagesContent.Count > 0)
                    {
                        pageData.SetData(pagesContent[i]);
                    }

                    loadDocumentEntity.SetPages(pageData);
                }

                return(loadDocumentEntity);
            }
        }
예제 #6
0
        private LoadDocumentEntity LoadDocument(string guid, string password)
        {
            try
            {
                dynamic options = null;
                //GroupDocs.Editor cannot detect text-based Cells documents formats (like CSV) automatically
                if (guid.EndsWith("csv", StringComparison.OrdinalIgnoreCase))
                {
                    options = new SpreadsheetToHtmlOptions();
                }
                else
                {
                    options = EditorHandler.DetectOptionsFromExtension(guid);
                }

                if (options is SpreadsheetToHtmlOptions)
                {
                    options.TextOptions = options.TextLoadOptions(",");
                }
                else
                {
                    options.Password = password;
                }
                string bodyContent;

                using (System.IO.FileStream inputDoc = System.IO.File.OpenRead(guid))

                    using (InputHtmlDocument htmlDoc = EditorHandler.ToHtml(inputDoc, options))
                    {
                        bodyContent = htmlDoc.GetEmbeddedHtml();
                    }
                LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();
                loadDocumentEntity.SetGuid(System.IO.Path.GetFileName(guid));
                PageDescriptionEntity page = new PageDescriptionEntity();
                page.SetData(bodyContent);
                loadDocumentEntity.SetPages(page);
                return(loadDocumentEntity);
            }
            catch
            {
                throw;
            }
        }
        private LoadDocumentEntity LoadDocument(string guid, string password)
        {
            LoadDocumentEntity loadDocumentEntity = new LoadDocumentEntity();

            loadDocumentEntity.SetGuid(guid);
            ILoadOptions loadOptions = GetLoadOptions(guid);

            if (loadOptions != null)
            {
                loadOptions.Password = password;
            }

            // Instantiate Editor object by loading the input file
            using (GroupDocs.Editor.Editor editor = new GroupDocs.Editor.Editor(guid, delegate { return(loadOptions); }))
            {
                IDocumentInfo documentInfo = editor.GetDocumentInfo(password);

                dynamic editOptions = GetEditOptions(guid);
                if (editOptions is WordProcessingEditOptions)
                {
                    editOptions.EnablePagination = true;

                    // Open input document for edit — obtain an intermediate document, that can be edited
                    EditableDocument      beforeEdit = editor.Edit(editOptions);
                    string                allEmbeddedInsideString = beforeEdit.GetEmbeddedHtml();
                    PageDescriptionEntity page = new PageDescriptionEntity();
                    page.SetData(allEmbeddedInsideString);
                    loadDocumentEntity.SetPages(page);
                    beforeEdit.Dispose();
                }
                else if (editOptions is SpreadsheetEditOptions)
                {
                    for (var i = 0; i < documentInfo.PageCount; i++)
                    {
                        // Let's create an intermediate EditableDocument from the i tab
                        SpreadsheetEditOptions sheetEditOptions = new SpreadsheetEditOptions();
                        sheetEditOptions.WorksheetIndex = i; // index is 0-based
                        EditableDocument tabBeforeEdit = editor.Edit(sheetEditOptions);

                        // Get document as a single base64-encoded string, where all resources (images, fonts, etc)
                        // are embedded inside this string along with main textual content
                        string allEmbeddedInsideString = tabBeforeEdit.GetEmbeddedHtml();
                        PageDescriptionEntity page     = new PageDescriptionEntity();
                        page.SetData(allEmbeddedInsideString);
                        page.number = i + 1;
                        loadDocumentEntity.SetPages(page);
                        tabBeforeEdit.Dispose();
                    }
                }
                else if (editOptions is PresentationEditOptions)
                {
                    for (var i = 0; i < documentInfo.PageCount; i++)
                    {
                        // Create editing options
                        PresentationEditOptions presentationEditOptions = new PresentationEditOptions();
                        // Specify slide index from original document.
                        editOptions.SlideNumber = i; // Because index is 0-based, it is 1st slide
                        EditableDocument slideBeforeEdit = editor.Edit(presentationEditOptions);

                        // Get document as a single base64-encoded string, where all resources (images, fonts, etc)
                        // are embedded inside this string along with main textual content
                        string allEmbeddedInsideString = slideBeforeEdit.GetEmbeddedHtml();
                        PageDescriptionEntity page     = new PageDescriptionEntity();
                        page.SetData(allEmbeddedInsideString);
                        page.number = i + 1;
                        loadDocumentEntity.SetPages(page);
                        slideBeforeEdit.Dispose();
                    }
                }
            }

            return(loadDocumentEntity);
        }