Beispiel #1
0
        private static List <PageDataDescriptionEntity> GetAnnotatedPagesForPrint(string password, string documentGuid)
        {
            AnnotatedDocumentEntity description = new AnnotatedDocumentEntity();

            try
            {
                using (FileStream outputStream = File.OpenRead(documentGuid))
                {
                    using (GroupDocs.Annotation.Annotator annotator = new GroupDocs.Annotation.Annotator(outputStream, GetLoadOptions(password)))
                    {
                        IDocumentInfo info         = annotator.Document.GetDocumentInfo();
                        List <string> pagesContent = GetAllPagesContent(annotator, info);

                        for (int i = 0; i < info.PagesInfo.Count; i++)
                        {
                            PageDataDescriptionEntity page = new PageDataDescriptionEntity();

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

                            description.pages.Add(page);
                        }
                    }
                }

                return(description.pages);
            }
            catch (FileNotFoundException ex)
            {
                throw ex;
            }
        }
Beispiel #2
0
        static MemoryStream RenderPageToMemoryStream(GroupDocs.Annotation.Annotator annotator, int pageNumberToRender)
        {
            MemoryStream result = new MemoryStream();

            PreviewOptions previewOptions = new PreviewOptions(pageNumber => result)
            {
                PreviewFormat  = PreviewFormats.PNG,
                PageNumbers    = new[] { pageNumberToRender },
                RenderComments = false
            };

            annotator.Document.GeneratePreview(previewOptions);

            return(result);
        }
Beispiel #3
0
        public HttpResponseMessage LoadDocumentPage(AnnotationPostedDataEntity loadDocumentPageRequest)
        {
            string password = "";

            try
            {
                // get/set parameters
                string documentGuid = loadDocumentPageRequest.guid;
                int    pageNumber   = loadDocumentPageRequest.page;
                password = loadDocumentPageRequest.password;
                PageDataDescriptionEntity loadedPage = new PageDataDescriptionEntity();

                // get page image
                byte[] bytes;

                using (GroupDocs.Annotation.Annotator annotator = new GroupDocs.Annotation.Annotator(documentGuid, GetLoadOptions(password)))
                {
                    using (var memoryStream = RenderPageToMemoryStream(annotator, pageNumber))
                    {
                        bytes = memoryStream.ToArray();
                    }

                    IDocumentInfo    info         = annotator.Document.GetDocumentInfo();
                    AnnotationBase[] annotations  = annotator.Get().ToArray();
                    string           documentType = getDocumentType(info);

                    if (annotations != null && annotations.Length > 0)
                    {
                        loadedPage.SetAnnotations(AnnotationMapper.MapForPage(annotations, pageNumber, info.PagesInfo[pageNumber - 1], documentType));
                    }

                    string encodedImage = Convert.ToBase64String(bytes);
                    loadedPage.SetData(encodedImage);

                    loadedPage.height = info.PagesInfo[pageNumber - 1].Height;
                    loadedPage.width  = info.PagesInfo[pageNumber - 1].Width;
                    loadedPage.number = pageNumber;
                }

                // return loaded page object
                return(Request.CreateResponse(HttpStatusCode.OK, loadedPage));
            }
            catch (Exception ex)
            {
                // set exception message
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex, password)));
            }
        }
Beispiel #4
0
        public AnnotatedDocumentEntity LoadDocument(AnnotationPostedDataEntity loadDocumentRequest, bool loadAllPages)
        {
            string password = loadDocumentRequest.password;
            AnnotatedDocumentEntity description = new AnnotatedDocumentEntity();
            string documentGuid = loadDocumentRequest.guid;

            using (GroupDocs.Annotation.Annotator annotator = new GroupDocs.Annotation.Annotator(documentGuid, GetLoadOptions(password)))
            {
                IDocumentInfo    info        = annotator.Document.GetDocumentInfo();
                AnnotationBase[] annotations = annotator.Get().ToArray();
                description.guid = loadDocumentRequest.guid;
                string documentType = getDocumentType(info);

                description.supportedAnnotations = new SupportedAnnotations().GetSupportedAnnotations(documentType);

                List <string> pagesContent = new List <string>();

                if (loadAllPages)
                {
                    pagesContent = GetAllPagesContent(annotator, info);
                }

                for (int i = 0; i < info.PagesInfo.Count; i++)
                {
                    PageDataDescriptionEntity page = new PageDataDescriptionEntity
                    {
                        number = i + 1,
                        height = info.PagesInfo[i].Height,
                        width  = info.PagesInfo[i].Width,
                    };

                    if (annotations != null && annotations.Length > 0)
                    {
                        page.SetAnnotations(AnnotationMapper.MapForPage(annotations, i + 1, info.PagesInfo[i], documentType));
                    }

                    if (pagesContent.Count > 0)
                    {
                        page.SetData(pagesContent[i]);
                    }
                    description.pages.Add(page);
                }
            }

            description.guid = documentGuid;
            // return document description
            return(description);
        }
Beispiel #5
0
        private static List <string> GetAllPagesContent(GroupDocs.Annotation.Annotator annotator, IDocumentInfo pages)
        {
            List <string> allPages = new List <string>();

            //get page HTML
            for (int i = 0; i < pages.PageCount; i++)
            {
                byte[] bytes;
                using (var memoryStream = RenderPageToMemoryStream(annotator, i + 1))
                {
                    bytes = memoryStream.ToArray();
                }

                string encodedImage = Convert.ToBase64String(bytes);
                allPages.Add(encodedImage);
            }

            return(allPages);
        }
Beispiel #6
0
        public static void RemoveAnnotations(string documentGuid, string password)
        {
            string tempPath = GetTempPath(documentGuid);

            try
            {
                using (Stream inputStream = File.Open(documentGuid, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    using (GroupDocs.Annotation.Annotator annotator = new GroupDocs.Annotation.Annotator(inputStream, GetLoadOptions(password)))
                    {
                        annotator.Save(tempPath, new SaveOptions {
                            AnnotationTypes = AnnotationType.None
                        });
                    }
                }

                File.Delete(documentGuid);
                File.Move(tempPath, documentGuid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #7
0
        public HttpResponseMessage Annotate(AnnotationPostedDataEntity annotateDocumentRequest)
        {
            AnnotatedDocumentEntity annotatedDocument = new AnnotatedDocumentEntity();

            try
            {
                // get/set parameters
                string documentGuid = annotateDocumentRequest.guid;
                string password     = annotateDocumentRequest.password;
                string documentType = SupportedImageFormats.Contains(Path.GetExtension(annotateDocumentRequest.guid).ToLowerInvariant()) ? "image" : annotateDocumentRequest.documentType;
                string tempPath     = GetTempPath(documentGuid);

                AnnotationDataEntity[] annotationsData = annotateDocumentRequest.annotationsData;
                // initiate list of annotations to add
                List <AnnotationBase> annotations = new List <AnnotationBase>();

                using (GroupDocs.Annotation.Annotator annotator = new GroupDocs.Annotation.Annotator(documentGuid, GetLoadOptions(password)))
                {
                    IDocumentInfo info = annotator.Document.GetDocumentInfo();

                    for (int i = 0; i < annotationsData.Length; i++)
                    {
                        AnnotationDataEntity annotationData = annotationsData[i];
                        PageInfo             pageInfo       = info.PagesInfo[annotationsData[i].pageNumber - 1];
                        // add annotation, if current annotation type isn't supported by the current document type it will be ignored
                        try
                        {
                            BaseAnnotator baseAnnotator = AnnotatorFactory.createAnnotator(annotationData, pageInfo);
                            if (baseAnnotator.IsSupported(documentType))
                            {
                                annotations.Add(baseAnnotator.GetAnnotationBase(documentType));
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new AnnotatorException(ex.Message, ex);
                        }
                    }
                }

                // Add annotation to the document
                RemoveAnnotations(documentGuid, password);
                // check if annotations array contains at least one annotation to add
                if (annotations.Count != 0)
                {
                    using (GroupDocs.Annotation.Annotator annotator = new GroupDocs.Annotation.Annotator(documentGuid, GetLoadOptions(password)))
                    {
                        foreach (var annotation in annotations)
                        {
                            annotator.Add(annotation);
                        }

                        annotator.Save(tempPath);
                    }

                    if (File.Exists(documentGuid))
                    {
                        File.Delete(documentGuid);
                    }

                    File.Move(tempPath, documentGuid);
                }

                annotatedDocument      = new AnnotatedDocumentEntity();
                annotatedDocument.guid = documentGuid;
                if (annotateDocumentRequest.print)
                {
                    annotatedDocument.pages = GetAnnotatedPagesForPrint(password, documentGuid);
                    File.Move(documentGuid, annotateDocumentRequest.guid);
                }
            }
            catch (Exception ex)
            {
                // set exception message
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new Resources().GenerateException(ex)));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, annotatedDocument));
        }