Exemplo n.º 1
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);
        }