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);
        }
        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);
        }
コード例 #3
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);
        }