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; } }
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); }
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 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))); } }