Exemplo n.º 1
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);
        }
Exemplo n.º 2
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)));
            }
        }
        public HttpResponseMessage loadFileTree(AnnotationPostedDataEntity fileTreeRequest)
        {
            string relDirPath = fileTreeRequest.path;
            // get file list from storage path
            FileTreeOptions fileListOptions = new FileTreeOptions(relDirPath);
            // get temp directory name
            string tempDirectoryName = new AnnotationConfig().TempFolderName;

            try
            {
                FileTreeContainer fileListContainer = AnnotationImageHandler.LoadFileTree(fileListOptions);

                List <FileDescriptionEntity> fileList = new List <FileDescriptionEntity>();
                // parse files/folders list
                foreach (FileDescription fd in fileListContainer.FileTree)
                {
                    FileDescriptionEntity fileDescription = new FileDescriptionEntity();
                    fileDescription.guid = fd.Guid;
                    // check if current file/folder is temp directory or is hidden
                    FileInfo fileInfo = new FileInfo(fileDescription.guid);
                    if (tempDirectoryName.ToLower().Equals(fileDescription.guid) || fileInfo.Attributes.HasFlag(FileAttributes.Hidden))
                    {
                        // ignore current file and skip to next one
                        continue;
                    }
                    else
                    {
                        // set file/folder name
                        fileDescription.name = fd.Name;
                    }
                    // set file type
                    fileDescription.docType = fd.DocumentType;
                    // set is directory true/false
                    fileDescription.isDirectory = fd.IsDirectory;
                    // set file size
                    fileDescription.size = fd.Size;
                    // add object to array list
                    fileList.Add(fileDescription);
                }
                return(Request.CreateResponse(HttpStatusCode.OK, fileList));
            }
            catch (System.Exception ex)
            {
                // set exception message
                return(Request.CreateResponse(HttpStatusCode.OK, new Resources().GenerateException(ex)));
            }
        }
Exemplo n.º 4
0
        public HttpResponseMessage LoadDocumentDescription(AnnotationPostedDataEntity postedData)
        {
            string password = "";

            try
            {
                AnnotatedDocumentEntity loadDocumentEntity = LoadDocument(postedData, globalConfiguration.GetAnnotationConfiguration().GetPreloadPageCount() == 0);
                // return document description
                return(Request.CreateResponse(HttpStatusCode.OK, loadDocumentEntity));
            }
            catch (Exception ex)
            {
                // set exception message
                // TODO: return InternalServerError for common Exception and Forbidden for PasswordProtectedException
                return(Request.CreateResponse(HttpStatusCode.Forbidden, new Resources().GenerateException(ex, password)));
            }
        }
        public HttpResponseMessage loadDocumentPage(AnnotationPostedDataEntity loadDocumentPageRequest)
        {
            try
            {
                // get/set parameters
                string           documentGuid = loadDocumentPageRequest.guid;
                int              pageNumber   = loadDocumentPageRequest.page;
                string           password     = loadDocumentPageRequest.password;
                LoadedPageEntity loadedPage   = new LoadedPageEntity();
                ImageOptions     imageOptions = new ImageOptions()
                {
                    PageNumber          = pageNumber,
                    CountPagesToConvert = 1
                };
                // get page image

                byte[] bytes;
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (Stream document = File.Open(documentGuid, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        List <PageImage> images      = AnnotationImageHandler.GetPages(document, imageOptions);
                        Stream           imageStream = images[pageNumber - 1].Stream;
                        imageStream.Position = 0;
                        imageStream.CopyTo(memoryStream);
                        bytes = memoryStream.ToArray();
                        foreach (PageImage page in images)
                        {
                            page.Stream.Close();
                        }
                    }
                }
                string encodedImage = Convert.ToBase64String(bytes);
                loadedPage.pageImage = encodedImage;
                // return loaded page object
                return(Request.CreateResponse(HttpStatusCode.OK, loadedPage));
            }
            catch (System.Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK, new Resources().GenerateException(ex)));
            }
        }
Exemplo n.º 6
0
        public void FileTreeStatusCodeTest()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "/../../../src";

            using (var server = new DirectServer(path))
            {
                AnnotationPostedDataEntity requestData = new AnnotationPostedDataEntity();
                requestData.path = "";

                var request = new SerialisableRequest
                {
                    Method     = "POST",
                    RequestUri = "/annotation/loadfiletree",
                    Content    = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestData)),
                    Headers    = new Dictionary <string, string> {
                        { "Content-Type", "application/json" },
                        { "Content-Length", JsonConvert.SerializeObject(requestData).Length.ToString() }
                    }
                };

                var result = server.DirectCall(request);
                Assert.That(result.StatusCode, Is.EqualTo(200));
            }
        }
Exemplo n.º 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));
        }
        public HttpResponseMessage Annotate(AnnotationPostedDataEntity annotateDocumentRequest)
        {
            AnnotatedDocumentEntity annotatedDocument = new AnnotatedDocumentEntity();

            try
            {
                // get/set parameters
                string documentGuid = annotateDocumentRequest.guid;
                string password     = annotateDocumentRequest.password;
                string documentType = annotateDocumentRequest.documentType;
                AnnotationDataEntity[] annotationsData = annotateDocumentRequest.annotationsData;
                // initiate AnnotatedDocument object
                // initiate list of annotations to add
                List <AnnotationInfo> annotations = new List <AnnotationInfo>();
                // get document info - required to get document page height and calculate annotation top position
                string        fileName  = System.IO.Path.GetFileName(documentGuid);
                FileInfo      fi        = new FileInfo(documentGuid);
                DirectoryInfo parentDir = fi.Directory;

                string documentPath  = "";
                string parentDirName = parentDir.Name;
                if (parentDir.FullName == GlobalConfiguration.Annotation.FilesDirectory.Replace("/", "\\"))
                {
                    documentPath = fileName;
                }
                else
                {
                    documentPath = Path.Combine(parentDirName, fileName);
                }
                DocumentInfoContainer documentInfo = AnnotationImageHandler.GetDocumentInfo(documentPath, password);
                // check if document type is image
                if (SupportedImageFormats.Contains(Path.GetExtension(documentGuid)))
                {
                    documentType = "image";
                }
                // initiate annotator object
                string notSupportedMessage = "";
                for (int i = 0; i < annotationsData.Length; i++)
                {
                    // create annotator
                    AnnotationDataEntity annotationData = annotationsData[i];
                    PageData             pageData       = documentInfo.Pages[annotationData.pageNumber - 1];
                    // add annotation, if current annotation type isn't supported by the current document type it will be ignored
                    try
                    {
                        BaseAnnotator annotator = AnnotatorFactory.createAnnotator(annotationData, pageData);
                        if (annotator.IsSupported(documentType))
                        {
                            annotations.Add(annotator.GetAnnotationInfo(documentType));
                        }
                        else
                        {
                            notSupportedMessage = annotator.Message;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        throw new System.Exception(ex.Message, ex);
                    }
                }

                // Add annotation to the document
                DocumentType type = DocumentTypesConverter.GetDocumentType(documentType);
                // Save result stream to file.
                string path = GlobalConfiguration.Annotation.OutputDirectory + Path.DirectorySeparatorChar + fileName;
                if (File.Exists(path))
                {
                    RemoveAnnotations(path);
                }
                // check if annotations array contains at least one annotation to add
                if (annotations.Count != 0)
                {
                    Stream cleanDoc = new FileStream(documentGuid, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                    Stream result   = AnnotationImageHandler.ExportAnnotationsToDocument(cleanDoc, annotations, type);
                    cleanDoc.Dispose();
                    cleanDoc.Close();
                    // Save result stream to file.
                    using (FileStream fileStream = new FileStream(path, FileMode.Create))
                    {
                        byte[] buffer = new byte[result.Length];
                        result.Seek(0, SeekOrigin.Begin);
                        result.Read(buffer, 0, buffer.Length);
                        fileStream.Write(buffer, 0, buffer.Length);
                        fileStream.Close();
                    }
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.OK, new Resources().GenerateException(new NotSupportedException(notSupportedMessage))));
                }
                annotatedDocument = new AnnotatedDocumentEntity()
                {
                    guid = path,
                };
            }
            catch (System.Exception ex)
            {
                // set exception message
                return(Request.CreateResponse(HttpStatusCode.OK, new Resources().GenerateException(ex)));
            }
            return(Request.CreateResponse(HttpStatusCode.OK, annotatedDocument));
        }
        public HttpResponseMessage loadDocumentDescription(AnnotationPostedDataEntity loadDocumentRequest)
        {
            try
            {
                // get/set parameters
                string documentGuid = loadDocumentRequest.guid;
                string password     = loadDocumentRequest.password;
                DocumentInfoContainer documentDescription;
                // get document info container
                string        fileName  = System.IO.Path.GetFileName(documentGuid);
                FileInfo      fi        = new FileInfo(documentGuid);
                DirectoryInfo parentDir = fi.Directory;

                string documentPath  = "";
                string parentDirName = parentDir.Name;
                if (parentDir.FullName == GlobalConfiguration.Annotation.FilesDirectory.Replace("/", "\\"))
                {
                    documentPath = fileName;
                }
                else
                {
                    documentPath = Path.Combine(parentDirName, fileName);
                }

                documentDescription = AnnotationImageHandler.GetDocumentInfo(documentPath, password);
                string documentType  = documentDescription.DocumentType;
                string fileExtension = Path.GetExtension(documentGuid);
                // check if document type is image
                if (SupportedImageFormats.Contains(fileExtension))
                {
                    documentType = "image";
                }
                else if (SupportedDiagrammFormats.Contains(fileExtension))
                {
                    documentType = "diagram";
                }
                // check if document contains annotations
                AnnotationInfo[] annotations = GetAnnotations(documentGuid, documentType, password);
                // initiate pages description list
                List <AnnotatedDocumentEntity> pagesDescription = new List <AnnotatedDocumentEntity>();
                // get info about each document page
                for (int i = 0; i < documentDescription.Pages.Count; i++)
                {
                    //initiate custom Document description object
                    AnnotatedDocumentEntity description = new AnnotatedDocumentEntity();
                    description.guid = documentGuid;
                    // set current page info for result
                    PageData pageData = documentDescription.Pages[i];
                    description.height = pageData.Height;
                    description.width  = pageData.Width;
                    description.number = pageData.Number;
                    description.supportedAnnotations = new SupportedAnnotations().GetSupportedAnnotations(documentType);
                    // set annotations data if document page contains annotations
                    if (annotations != null && annotations.Length > 0)
                    {
                        description.annotations = AnnotationMapper.instance.mapForPage(annotations, description.number);
                    }
                    pagesDescription.Add(description);
                }
                // return document description
                return(Request.CreateResponse(HttpStatusCode.OK, pagesDescription));
            }
            catch (System.Exception ex)
            {
                // set exception message
                return(Request.CreateResponse(HttpStatusCode.OK, new Resources().GenerateException(ex)));
            }
        }