public ActionResult ViewDocument(ViewDocumentParameters request)
        {
            string fileName = Path.GetFileName(request.Path);

            ViewDocumentResponse result = new ViewDocumentResponse
            {
                pageCss        = new string[] { },
                lic            = true,
                pdfDownloadUrl = GetPdfDownloadUrl(request),
                url            = GetFileUrl(request),
                path           = request.Path,
                name           = fileName
            };

            DocumentInfoContainer docInfo = annotator.GetDocumentInfo(request.Path);

            result.documentDescription = new FileDataJsonSerializer(docInfo.Pages).Serialize(true);
            result.docType             = docInfo.DocumentType.ToLower();
            result.fileType            = docInfo.FileType.ToLower();

            string applicationHost = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');

            var preloadCount = request.PreloadPagesCount;
            int pageCount    = preloadCount ?? 1;

            int[] pageNumbers = new int[docInfo.Pages.Count];
            for (int i = 0; i < pageNumbers.Length; i++)
            {
                pageNumbers[i] = i;
            }


            GetImageUrlsParameters imageUrlParameters = new GetImageUrlsParameters()
            {
                Path                = request.Path,
                FirstPage           = 0,
                PageCount           = pageNumbers.Length,
                UsePdf              = docInfo.Extension.ToLower().Equals("pdf"),
                Width               = docInfo.Pages[0].Width,
                SupportPageRotation = false,
                UseHtmlBasedEngine  = false
            };


            result.imageUrls = GetImageUrls(applicationHost, pageNumbers, imageUrlParameters);

            //result.imageUrls = urls.ToArray();

            JavaScriptSerializer serializer = new JavaScriptSerializer {
                MaxJsonLength = int.MaxValue
            };

            string serializedData = serializer.Serialize(result);

            // invoke event
            new DocumentOpenSubscriber().HandleEvent(request.Path);

            return(Content(serializedData, "application/json"));
        }
        public ActionResult Get(string file)
        {
            Response.AddHeader("Content-Type", "application/json");
            AnnotationImageHandler handler = Utils.createAnnotationImageHandler();

            int    pageNumber = int.Parse(Request.Params["page"]);
            String filename   = file;

            List <RowData>        result = new List <RowData>();
            DocumentInfoContainer documentInfoContainer = handler.GetDocumentInfo(filename);

            foreach (PageData pageData in documentInfoContainer.Pages)
            {
                if (pageData.Number == pageNumber)
                {
                    result = pageData.Rows;
                    break;
                }
            }
            return(Content(JsonConvert.SerializeObject(
                               result,
                               Formatting.Indented,
                               new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }
                               ), "application/json"));
        }
        //ExEnd:GetImageRepresentation

        //ExStart:GetTextCoordinatesInImage
        /// <summary>
        /// Gets text coordinates in image representation of document
        /// </summary>
        /// <param name="CommonUtilities.filePath">Source file path</param>
        public static void GetTextCoordinates(string filePath)
        {
            try
            {
                // Set configuration
                AnnotationConfig cfg = GetConfiguration();

                // Initialize annotator
                AnnotationImageHandler annotator = new AnnotationImageHandler(cfg);
                try
                {
                    annotator.CreateDocument(filePath);
                }
                catch { }

                var documentInfoContainer = annotator.GetDocumentInfo(filePath);

                // Go through all pages
                foreach (PageData pageData in documentInfoContainer.Pages)
                {
                    Console.WriteLine("Page number: " + pageData.Number);

                    //Go through all page rows
                    for (int i = 0; i < pageData.Rows.Count; i++)
                    {
                        RowData rowData = pageData.Rows[i];

                        // Write data to console
                        Console.WriteLine("Row: " + (i + 1));
                        Console.WriteLine("Text: " + rowData.Text);
                        Console.WriteLine("Text width: " + rowData.LineWidth);
                        Console.WriteLine("Text height: " + rowData.LineHeight);
                        Console.WriteLine("Distance from left: " + rowData.LineLeft);
                        Console.WriteLine("Distance from top: " + rowData.LineTop);

                        // Get words
                        string[] words = rowData.Text.Split(' ');

                        // Go through all word coordinates
                        for (int j = 0; j < words.Length; j++)
                        {
                            int coordinateIndex = j == 0 ? 0 : j + 1;
                            // Write data to console
                            Console.WriteLine(string.Empty);
                            Console.WriteLine("Word:'" + words[j] + "'");
                            Console.WriteLine("Word distance from left: " + rowData.TextCoordinates[coordinateIndex]);
                            Console.WriteLine("Word width: " + rowData.TextCoordinates[coordinateIndex + 1]);
                            Console.WriteLine(string.Empty);
                        }
                        Console.ReadKey();
                    }
                }
            }
            catch (System.Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
            //ExEnd:GetTextCoordinatesInImage
        }
Exemple #4
0
        public static ViewDocumentResponse ViewDocument(string path)
        {
            string request = path;

            string fileName             = Path.GetFileName(request);
            var    pathFinder           = new ApplicationPathFinder();
            string _appPath             = pathFinder.GetApplicationPath();
            ViewDocumentResponse result = new ViewDocumentResponse
            {
                pageCss        = new string[] { },
                lic            = true,
                pdfDownloadUrl = _appPath + "App_Data/" + request,
                url            = _appPath + "App_Data/" + request,
                path           = request,
                name           = fileName
            };
            DocumentInfoContainer docInfo = annotator.GetDocumentInfo(request);

            result.documentDescription = new FileDataJsonSerializer(docInfo.Pages).Serialize(true);
            result.docType             = docInfo.DocumentType;
            result.fileType            = docInfo.FileType;

            List <PageImage> imagePages = annotator.GetPages(request);

            // Provide images urls
            List <string> urls = new List <string>();

            // If no cache - save images to temp folder
            string tempFolderPath = Path.Combine(HttpContext.Current.Server.MapPath("~"), "Content", "TempStorage");

            foreach (PageImage pageImage in imagePages)
            {
                string docFoldePath = Path.Combine(tempFolderPath, request);

                if (!Directory.Exists(docFoldePath))
                {
                    Directory.CreateDirectory(docFoldePath);
                }

                string pageImageName = string.Format("{0}\\{1}.png", docFoldePath, pageImage.PageNumber);

                using (Stream stream = pageImage.Stream)
                    using (FileStream fileStream = new FileStream(pageImageName, FileMode.Create))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        stream.CopyTo(fileStream);
                    }
                string baseUrl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd('/') + "/";
                urls.Add(string.Format("{0}Content/TempStorage/{1}/{2}.png", baseUrl, request, pageImage.PageNumber));
            }

            result.imageUrls = urls.ToArray();

            // invoke event
            new DocumentOpenSubscriber().HandleEvent(request, _annotationSvc);

            return(result);
        }
Exemple #5
0
        public ActionResult Get(string file)
        {
            AnnotationImageHandler handler = Utils.createAnnotationImageHandler();
            String filename = file;
            DocumentInfoContainer result = handler.GetDocumentInfo(filename);

            Response.AddHeader("Content-Type", "application/json");
            return(Content(JsonConvert.SerializeObject(
                               result,
                               Formatting.Indented,
                               new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            }
                               ), "application/json"));
        }
        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)));
            }
        }