Example #1
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);
        }
Example #2
0
        //ExEnd:GetConfiguration

        //ExStart:GetImageRepresentation
        /// <summary>
        /// Gets image representation of document
        /// </summary>
        /// <param name="CommonUtilities.filePath">Source file path</param>
        public static void GetImageRepresentation(string filePath)
        {
            try
            {
                Stream           document = new FileStream(MapSourceFilePath(filePath), FileMode.Open);
                AnnotationConfig cfg      = GetConfiguration();

                AnnotationImageHandler annotationHandler = new AnnotationImageHandler(cfg);

                List <PageImage> images = annotationHandler.GetPages(document, new ImageOptions());

                // Save result stream to file.
                using (FileStream fileStream = new FileStream(MapDestinationFilePath("image.png"), FileMode.Create))
                {
                    byte[] buffer = new byte[images[0].Stream.Length];
                    images[0].Stream.Seek(0, SeekOrigin.Begin);
                    images[0].Stream.Read(buffer, 0, buffer.Length);
                    fileStream.Write(buffer, 0, buffer.Length);
                    fileStream.Close();
                }
            }
            catch (System.Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
        /// <summary>
        /// Get Image Representation of Pages for PDF
        /// </summary>
        public static void GetImageRepresentationOfPagesForPDF()
        {
            try
            {
                //ExStart:GetImageRepresentationOfPagesForPDF
                // Create instance of annotator.
                AnnotationConfig cfg = CommonUtilities.GetConfiguration();

                AnnotationImageHandler annotator = new AnnotationImageHandler(cfg);

                // Get input file stream
                Stream inputFile = new FileStream(CommonUtilities.MapSourceFilePath(CommonUtilities.filePath), FileMode.Open, FileAccess.ReadWrite);

                List <PageImage> images = annotator.GetPages(inputFile, new ImageOptions {
                    WithoutAnnotations = true
                });

                // Save result stream to file.
                using (FileStream fileStream = new FileStream(CommonUtilities.MapDestinationFilePath("Annotated.png"), FileMode.Create))
                {
                    byte[] buffer = new byte[images[0].Stream.Length];
                    images[0].Stream.Seek(0, SeekOrigin.Begin);
                    images[0].Stream.Read(buffer, 0, buffer.Length);
                    fileStream.Write(buffer, 0, buffer.Length);
                    fileStream.Close();
                }
                //ExEnd:GetImageRepresentationOfPagesForPDF
            }
            catch (System.Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
        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)));
            }
        }
        public ActionResult Get(string file)
        {
            Response.AddHeader("Content-Type", "image/png");
            AnnotationImageHandler handler = Utils.createAnnotationImageHandler();

            ImageOptions o          = new ImageOptions();
            int          pageNumber = int.Parse(Request.Params["page"]);

            o.PageNumbersToConvert = new List <int>(pageNumber);
            o.PageNumber           = pageNumber;
            o.CountPagesToConvert  = 1;
            if (!string.IsNullOrEmpty(Request.Params["width"]))
            {
                o.Width = int.Parse(Request.Params["width"]);
            }
            if (!string.IsNullOrEmpty(Request.Params["height"]))
            {
                o.Height = int.Parse(Request.Params["height"]);
            }

            Stream           stream = null;
            List <PageImage> list   = handler.GetPages(file, o);

            foreach (PageImage pageImage in list.Where(x => x.PageNumber == pageNumber))
            {
                stream = pageImage.Stream;
            }
            ;
            if (stream != null && stream.CanSeek)
            {
                stream.Seek(0, SeekOrigin.Begin);
                return(new FileStreamResult(stream, "image/png"));
            }
            else
            {
                return(new HttpNotFoundResult("Document page " + pageNumber + " not found"));
            }
        }
        /// <summary>
        /// Shows how to get thumbnails of pages for PDF
        /// </summary>
        public static void GetThumbnailsOfPagesForPDF()
        {
            try
            {
                //ExStart:GetThumnailsOfPagesForPDF
                // Create instance of annotator.
                AnnotationConfig cfg = CommonUtilities.GetConfiguration();

                AnnotationImageHandler annotator = new AnnotationImageHandler(cfg);

                // Get input file stream
                Stream inputFile = new FileStream(CommonUtilities.MapSourceFilePath(CommonUtilities.filePath), FileMode.Open, FileAccess.ReadWrite);

                List <PageImage> images = annotator.GetPages(inputFile, new ImageOptions {
                    WithoutAnnotations = true
                });

                //Then if we want get thumbnail we call GetThumbnail() method of PageImage item:
                foreach (PageImage pageImage in images)
                {
                    Stream stream = pageImage.GetThumbnail(); // do something with stream
                }

                // Default image size was 300x180. If need specified image size, you can pass method parameters:
                // image thumbnails 100x100
                foreach (PageImage pageImage in images)
                {
                    Stream stream = pageImage.GetThumbnail(100, 100);
                    // do something with stream
                }
                //ExEnd:GetThumnailsOfPagesForPDF
            }
            catch (System.Exception exp)
            {
                Console.WriteLine(exp.Message);
            }
        }
        public ActionResult GetDocumentPageImage(GetDocumentPageImageParameters parameters)
        {
            var guid       = parameters.Path;
            var pageIndex  = parameters.PageIndex;
            var pageNumber = pageIndex + 1;

            var imageOptions = new ImageOptions
            {
                //ConvertImageFileType = _convertImageFileType,

                /*Watermark = Utils.GetWatermark(parameters.WatermarkText, parameters.WatermarkColor,
                 * parameters.WatermarkPosition, parameters.WatermarkWidth),*/
                //Transformations = parameters.Rotate ? Transformation.Rotate : Transformation.None,
                PageNumbersToConvert = new List <int>()
                {
                    parameters.PageIndex
                },
                PageNumber = pageNumber,
                //JpegQuality = parameters.Quality.GetValueOrDefault()
            };
            DocumentInfoContainer documentInfoContainer = annotator.GetDocumentInfo(guid);

            if (parameters.Rotate && parameters.Width.HasValue)
            {
                int pageAngle        = documentInfoContainer.Pages[pageIndex].Angle;
                var isHorizontalView = pageAngle == 90 || pageAngle == 270;

                int sideLength = parameters.Width.Value;
                if (isHorizontalView)
                {
                    imageOptions.Height = sideLength;
                }
                else
                {
                    imageOptions.Width = sideLength;
                }
            }
            else if (parameters.Width.HasValue)
            {
                imageOptions.Width = parameters.Width.Value;
            }

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

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

            PageImage pageImage;
            string    docFoldePath = Path.Combine(tempFolderPath, parameters.Path);

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

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

            if (!System.IO.File.Exists(pageImageName))
            {
                pageImage = annotator.GetPages(guid, imageOptions).Single();
                using (FileStream fileStream = new FileStream(pageImageName, FileMode.Create))
                {
                    pageImage.Stream.Seek(0, SeekOrigin.Begin);
                    pageImage.Stream.CopyTo(fileStream);
                }
                pageImage.Stream.Position = 0;
                return(File(pageImage.Stream, String.Format("image/{0}", "png")));
            }
            Stream stream = new MemoryStream();

            using (FileStream fsSource = new FileStream(pageImageName,
                                                        FileMode.Open, FileAccess.Read))
            {
                fsSource.Seek(0, SeekOrigin.Begin);
                fsSource.CopyTo(stream);
            }
            stream.Position = 0;
            return(File(stream, "image/png"));
        }