コード例 #1
0
        public IEnumerable <ExtractedPackageDto> GetPackages(PostedDataDto postedData)
        {
            using (MetadataContext context = new MetadataContext(metadataConfiguration.GetAbsolutePath(postedData.guid), postedData.password))
            {
                var packages = new List <ExtractedPackageDto>();
                foreach (var package in context.GetPackages())
                {
                    List <PropertyDto>      properties  = new List <PropertyDto>();
                    List <KnownPropertyDto> descriptors = new List <KnownPropertyDto>();

                    foreach (var property in package.Properties)
                    {
                        properties.Add(new PropertyDto
                        {
                            name  = property.Name,
                            value = property.Value is Array ? ArrayUtil.AsString((Array)property.Value) : property.Value,
                            type  = (int)property.Type,
                        });
                    }

                    foreach (var descriptor in package.Descriptors)
                    {
                        var accessLevel = descriptor.AccessLevel;
                        if (arrayTypes.Contains(descriptor.Type))
                        {
                            accessLevel &= AccessLevels.Remove;
                        }

                        descriptors.Add(new KnownPropertyDto
                        {
                            name        = descriptor.Name,
                            type        = (int)descriptor.Type,
                            accessLevel = (int)accessLevel
                        });
                    }

                    packages.Add(new ExtractedPackageDto
                    {
                        id              = package.Id,
                        name            = package.Name,
                        index           = package.Index,
                        type            = (int)package.Type,
                        properties      = properties,
                        knownProperties = descriptors,
                    });
                }
                return(packages);
            }
        }
        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);
        }