/// <summary>
 /// Get all annotations from the document
 /// </summary>
 /// <param name="documentGuid">string</param>
 /// <param name="documentType">string</param>
 /// <returns>AnnotationInfo[]</returns>
 private AnnotationInfo[] GetAnnotations(string documentGuid, string documentType, string password)
 {
     try
     {
         FileStream   documentStream = new FileStream(documentGuid, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
         DocumentType docType        = DocumentTypesConverter.GetDocumentType(documentType);
         return(new BaseImporter(documentStream, AnnotationImageHandler, password).ImportAnnotations(docType));
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
        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));
        }