protected Stream GetPreviewImagesExcelStream(Content content, IEnumerable <SNCR.Image> previewImages, RestrictionType?restrictionType = null) { CheckLicense(LicenseProvider.Cells); try { var ms = new MemoryStream(); var oldExcel = content.Name.ToLower().EndsWith(".xls"); var fileFormat = oldExcel ? AsposeCells.FileFormatType.Excel97To2003 : AsposeCells.FileFormatType.Xlsx; var saveFormat = oldExcel ? AsposeCells.SaveFormat.Excel97To2003 : AsposeCells.SaveFormat.Xlsx; var document = new AsposeCells.Workbook(fileFormat); var index = 1; var imageOptions = new PreviewImageOptions() { RestrictionType = restrictionType }; foreach (var previewImage in previewImages.Where(previewImage => previewImage != null)) { using (var imgStream = GetRestrictedImage(previewImage, imageOptions)) { var image = System.Drawing.Image.FromStream(imgStream); var imageForDocument = ResizeImage(image, Math.Min(image.Width, PREVIEW_EXCEL_WIDTH), Math.Min(image.Height, PREVIEW_EXCEL_HEIGHT)); if (imageForDocument != null) { using (var imageStream = new MemoryStream()) { imageForDocument.Save(imageStream, Common.PREVIEWIMAGEFORMAT); try { var ws = index == 1 ? document.Worksheets[0] : document.Worksheets.Add("Sheet" + index); ws.Pictures.Add(0, 0, imageStream); } catch (IndexOutOfRangeException ex) { SnLog.WriteException(ex, "Error during document generation. Path: " + previewImage.Path); break; } } } } index++; } document.Save(ms, saveFormat); ms.Seek(0, SeekOrigin.Begin); return(ms); } catch (Exception ex) { SnLog.WriteException(ex); } return(null); }
protected Stream GetPreviewImagesPowerPointStream(Content content, IEnumerable <SNCR.Image> previewImages, RestrictionType?restrictionType = null) { CheckLicense(LicenseProvider.Slides); try { var ms = new MemoryStream(); var extension = ContentNamingProvider.GetFileExtension(content.Name).ToLower(); var oldPpt = Common.PRESENTATION_EXTENSIONS.Contains(extension); var saveFormat = oldPpt ? AsposeSlides.Export.SaveFormat.Ppt : AsposeSlides.Export.SaveFormat.Pptx; var docPresentation = new AsposeSlides.Presentation(); var index = 1; var imageOptions = new PreviewImageOptions() { RestrictionType = restrictionType }; foreach (var previewImage in previewImages.Where(previewImage => previewImage != null)) { using (var imgStream = GetRestrictedImage(previewImage, imageOptions)) { var image = System.Drawing.Image.FromStream(imgStream); var imageForDocument = ResizeImage(image, Math.Min(image.Width, Common.PREVIEW_POWERPOINT_WIDTH), Math.Min(image.Height, Common.PREVIEW_POWERPOINT_HEIGHT)); if (imageForDocument != null) { try { var img = docPresentation.Images.AddImage(imageForDocument); var slide = docPresentation.Slides[0]; if (index > 1) { docPresentation.Slides.AddClone(slide); slide = docPresentation.Slides[index - 1]; } slide.Shapes.AddPictureFrame(AsposeSlides.ShapeType.Rectangle, 10, 10, imageForDocument.Width, imageForDocument.Height, img); } catch (IndexOutOfRangeException ex) { SnLog.WriteException(ex, "Error during document generation. Path: " + previewImage.Path); break; } } } index++; } docPresentation.Save(ms, saveFormat); ms.Seek(0, SeekOrigin.Begin); return(ms); } catch (Exception ex) { SnLog.WriteException(ex); } return(null); }
// ===================================================================================================== Generate documents protected Stream GetPreviewImagesPdfStream(Content content, IEnumerable <SNCR.Image> previewImages, RestrictionType?restrictionType = null) { CheckLicense(LicenseProvider.Pdf); try { var ms = new MemoryStream(); using var document = new AsposePdf.Document(); var index = 1; var pageAttributes = GetPageAttributes(content); var imageOptions = new PreviewImageOptions() { RestrictionType = restrictionType }; foreach (var previewImage in previewImages.Where(previewImage => previewImage != null)) { // use the stored rotation value for this page if exists int?rotation = pageAttributes.ContainsKey(previewImage.Index) ? pageAttributes[previewImage.Index].degree : null; imageOptions.Rotation = rotation; using (var imgStream = GetRestrictedImage(previewImage, imageOptions)) { int newWidth; int newHeight; // Compute dimensions using a SQUARE (max width and height are equal). ComputeResizedDimensionsWithRotation(previewImage, PREVIEW_PDF_HEIGHT, rotation, out newWidth, out newHeight); var imageStamp = new AsposePdf.ImageStamp(imgStream) { TopMargin = 10, HorizontalAlignment = AsposePdf.HorizontalAlignment.Center, VerticalAlignment = AsposePdf.VerticalAlignment.Top, Width = newWidth, Height = newHeight }; try { var page = index == 1 ? document.Pages[1] : document.Pages.Add(); // If the final image is landscape, we need to rotate the page // to fit the image. It does not matter if the original image // was landscape or the rotation made it that way. if (newWidth > newHeight) { page.Rotate = AsposePdf.Rotation.on90; } page.AddStamp(imageStamp); } catch (IndexOutOfRangeException ex) { SnLog.WriteException(ex, "Error during pdf generation. Path: " + previewImage.Path); break; } } index++; } document.Save(ms); ms.Seek(0, SeekOrigin.Begin); return(ms); } catch (Exception ex) { SnLog.WriteException(ex); } return(null); }
protected Stream GetPreviewImagesWordStream(Content content, IEnumerable <SNCR.Image> previewImages, RestrictionType?restrictionType = null) { CheckLicense(LicenseProvider.Words); try { var ms = new MemoryStream(); var document = new AsposeWords.Document(); var builder = new AsposeWords.DocumentBuilder(document); var index = 1; var saveFormat = content.Name.ToLower().EndsWith(".docx") ? AsposeWords.SaveFormat.Docx : AsposeWords.SaveFormat.Doc; var pageAttributes = GetPageAttributes(content); var imageOptions = new PreviewImageOptions() { RestrictionType = restrictionType }; foreach (var previewImage in previewImages.Where(previewImage => previewImage != null)) { // use the stored rotation value for this page if exists int?rotation = pageAttributes.ContainsKey(previewImage.Index) ? pageAttributes[previewImage.Index].degree : null; imageOptions.Rotation = rotation; using (var imgStream = GetRestrictedImage(previewImage, imageOptions)) { int newWidth; int newHeight; // Compute dimensions using a SQUARE (max width and height are equal). ComputeResizedDimensionsWithRotation(previewImage, PREVIEW_WORD_HEIGHT, rotation, out newWidth, out newHeight); try { // skip to the next page if (index > 1) { builder.Writeln(""); } // If the final image is landscape, we need to rotate the page // to fit the image. It does not matter if the original image // was landscape or the rotation made it that way. // Switch orientation only if needed. if (newWidth > newHeight) { if (builder.PageSetup.Orientation != AsposeWords.Orientation.Landscape) { if (index > 1) { builder.InsertBreak(AsposeWords.BreakType.SectionBreakContinuous); } builder.PageSetup.Orientation = AsposeWords.Orientation.Landscape; } } else { if (builder.PageSetup.Orientation != AsposeWords.Orientation.Portrait) { if (index > 1) { builder.InsertBreak(AsposeWords.BreakType.SectionBreakContinuous); } builder.PageSetup.Orientation = AsposeWords.Orientation.Portrait; } } builder.InsertImage(imgStream, RelativeHorizontalPosition.LeftMargin, -5, RelativeVerticalPosition.TopMargin, -50, newWidth, newHeight, WrapType.Square); } catch (IndexOutOfRangeException ex) { SnLog.WriteException(ex, "Error during document generation. Path: " + previewImage.Path); break; } } index++; } document.Save(ms, saveFormat); ms.Seek(0, SeekOrigin.Begin); return(ms); } catch (Exception ex) { SnLog.WriteException(ex); } return(null); }
/// <summary> /// Gets a stream containing the image binary in the specified property (or the default Binary field). /// Image dimensions can be determined by providing the width and height values among the parameters. /// The image is also redacted if the user does not have enough permissions to see the full image /// in case of preview images, only restricted ones. /// </summary> /// <param name="propertyName">Name of the binary property to serve, default is Binary.</param> /// <param name="parameters">Optional parameters that may include image width and height.</param> /// <param name="contentType">Out parameter, filled with the binary content type to be served to the client.</param> /// <param name="fileName">Out parameter, filled with the file name to serve to the client.</param> public Stream GetImageStream(string propertyName, IDictionary <string, object> parameters, out string contentType, out BinaryFileName fileName) { Stream imageStream; object widthParam = null; object heightParam = null; object watermarkParam = null; parameters?.TryGetValue("width", out widthParam); parameters?.TryGetValue("height", out heightParam); parameters?.TryGetValue("watermark", out watermarkParam); var binaryData = !string.IsNullOrEmpty(propertyName) ? GetBinary(propertyName) : Binary; contentType = binaryData?.ContentType ?? string.Empty; fileName = string.IsNullOrEmpty(propertyName) || string.CompareOrdinal(propertyName, "Binary") == 0 ? new BinaryFileName(Name) : binaryData?.FileName ?? string.Empty; if (DocumentPreviewProvider.Current != null && DocumentPreviewProvider.Current.IsPreviewOrThumbnailImage(NodeHead.Get(Id))) { var options = new PreviewImageOptions { BinaryFieldName = propertyName }; if (watermarkParam != null && ((int.TryParse((string)watermarkParam, out var wmValue) && wmValue == 1) || (bool.TryParse((string)watermarkParam, out var wmBool) && wmBool))) { options.RestrictionType = RestrictionType.Watermark; } // get preview image with watermark or redaction if necessary imageStream = DocumentPreviewProvider.Current.GetRestrictedImage(this, options); } else { imageStream = binaryData?.GetStream(); } if (imageStream == null) { return(new MemoryStream()); } imageStream.Position = 0; int ConvertImageParameter(object param) { if (param != null) { // we recognize int and string values as well switch (param) { case int i: return(i); case string s when int.TryParse(s, out var pint): return(pint); } } return(200); } // no resize parameters: return the original stream if (widthParam == null || heightParam == null) { return(imageStream); } var width = ConvertImageParameter(widthParam); var height = ConvertImageParameter(heightParam); // compute a new, resized stream on-the-fly var resizedStream = ImageResizer.CreateResizedImageFile(imageStream, width, height, 80, getImageFormat(contentType)); // in case the method created a new stream, we have to close the original to prevent memory leak if (!ReferenceEquals(resizedStream, imageStream)) { imageStream.Dispose(); } return(resizedStream); }