/// <summary> /// Rotate page in Image mode /// </summary> public static void Reorder_Pages_In_Image_Mode() { Console.WriteLine("***** {0} *****", "Reorder pages in Image mode"); /* ********************* SAMPLE ********************* */ /* ******************** Reorder 1st and 2nd pages *********************** */ // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); string guid = "word.doc"; int pageNumber = 1; int newPosition = 2; // Perform page reorder ReorderPageOptions options = new ReorderPageOptions(guid, pageNumber, newPosition); imageHandler.ReorderPage(options); /* ******************** Retrieve all document pages including transformation *********************** */ // Set image options to include reorder transformations ImageOptions imageOptions = new ImageOptions { Transformations = Transformation.Reorder }; // Get image representation of all document pages, including reorder transformations List<PageImage> pages = imageHandler.GetPages(guid, imageOptions); /* ******************** Retrieve all document pages excluding transformation *********************** */ // Set image options NOT to include ANY transformations ImageOptions noTransformationsOptions = new ImageOptions { Transformations = Transformation.None // This is by default }; // Get image representation of all document pages, without transformations List<PageImage> pagesWithoutTransformations = imageHandler.GetPages(guid, noTransformationsOptions); // Get image representation of all document pages, without transformations List<PageImage> pagesWithoutTransformations2 = imageHandler.GetPages(guid); }
public ActionResult Get(int?width, int?height, string file, string attachment, int page) { ViewerImageHandler handler = Utils.CreateViewerImageHandler(); ImageOptions o = new ImageOptions(); List <int> pageNumberstoRender = new List <int>(); pageNumberstoRender.Add(page); o.PageNumbersToRender = pageNumberstoRender; o.PageNumber = page; o.CountPagesToRender = 1; if (width.HasValue) { o.Width = Convert.ToInt32(width); } if (height.HasValue) { o.Height = Convert.ToInt32(height); } Stream stream = null; DocumentInfoContainer info = handler.GetDocumentInfo(file); // Iterate over the attachments collection foreach (AttachmentBase attachmentBase in info.Attachments.Where(x => x.Name == attachment)) { List <PageImage> pages = handler.GetPages(attachmentBase); foreach (PageImage attachmentPage in pages.Where(x => x.PageNumber == page)) { stream = attachmentPage.Stream; } ; } return(new FileStreamResult(stream, "image/png")); }
/// <summary> /// Render document in image representation with watermark /// </summary> /// <param name="DocumentName">file/document name</param> /// <param name="WatermarkText">watermark text</param> /// <param name="WatermarkColor"> System.Drawing.Color</param> /// <param name="position">Watermark Position is optional parameter. Default value is WatermarkPosition.Diagonal</param> /// <param name="WatermarkWidth"> width of watermark as integer. it is optional Parameter default value is 100</param> /// <param name="DocumentPassword">Password Parameter is optional</param> public static void RenderDocumentAsImages(String DocumentName, String WatermarkText, Color WatermarkColor, WatermarkPosition position = WatermarkPosition.Diagonal, int WatermarkWidth = 100, String DocumentPassword = null) { //ExStart:RenderAsImageWithWaterMark //Get Configurations ViewerConfig config = Utilities.GetConfigurations(); // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); // Guid implies that unique document name string guid = DocumentName; //Initialize ImageOptions Object ImageOptions options = new ImageOptions(); // Set password if document is password protected. if (!String.IsNullOrEmpty(DocumentPassword)) { options.Password = DocumentPassword; } // Call AddWatermark and pass the reference of ImageOptions object as 1st parameter Utilities.PageTransformations.AddWatermark(ref options, WatermarkText, WatermarkColor, position, WatermarkWidth); //Get document pages in image form List <PageImage> Images = imageHandler.GetPages(guid, options); foreach (PageImage image in Images) { //Save each image at disk Utilities.SaveAsImage(image.PageNumber + "_" + DocumentName, image.Stream); } //ExEnd:RenderAsImageWithWaterMark }
/// <summary> /// Render simple document in image representation /// </summary> /// <param name="DocumentName">File name</param> /// <param name="DocumentPassword">Optional</param> public static void RenderDocumentAsImages(String DocumentName, String DocumentPassword = null) { //ExStart:RenderAsImage //Get Configurations ViewerConfig config = Utilities.GetConfigurations(); // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); // Guid implies that unique document name string guid = DocumentName; //Initialize ImageOptions Object ImageOptions options = new ImageOptions(); // Set password if document is password protected. if (!String.IsNullOrEmpty(DocumentPassword)) { options.Password = DocumentPassword; } //Get document pages in image form List <PageImage> Images = imageHandler.GetPages(guid, options); foreach (PageImage image in Images) { //Save each image at disk Utilities.SaveAsImage(image.PageNumber + "_" + DocumentName, image.Stream); } //ExEnd:RenderAsImage }
/// <summary> /// Render a document in image representation whom located at web/remote location. /// </summary> /// <param name="DocumentURL">URL of the document</param> /// <param name="DocumentPassword">Password Parameter is optional</param> public static void RenderDocumentAsImages(Uri DocumentURL, String DocumentPassword = null) { //ExStart:RenderRemoteDocAsImages //Get Configurations ViewerConfig config = Utilities.GetConfigurations(); // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); //Initialize ImageOptions Object ImageOptions options = new ImageOptions(); // Set password if document is password protected. if (!String.IsNullOrEmpty(DocumentPassword)) { options.Password = DocumentPassword; } //Get document pages in image form List <PageImage> Images = imageHandler.GetPages(DocumentURL, options); foreach (PageImage image in Images) { //Save each image at disk Utilities.SaveAsImage(image.PageNumber + "_" + Path.GetFileName(DocumentURL.LocalPath), image.Stream); } //ExEnd:RenderRemoteDocAsImages }
public static void Add_Watermark_For_Image() { Console.WriteLine("***** {0} *****", "Add Watermark to Image page representation"); /* ********************* SAMPLE ********************* */ // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); string guid = "word.doc"; ImageOptions options = new ImageOptions(); // Set watermark properties Watermark watermark = new Watermark("This is watermark text"); watermark.Color = System.Drawing.Color.Blue; watermark.Position = WatermarkPosition.Diagonal; watermark.Width = 100; options.Watermark = watermark; // Get document pages image representation with watermark List<PageImage> pages = imageHandler.GetPages(guid, options); }
/// <summary> /// Get attached file's image representation /// </summary> /// <param name="DocumentName">Input document name</param> public static void GetEmailAttachmentImageRepresentation(String DocumentName) { try { //ExStart:GetEmailAttachmentImageRepresentation // Setup GroupDocs.Viewer config ViewerConfig config = Utilities.GetConfigurations(); // Init viewer image handler ViewerImageHandler handler = new ViewerImageHandler(config); DocumentInfoContainer info = handler.GetDocumentInfo(DocumentName); // Iterate over the attachments collection foreach (AttachmentBase attachment in info.Attachments) { Console.WriteLine("Attach name: {0}, size: {1}", attachment.Name, attachment.FileType); // Get attachment document image representation List <PageImage> pages = handler.GetPages(attachment); foreach (PageImage page in pages) { Console.WriteLine(" Page: {0}, size: {1}", page.PageNumber, page.Stream.Length); } } //ExEnd:GetEmailAttachmentImageRepresentation } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
public static void InImageRepresentation() { // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); string guid = "document.xlsx"; // Set image options to show grid lines ImageOptions options = new ImageOptions(); options.CellsOptions.ShowHiddenSheets = true; DocumentInfoContainer container = imageHandler.GetDocumentInfo(new DocumentInfoOptions(guid)); foreach (PageData page in container.Pages) Console.WriteLine("Page number: {0}, Page Name: {1}, IsVisible: {2}", page.Number, page.Name, page.IsVisible); List<PageImage> pages = imageHandler.GetPages(guid, options); foreach (PageImage page in pages) { Console.WriteLine("Page number: {0}", page.PageNumber); // Page image stream Stream imageContent = page.Stream; } }
public static void Run() { //Initialize viewer config ViewerConfig viewerConfig = new ViewerConfig(); viewerConfig.StoragePath = "c:\\storage"; //Initialize viewer handler ViewerImageHandler viewerImageHandler = new ViewerImageHandler(viewerConfig); //Set encoding Encoding encoding = Encoding.GetEncoding("shift-jis"); //Set image options ImageOptions imageOptions = new ImageOptions(); imageOptions.WordsOptions.Encoding = encoding; imageOptions.CellsOptions.Encoding = encoding; imageOptions.EmailOptions.Encoding = encoding; //Get words document pages with encoding string wordsDocumentGuid = "document.txt"; List<PageImage> wordsDocumentPages = viewerImageHandler.GetPages(wordsDocumentGuid, imageOptions); //Get cells document pages with encoding string cellsDocumentGuid = "document.csv"; List<PageImage> cellsDocumentPages = viewerImageHandler.GetPages(cellsDocumentGuid, imageOptions); //Get email document pages with encoding string emailDocumentGuid = "document.msg"; List<PageImage> emailDocumentPages = viewerImageHandler.GetPages(emailDocumentGuid, imageOptions); //Get words document info with encoding DocumentInfoOptions wordsDocumentInfoOptions = new DocumentInfoOptions(wordsDocumentGuid); wordsDocumentInfoOptions.WordsDocumentInfoOptions.Encoding = encoding; DocumentInfoContainer wordsDocumentInfoContainer = viewerImageHandler.GetDocumentInfo(wordsDocumentInfoOptions); //Get cells document info with encoding DocumentInfoOptions cellsDocumentInfoOptions = new DocumentInfoOptions(cellsDocumentGuid); cellsDocumentInfoOptions.CellsDocumentInfoOptions.Encoding = encoding; DocumentInfoContainer cellsDocumentInfoContainer = viewerImageHandler.GetDocumentInfo(cellsDocumentInfoOptions); //Get email document info with encoding DocumentInfoOptions emailDocumentInfoOptions = new DocumentInfoOptions(emailDocumentGuid); emailDocumentInfoOptions.EmailDocumentInfoOptions.Encoding = encoding; DocumentInfoContainer emailDocumentInfoContainer = viewerImageHandler.GetDocumentInfo(emailDocumentInfoOptions); }
public ActionResult GetDocumentPageImage(GetDocumentPageImageParameters parameters) { //parameters.IgnoreDocumentAbsence - not supported //parameters.InstanceIdToken - not supported string guid = parameters.Path; int pageIndex = parameters.PageIndex; int pageNumber = pageIndex + 1; /* * //NOTE: This feature is supported starting from version 3.2.0 * CultureInfo cultureInfo = string.IsNullOrEmpty(parameters.Locale) * ? new CultureInfo("en-Us") * : new CultureInfo(parameters.Locale); * * ViewerImageHandler viewerImageHandler = new ViewerImageHandler(viewerConfig, cultureInfo); */ var imageOptions = new ImageOptions { ConvertImageFileType = _convertImageFileType, Watermark = Utils.GetWatermark(parameters.WatermarkText, parameters.WatermarkColor, parameters.WatermarkPosition, parameters.WatermarkWidth), Transformations = parameters.Rotate ? Transformation.Rotate : Transformation.None }; if (parameters.Rotate && parameters.Width.HasValue) { DocumentInfoOptions documentInfoOptions = new DocumentInfoOptions(guid); DocumentInfoContainer documentInfoContainer = _imageHandler.GetDocumentInfo(documentInfoOptions); int side = parameters.Width.Value; int pageAngle = documentInfoContainer.Pages[pageIndex].Angle; if (pageAngle == 90 || pageAngle == 270) { imageOptions.Height = side; } else { imageOptions.Width = side; } } /* * //NOTE: This feature is supported starting from version 3.2.0 * if (parameters.Quality.HasValue) * imageOptions.JpegQuality = parameters.Quality.Value; */ using (new InterProcessLock(guid)) { List <PageImage> pageImages = _imageHandler.GetPages(guid, imageOptions); PageImage pageImage = pageImages.Single(_ => _.PageNumber == pageNumber); return(File(pageImage.Stream, GetContentType(_convertImageFileType))); } }
public static List <PageImage> LoadPageImageList(ViewerImageHandler handler, String filename, ImageOptions options) { try { return(handler.GetPages(filename, options)); } catch (Exception x) { throw x; } }
/// <summary> /// document in image representation and reorder a page /// </summary> /// <param name="DocumentName">file/document name</param> /// <param name="CurrentPageNumber">Page existing order number</param> /// <param name="NewPageNumber">Page new order number</param> /// <param name="DocumentPassword">Password Parameter is optional</param> public static List <ImageInfo> RenderDocumentAsImages(String DocumentName, int CurrentPageNumber, int NewPageNumber, String DocumentPassword = null) { //ExStart:RenderAsImageAndReorderPage //Get Configurations ViewerConfig config = Utilities.GetConfigurations(); // Cast ViewerHtmlHandler class object to its base class(ViewerHandler). ViewerHandler <PageImage> handler = new ViewerImageHandler(config); // Guid implies that unique document name string guid = DocumentName; //Initialize ImageOptions Object and setting Reorder Transformation ImageOptions options = new ImageOptions { Transformations = Transformation.Reorder }; // Set password if document is password protected. if (!String.IsNullOrEmpty(DocumentPassword)) { options.Password = DocumentPassword; } //Call ReorderPage and pass the reference of ViewerHandler's class parameter by reference. Utilities.PageTransformations.ReorderPage(ref handler, guid, CurrentPageNumber, NewPageNumber); //down cast the handler(ViewerHandler) to viewerHtmlHandler ViewerImageHandler imageHandler = (ViewerImageHandler)handler; //Get document pages in image form List <PageImage> Images = imageHandler.GetPages(guid, options); List <ImageInfo> contents = new List <ImageInfo>(); foreach (PageImage image in Images) { string imgname = image.PageNumber + "_" + Path.GetFileNameWithoutExtension(DocumentName); imgname = Regex.Replace(imgname, @"\s+", "_"); Utilities.SaveAsImage(Path.GetDirectoryName(DocumentName), imgname, image.Stream); ImageInfo imageInfo = new ImageInfo(); imageInfo.ImageUrl = @"/Uploads/images/" + imgname + ".jpg?" + Guid.NewGuid().ToString(); imageInfo.PageNmber = image.PageNumber; imageInfo.HtmlContent = @"<div class='image_page'><img src='" + imageInfo.ImageUrl + "' /></div>"; contents.Add(imageInfo); } return(contents); //ExEnd:RenderAsImageAndReorderPage }
/// <summary> /// Render document in image representation with watermark /// </summary> /// <param name="DocumentName">file/document name</param> /// <param name="WatermarkText">watermark text</param> /// <param name="WatermarkColor"> System.Drawing.Color</param> /// <param name="position">Watermark Position is optional parameter. Default value is WatermarkPosition.Diagonal</param> /// <param name="WatermarkWidth"> width of watermark as integer. it is optional Parameter default value is 100</param> /// <param name="DocumentPassword">Password Parameter is optional</param> public static List <ImageInfo> RenderDocumentAsImages(String DocumentName, String WatermarkText, Color WatermarkColor, int WatermarkWidth = 100, String DocumentPassword = null) { //ExStart:RenderAsImageWithWaterMark //Get Configurations ViewerConfig config = Utilities.GetConfigurations(); // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); // Guid implies that unique document name string guid = DocumentName; //Initialize ImageOptions Object ImageOptions options = new ImageOptions(); // Set password if document is password protected. if (!String.IsNullOrEmpty(DocumentPassword)) { options.Password = DocumentPassword; } // Call AddWatermark and pass the reference of ImageOptions object as 1st parameter Utilities.PageTransformations.AddWatermark(ref options, WatermarkText, WatermarkColor, WatermarkPosition.Diagonal, WatermarkWidth); //Get document pages in image form List <PageImage> Images = imageHandler.GetPages(guid, options); List <ImageInfo> contents = new List <ImageInfo>(); foreach (PageImage image in Images) { string imgname = image.PageNumber + "_" + Path.GetFileNameWithoutExtension(DocumentName); imgname = Regex.Replace(imgname, @"\s+", "_"); Utilities.SaveAsImage(Path.GetDirectoryName(DocumentName), imgname, image.Stream); ImageInfo imageInfo = new ImageInfo(); imageInfo.ImageUrl = @"/Uploads/images/" + imgname + ".jpg?" + Guid.NewGuid().ToString(); imageInfo.PageNmber = image.PageNumber; imageInfo.HtmlContent = @"<div class='image_page'><img src='" + imageInfo.ImageUrl + "' /></div>"; contents.Add(imageInfo); } return(contents); //ExEnd:RenderAsImageWithWaterMark }
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, CountPagesToRender = 1, PageNumber = pageNumber, JpegQuality = parameters.Quality.GetValueOrDefault() }; imageOptions.CellsOptions.OnePagePerSheet = false; if (parameters.Rotate && parameters.Width.HasValue) { DocumentInfoContainer documentInfoContainer = _imageHandler.GetDocumentInfo(guid); 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; } var pageImage = _imageHandler.GetPages(guid, imageOptions).Single(); return(File(pageImage.Stream, Utils.GetMimeType(_convertImageFileType))); }
/// <summary> /// Get document representation from Uri /// </summary> public static void Get_pages_from_Uri() { Console.WriteLine("***** {0} *****", "Get document representation from Uri"); /* ********************* SAMPLE ********************* */ // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); Uri uri = new Uri("http://groupdocs.com/images/banner/carousel2/signature.png"); // Get pages by absolute path List<PageImage> pages = imageHandler.GetPages(uri); Console.WriteLine("Page count: {0}", pages.Count); }
public HttpResponseMessage Get(int?width, string file, string attachment, int page, string watermarkText, int?watermarkColor, WatermarkPosition?watermarkPosition, int?watermarkWidth, byte watermarkOpacity, int?height = null) { ViewerImageHandler handler = Utils.CreateViewerImageHandler(); ImageOptions o = new ImageOptions(); List <int> pageNumberstoRender = new List <int>(); pageNumberstoRender.Add(page); o.PageNumbersToRender = pageNumberstoRender; o.PageNumber = page; o.CountPagesToRender = 1; if (width.HasValue) { o.Width = Convert.ToInt32(width); } if (height.HasValue) { o.Height = Convert.ToInt32(height); } if (watermarkText != "") { o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity); } Stream stream = null; DocumentInfoContainer info = handler.GetDocumentInfo(file); // Iterate over the attachments collection foreach (AttachmentBase attachmentBase in info.Attachments.Where(x => x.Name == attachment)) { List <PageImage> pages = handler.GetPages(attachmentBase, o); foreach (PageImage attachmentPage in pages.Where(x => x.PageNumber == page)) { stream = attachmentPage.Stream; } ; } var result = new HttpResponseMessage(HttpStatusCode.OK); Image image = Image.FromStream(stream); MemoryStream memoryStream = new MemoryStream(); image.Save(memoryStream, ImageFormat.Jpeg); result.Content = new ByteArrayContent(memoryStream.ToArray()); result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); return(result); }
/// <summary> /// Get document representation from relative path /// </summary> public static void Get_pages_relative_path() { Console.WriteLine("***** {0} *****", "Get document representation from relative path"); /* ********************* SAMPLE ********************* */ // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); // Set relative path. So that full path will be C:\storage\word.doc string guid = "word.doc"; // Get pages by absolute path List<PageImage> pages = imageHandler.GetPages(guid); Console.WriteLine("Page count: {0}", pages.Count); }
/// <summary> /// document in image representation and reorder a page /// </summary> /// <param name="DocumentName">file/document name</param> /// <param name="CurrentPageNumber">Page existing order number</param> /// <param name="NewPageNumber">Page new order number</param> /// <param name="DocumentPassword">Password Parameter is optional</param> public static void RenderDocumentAsImages(String DocumentName, int CurrentPageNumber, int NewPageNumber, String DocumentPassword = null) { //ExStart:RenderAsImageAndReorderPage //Get Configurations ViewerConfig config = Utilities.GetConfigurations(); // Cast ViewerHtmlHandler class object to its base class(ViewerHandler). ViewerHandler <PageImage> handler = new ViewerImageHandler(config); // Guid implies that unique document name string guid = DocumentName; //Initialize ImageOptions Object and setting Reorder Transformation ImageOptions options = new ImageOptions { Transformations = Transformation.Reorder }; // Set password if document is password protected. if (!String.IsNullOrEmpty(DocumentPassword)) { options.Password = DocumentPassword; } //Call ReorderPage and pass the reference of ViewerHandler's class parameter by reference. Utilities.PageTransformations.ReorderPage(ref handler, guid, CurrentPageNumber, NewPageNumber); //down cast the handler(ViewerHandler) to viewerHtmlHandler ViewerImageHandler imageHandler = (ViewerImageHandler)handler; //Get document pages in image form List <PageImage> Images = imageHandler.GetPages(guid, options); foreach (PageImage image in Images) { //Save each image at disk Utilities.SaveAsImage(image.PageNumber + "_" + DocumentName, image.Stream); } //ExEnd:RenderAsImageAndReorderPage }
/// <summary> /// Perform multiple transformations in Image mode /// </summary> public static void Multiple_Transformations_For_Image() { Console.WriteLine("***** {0} *****", "Perform multiple transformations in Image mode"); /* ********************* SAMPLE ********************* */ // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); string guid = "word.doc"; // Rotate first page 90 degrees imageHandler.RotatePage(new RotatePageOptions(guid, 1, 90)); // Rotate second page 180 degrees imageHandler.RotatePage(new RotatePageOptions(guid, 2, 180)); // Reorder first and second pages imageHandler.ReorderPage(new ReorderPageOptions(guid, 1, 2)); // Set options to include rotate and reorder transformations ImageOptions options = new ImageOptions { Transformations = Transformation.Rotate | Transformation.Reorder }; // Set watermark properties Watermark watermark = new Watermark("This is watermark text") { Color = System.Drawing.Color.Blue, Position = WatermarkPosition.Diagonal, Width = 100 }; options.Watermark = watermark; // Get document pages image representation with multiple transformations List<PageImage> pages = imageHandler.GetPages(guid, options); }
/// <summary> /// Render the document in image form and set the rotation angle to rotate the page while display. /// </summary> /// <param name="DocumentName"></param> /// <param name="RotationAngle">rotation angle in digits</param> /// <param name="DocumentPassword"></param> public static void RenderDocumentAsImages(String DocumentName, int RotationAngle, String DocumentPassword = null) { //ExStart:RenderAsImageWithRotationTransformation //Get Configurations ViewerConfig config = Utilities.GetConfigurations(); // Create image handler ViewerHandler <PageImage> handler = new ViewerImageHandler(config); // Guid implies that unique document name string guid = DocumentName; //Initialize ImageOptions Object and setting Rotate Transformation ImageOptions options = new ImageOptions { Transformations = Transformation.Rotate }; // Set password if document is password protected. if (!String.IsNullOrEmpty(DocumentPassword)) { options.Password = DocumentPassword; } //Call RotatePages to apply rotate transformation to a page Utilities.PageTransformations.RotatePages(ref handler, guid, 1, RotationAngle); //down cast the handler(ViewerHandler) to viewerHtmlHandler ViewerImageHandler imageHandler = (ViewerImageHandler)handler; //Get document pages in image form List <PageImage> Images = imageHandler.GetPages(guid, options); foreach (PageImage image in Images) { //Save each image at disk Utilities.SaveAsImage(image.PageNumber + "_" + DocumentName, image.Stream); } //ExEnd:RenderAsImageWithRotationTransformation }
/// <summary> /// Get document Image representation /// </summary> public static void Get_document_Image_representation() { Console.WriteLine("***** {0} *****", "Get document image representation"); /* ********************* SAMPLE ********************* */ // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); string guid = "word.doc"; List<PageImage> pages = imageHandler.GetPages(guid); foreach (PageImage page in pages) { Console.WriteLine("Page number: {0}", page.PageNumber); // Page image stream Stream imageContent = page.Stream; } }
public static void InImageRepresentation() { // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); string guid = "document.xlsx"; // Set image options to show grid lines ImageOptions options = new ImageOptions(); options.CellsOptions.ShowGridLines = true; List<PageImage> pages = imageHandler.GetPages(guid, options); foreach (PageImage page in pages) { Console.WriteLine("Page number: {0}", page.PageNumber); // Page image stream Stream imageContent = page.Stream; } }
public HttpResponseMessage LoadDocumentPage(PostedDataWrapper postedData) { try { // get/set parameters string documentGuid = postedData.guid; int pageNumber = postedData.page; bool htmlMode = postedData.htmlMode; string password = postedData.password; LoadedPageWrapper loadedPage = new LoadedPageWrapper(); string angle = "0"; // set options if (htmlMode) { HtmlOptions htmlOptions = new HtmlOptions(); htmlOptions.PageNumber = pageNumber; htmlOptions.CountPagesToRender = 1; htmlOptions.IsResourcesEmbedded = true; // set password for protected document if (!String.IsNullOrEmpty(password)) { htmlOptions.Password = password; } // get page HTML loadedPage.pageHtml = viewerHtmlHandler.GetPages(documentGuid, htmlOptions)[0].HtmlContent; // get page rotation angle angle = viewerHtmlHandler.GetDocumentInfo(documentGuid).Pages[pageNumber - 1].Angle.ToString(); } else { ImageOptions imageOptions = new ImageOptions(); imageOptions.PageNumber = pageNumber; imageOptions.CountPagesToRender = 1; // set password for protected document if (!String.IsNullOrEmpty(password)) { imageOptions.Password = password; } byte[] bytes; using (var memoryStream = new MemoryStream()) { viewerImageHandler.GetPages(documentGuid, imageOptions)[0].Stream.CopyTo(memoryStream); bytes = memoryStream.ToArray(); } string incodedImage = Convert.ToBase64String(bytes); loadedPage.pageImage = incodedImage; // get page rotation angle angle = viewerImageHandler.GetDocumentInfo(documentGuid).Pages[pageNumber - 1].Angle.ToString(); } loadedPage.angle = angle; // return loaded page object return(Request.CreateResponse(HttpStatusCode.OK, loadedPage)); } catch (Exception ex) { // set exception message ErrorMsgWrapper errorMsgWrapper = new ErrorMsgWrapper(); errorMsgWrapper.message = ex.Message; errorMsgWrapper.exception = ex; return(Request.CreateResponse(HttpStatusCode.OK, errorMsgWrapper)); } }
public static GetImageUrlsResponse GetImageUrls(GetImageUrlsParameters parameters) { var docPath = parameters.Path; if (string.IsNullOrEmpty(parameters.Path)) { var empty = new GetImageUrlsResponse { imageUrls = new string[0] }; return(empty); } DocumentInfoContainer documentInfoContainer = _imageHandler.GetDocumentInfo(parameters.Path); int[] pageNumbers = new int[documentInfoContainer.Pages.Count]; for (int i = 0; i < documentInfoContainer.Pages.Count; i++) { pageNumbers[i] = documentInfoContainer.Pages[i].Number; } var applicationHost = GetApplicationHost(); string[] imageUrls = ImageUrlHelper.GetImageUrls(applicationHost, pageNumbers, parameters); string[] attachmentUrls = new string[0]; foreach (AttachmentBase attachment in documentInfoContainer.Attachments) { List <PageImage> pages = _imageHandler.GetPages(attachment); var attachmentInfo = _imageHandler.GetDocumentInfo(_tempPath + "\\" + Path.GetFileNameWithoutExtension(docPath) + Path.GetExtension(docPath).Replace(".", "_") + "\\attachments\\" + attachment.Name); GetImageUrlsParameters attachmentResponse = parameters; attachmentResponse.Path = attachmentInfo.Guid; int[] attachmentPageNumbers = new int[pages.Count]; for (int i = 0; i < pages.Count; i++) { attachmentPageNumbers[i] = pages[i].PageNumber; } Array.Resize <string>(ref attachmentUrls, (attachmentUrls.Length + pages.Count)); string[] attachmentImagesUrls = new string[pages.Count]; attachmentImagesUrls = ImageUrlHelper.GetImageUrls(applicationHost, attachmentPageNumbers, attachmentResponse); attachmentImagesUrls.CopyTo(attachmentUrls, (attachmentUrls.Length - pages.Count)); } if (documentInfoContainer.Attachments.Count > 0) { var imagesUrls = new string[attachmentUrls.Length + imageUrls.Length]; imageUrls.CopyTo(imagesUrls, 0); attachmentUrls.CopyTo(imagesUrls, imageUrls.Length); var result = new GetImageUrlsResponse { imageUrls = imagesUrls }; return(result); } else { var result = new GetImageUrlsResponse { imageUrls = imageUrls }; return(result); } }
public ActionResult Get(int?width, int?height, string file, int page, string watermarkText, int?watermarkColor, WatermarkPosition?watermarkPosition, int?watermarkWidth, byte watermarkOpacity, int?rotate, int?zoom) { if (Utils.IsValidUrl(file)) { file = Utils.DownloadToStorage(file); } ViewerImageHandler handler = Utils.CreateViewerImageHandler(); if (rotate.HasValue) { if (rotate.Value > 0) { handler.ClearCache(file); } } ImageOptions options = new ImageOptions(); options.PageNumbersToRender = new List <int>(new int[] { page }); options.PageNumber = page; options.CountPagesToRender = 1; if (Path.GetExtension(file).ToLower().StartsWith(".xls")) { options.CellsOptions.OnePagePerSheet = false; options.CellsOptions.CountRowsPerPage = 150; } if (watermarkText != "") { options.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity); } if (width.HasValue) { int w = Convert.ToInt32(width); if (zoom.HasValue) { w = w + zoom.Value; } options.Width = w; } if (height.HasValue) { if (zoom.HasValue) { options.Height = options.Height + zoom.Value; } } if (rotate.HasValue) { if (rotate.Value > 0) { if (width.HasValue) { int side = options.Width; DocumentInfoContainer documentInfoContainer = handler.GetDocumentInfo(file); int pageAngle = documentInfoContainer.Pages[page - 1].Angle; if (pageAngle == 90 || pageAngle == 270) { options.Height = side; } else { options.Width = side; } } options.Transformations = Transformation.Rotate; handler.RotatePage(file, new RotatePageOptions(page, rotate.Value)); } } else { options.Transformations = Transformation.None; handler.RotatePage(file, new RotatePageOptions(page, 0)); } using (new InterProcessLock(file)) { List <PageImage> list = handler.GetPages(file, options); PageImage pageImage = list.Single(_ => _.PageNumber == page); return(File(pageImage.Stream, "image/png")); } }
public ActionResult ViewDocument(ViewDocumentParameters request) { var fileName = Path.GetFileName(request.Path); var result = new ViewDocumentResponse { pageCss = new string[] {}, lic = true, pdfDownloadUrl = GetPdfDownloadUrl(request), pdfPrintUrl = GetPdfPrintUrl(request), url = GetFileUrl(request), path = request.Path, name = fileName }; var docInfo = _imageHandler.GetDocumentInfo(new DocumentInfoOptions(request.Path)); result.documentDescription = new FileDataJsonSerializer(docInfo.Pages, new FileDataOptions()).Serialize(true); result.docType = docInfo.DocumentType; result.fileType = docInfo.FileType; var imageOptions = new ImageOptions { Watermark = GetWatermark(request) }; var imagePages = _imageHandler.GetPages(request.Path, imageOptions); // Provide images urls var urls = new List <string>(); // If no cache - save images to temp folder //var tempFolderPath = Path.Combine(Microsoft.SqlServer.Server.MapPath("~"), "Content", "TempStorage"); var tempFolderPath = Path.Combine(HttpContext.Server.MapPath("~"), "Content", "TempStorage"); foreach (var pageImage in imagePages) { var docFoldePath = Path.Combine(tempFolderPath, request.Path); if (!Directory.Exists(docFoldePath)) { Directory.CreateDirectory(docFoldePath); } var pageImageName = string.Format("{0}\\{1}.png", docFoldePath, pageImage.PageNumber); using (var stream = pageImage.Stream) using (FileStream fileStream = new FileStream(pageImageName, FileMode.Create)) { stream.Seek(0, SeekOrigin.Begin); stream.CopyTo(fileStream); } var baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/"; urls.Add(string.Format("{0}Content/TempStorage/{1}/{2}.png", baseUrl, request.Path, pageImage.PageNumber)); } result.imageUrls = urls.ToArray(); var serializer = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }; var serializedData = serializer.Serialize(result); return(Content(serializedData, "application/json")); }
protected void Page_Load(object sender, EventArgs e) { var file = GetValueFromQueryString("file"); var attachment = GetValueFromQueryString("attachment"); int page = Convert.ToInt32(GetValueFromQueryString("page")); int?width = Convert.ToInt32(GetValueFromQueryString("width")); int?height = Convert.ToInt32(GetValueFromQueryString("width")); if (Utils.IsValidUrl(file)) { file = Utils.DownloadToStorage(file); } string watermarkText = GetValueFromQueryString("watermarkText"); int? watermarkColor = Convert.ToInt32(GetValueFromQueryString("watermarkColor")); WatermarkPosition watermarkPosition = (WatermarkPosition)Enum.Parse(typeof(WatermarkPosition), GetValueFromQueryString("watermarkPosition"), true); string widthFromQuery = GetValueFromQueryString("watermarkWidth"); int? watermarkWidth = GetValueFromQueryString("watermarkWidth") == "null" ? null : (int?)Convert.ToInt32(GetValueFromQueryString("watermarkWidth")); byte watermarkOpacity = Convert.ToByte(GetValueFromQueryString("watermarkOpacity")); ViewerImageHandler handler = Utils.CreateViewerImageHandler(); ImageOptions o = new ImageOptions(); List <int> pageNumberstoRender = new List <int>(); pageNumberstoRender.Add(page); o.PageNumbersToRender = pageNumberstoRender; o.PageNumber = page; o.CountPagesToRender = 1; if (width.HasValue) { o.Width = Convert.ToInt32(width); } if (height.HasValue) { o.Height = Convert.ToInt32(height); } if (watermarkText != "") { o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity); } Stream stream = null; DocumentInfoContainer info = handler.GetDocumentInfo(file); // Iterate over the attachments collection foreach (AttachmentBase attachmentBase in info.Attachments.Where(x => x.Name == attachment)) { List <PageImage> pages = handler.GetPages(attachmentBase); foreach (PageImage attachmentPage in pages.Where(x => x.PageNumber == page)) { stream = attachmentPage.Stream; } ; } var result = new HttpResponseMessage(HttpStatusCode.OK); System.Drawing.Image image = System.Drawing.Image.FromStream(stream); MemoryStream memoryStream = new MemoryStream(); image.Save(memoryStream, ImageFormat.Jpeg); HttpContext.Current.Response.ContentType = "image/jpeg"; memoryStream.WriteTo(HttpContext.Current.Response.OutputStream); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); }
/// <summary> /// Get document representation from Stream /// </summary> public static void Get_pages_from_stream() { Console.WriteLine("***** {0} *****", "Get document representation from Stream"); /* ********************* SAMPLE ********************* */ // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); using (FileStream fileStream = new FileStream(@"C:\storage\word.doc", FileMode.Open, FileAccess.Read)) { // Get pages by absolute path List<PageImage> pages = imageHandler.GetPages(fileStream, "word.doc"); Console.WriteLine("Page count: {0}", pages.Count); } }
protected void Page_Load(object sender, EventArgs e) { _config = new ViewerConfig { StoragePath = _storagePath, UseCache = true }; ViewerImageHandler imageHandler = (ViewerImageHandler)HttpContext.Current.Session["imageHandler"]; ViewerHtmlHandler htmlHandler = (ViewerHtmlHandler)HttpContext.Current.Session["htmlHandler"]; GetDocumentPageImageParameters parameters = new GetDocumentPageImageParameters(); foreach (String key in Request.QueryString.AllKeys) { if (!string.IsNullOrEmpty(Request.QueryString[key])) { var propertyInfo = parameters.GetType().GetProperty(key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); propertyInfo.SetValue(parameters, ChangeType(Request.QueryString[key], propertyInfo.PropertyType), null); } } string guid = parameters.Path; int pageIndex = parameters.PageIndex; int pageNumber = pageIndex + 1; var displayName = parameters.Path; /* * //NOTE: This feature is supported starting from version 3.2.0 * CultureInfo cultureInfo = string.IsNullOrEmpty(parameters.Locale) * ? new CultureInfo("en-Us") * : new CultureInfo(parameters.Locale); * * ViewerImageHandler viewerImageHandler = new ViewerImageHandler(viewerConfig, cultureInfo); */ var imageOptions = new ImageOptions { //ConvertImageFileType = _convertImageFileType, ConvertImageFileType = ConvertImageFileType.JPG, JpegQuality = 100, Watermark = Utils.GetWatermark(parameters.WatermarkText, parameters.WatermarkColor, parameters.WatermarkPosition, parameters.WatermarkWidth), Transformations = parameters.Rotate ? Transformation.Rotate : Transformation.None }; if (parameters.Rotate && parameters.Width.HasValue) { DocumentInfoContainer documentInfoContainer = imageHandler.GetDocumentInfo(guid); int side = parameters.Width.Value; int pageAngle = documentInfoContainer.Pages[pageIndex].Angle; if (pageAngle == 90 || pageAngle == 270) { imageOptions.Height = side; } else { imageOptions.Width = side; } } /* * //NOTE: This feature is supported starting from version 3.2.0 * if (parameters.Quality.HasValue) * imageOptions.JpegQuality = parameters.Quality.Value; */ using (new InterProcessLock(guid)) { List <PageImage> pageImages = imageHandler.GetPages(guid, imageOptions); PageImage pageImage = pageImages.Single(_ => _.PageNumber == pageNumber); var fileStream = pageImage.Stream; // return File(pageImage.Stream, GetContentType(_convertImageFileType)); byte[] Bytes = new byte[fileStream.Length]; fileStream.Read(Bytes, 0, Bytes.Length); string contentDispositionString = "attachment; filename=\"" + displayName + "\""; contentDispositionString = new ContentDisposition { FileName = displayName, Inline = true }.ToString(); HttpContext.Current.Response.ContentType = "image/jpeg"; HttpContext.Current.Response.AddHeader("Content-Disposition", contentDispositionString); HttpContext.Current.Response.AddHeader("Content-Length", fileStream.Length.ToString()); try { HttpContext.Current.Response.OutputStream.Write(Bytes, 0, Bytes.Length); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } catch (HttpException x) { // Ignore it. } } }
public HttpResponseMessage Get(int?width, string file, int page, string watermarkText, int?watermarkColor, WatermarkPosition?watermarkPosition, int?watermarkWidth, byte watermarkOpacity, int?rotate, int?zoom, int?height = null) { if (Utils.IsValidUrl(file)) { file = Utils.DownloadToStorage(file); } ViewerImageHandler handler = Utils.CreateViewerImageHandler(); if (rotate.HasValue) { if (rotate.Value > 0) { handler.ClearCache(file); } } ImageOptions options = new ImageOptions(); options.PageNumbersToRender = new List <int>(new int[] { page }); options.PageNumber = page; options.CountPagesToRender = 1; if (Path.GetExtension(file).ToLower().StartsWith(".xls")) { options.CellsOptions.OnePagePerSheet = false; options.CellsOptions.CountRowsPerPage = 150; } if (watermarkText != "") { options.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity); } if (width.HasValue) { int w = Convert.ToInt32(width); if (zoom.HasValue) { w = w + zoom.Value; } options.Width = w; } if (height.HasValue) { if (zoom.HasValue) { options.Height = options.Height + zoom.Value; } } if (rotate.HasValue) { if (rotate.Value > 0) { if (width.HasValue) { int side = options.Width; DocumentInfoContainer documentInfoContainer = handler.GetDocumentInfo(file); int pageAngle = documentInfoContainer.Pages[page - 1].Angle; if (pageAngle == 90 || pageAngle == 270) { options.Height = side; } else { options.Width = side; } } options.Transformations = Transformation.Rotate; handler.RotatePage(file, new RotatePageOptions(page, rotate.Value)); } } else { options.Transformations = Transformation.None; handler.RotatePage(file, new RotatePageOptions(page, 0)); } List <PageImage> list = handler.GetPages(file, options); PageImage pageImage = list.Single(_ => _.PageNumber == page); Stream stream = pageImage.Stream; var result = new HttpResponseMessage(HttpStatusCode.OK); Image image = Image.FromStream(stream); MemoryStream memoryStream = new MemoryStream(); image.Save(memoryStream, ImageFormat.Jpeg); result.Content = new ByteArrayContent(memoryStream.ToArray()); result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); return(result); }
/// <summary> /// Rotate page in Image mode /// </summary> public static void Rotate_page_in_Image_mode() { Console.WriteLine("***** {0} *****", "Rotate page in Image mode"); /* ********************* SAMPLE ********************* */ string licensePath = @"D:\GroupDocs.Viewer.lic"; // Setup license GroupDocs.Viewer.License lic = new GroupDocs.Viewer.License(); lic.SetLicense(licensePath); /* ******************** SAMPLE BEGIN *********************** */ /* ******************** Rotate 1st page of the document by 90 deg *********************** */ // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create image handler ViewerImageHandler imageHandler = new ViewerImageHandler(config); string guid = "word.doc"; // Set rotation angle 90 for page number 1 RotatePageOptions rotateOptions = new RotatePageOptions(guid, 1, 90); // Perform page rotation imageHandler.RotatePage(rotateOptions); /* ******************** Retrieve all document pages including transformation *********************** */ // Set image options to include rotate transformations ImageOptions imageOptions = new ImageOptions { Transformations = Transformation.Rotate }; // Get image representation of all document pages, including rotate transformations List<PageImage> pages = imageHandler.GetPages(guid, imageOptions); /* ******************** Retrieve all document pages excluding transformation *********************** */ // Set image options NOT to include ANY transformations ImageOptions noTransformationsOptions = new ImageOptions { Transformations = Transformation.None // This is by default }; // Get image representation of all document pages, without transformations List<PageImage> pagesWithoutTransformations = imageHandler.GetPages(guid, noTransformationsOptions); // Get image representation of all document pages, without transformations List<PageImage> pagesWithoutTransformations2 = imageHandler.GetPages(guid); /********************* SAMPLE END *************************/ //foreach (PageImage page in pages) //{ // // Page number // Console.WriteLine("Page number: {0}", page.PageNumber); // // Page image stream // Stream imageContent = page.Stream; // using ( // FileStream file = new FileStream(string.Format(@"C:\{0}.png", page.PageNumber), // FileMode.Create, FileAccess.ReadWrite)) // { // MemoryStream xxx = new MemoryStream(); // page.Stream.CopyTo(xxx); // var arr = xxx.ToArray(); // file.Write(arr, 0, arr.Length); // } //} }