public ActionResult Convert(DemoModel model) { PedamorfResponse response = null; try { using (PedamorfServiceClient client = PedamorfServiceManager.GetClient(model.ServiceHost)) { if (!string.IsNullOrEmpty(model.UrlButton)) { response = client.ConvertUrl(model.Url, new HtmlConversionOptions() { Orientation = model.Landscape ? PageOrientation.Landscape : PageOrientation.Portrait }); } else if (!string.IsNullOrEmpty(model.HtmlButton)) { response = client.ConvertHtml(model.Html); } else if (!string.IsNullOrEmpty(model.DocumentButton) && model.Document1.ContentLength > 0) { response = client.ConvertFiles(model.Document1.InputStream, model.Document1.FileName, model.Document2.InputStream, model.Document2.FileName); } else if (!string.IsNullOrEmpty(model.ImageButton) && model.Image.ContentLength > 0) { ImageConversionOptions options = new ImageConversionOptions(); int imageWidth; int imageHeight; if (int.TryParse(model.ImageWidth, out imageWidth)) { options.ImageWidthPixelsMin = imageWidth; } if (int.TryParse(model.ImageHeight, out imageHeight)) { options.ImageHeightPixelsMin = imageHeight; } response = client.ConvertImage(model.Image.InputStream, model.Image.FileName, options); } } } catch (Exception ex) { TempData["error"] = ex.Message; return(View()); } if (!response.Error) { return(File(response.ResultPdf, "application/pdf")); } else { TempData["error"] = response.ErrorMessage; return(View()); } }
public Document(String attachmentFile, String originalName, String saveToPath, String batchNumber, long emailID, DocumentSource source) { Fields = new Dictionary <String, String>(); this.saveToPath = saveToPath; this.batchNumber = batchNumber; this.emailID = emailID; OriginalName = originalName; RelativePath = Path.GetFileName(saveToPath); AttachmentFile = attachmentFile; Extension = Path.GetExtension(attachmentFile).Trim('.', ' ').ToLower(); Source = source; ErrorHandling = new ErrorHandling(); options = new ImageConversionOptions(saveToPath, RelativePath); Complete = false; Success = false; IsBody = (source == DocumentSource.Body); ShouldRetry = true; Fields.Add("%Document.Source", source.ToString()); Fields.Add("%Document.IsBody", IsBody.ToString()); Fields.Add("%Document.Extension", Extension); Fields.Add("%Document.AttachmentFileName", OriginalName); Fields.Add("%Document.FailureReason", String.Empty); }
public List <PageInfo> Convert(String inputFile, ImageConversionOptions options) { Diagram diagram = new Diagram(inputFile); if (options.BinarisationAlgorithm == BinarisationAlgorithm.Default) { options.BinarisationAlgorithm = BinarisationAlgorithm.OtsuThreshold; } var pages = new List <PageInfo>(); var saveOptions = new ImageSaveOptions(SaveFileFormat.PNG); saveOptions.Resolution = options.Resolution; saveOptions.PageCount = 1; for (int i = 0; i < diagram.Pages.Count; i++) { saveOptions.PageIndex = i; using (MemoryStream ms = new MemoryStream()) { // Save the page to the memory stream diagram.Save(ms, saveOptions); // Set the position back to the start of the stream ms.Seek(0, SeekOrigin.Begin); // Convert the page and add it to the list pages.AddRange(ImageProcessingEngine.Instance.Convert(ms, options)); } } return(pages); }
public ActionResult Convert(DemoModel model) { PedamorfResponse response = null; try { using (PedamorfServiceClient client = PedamorfServiceManager.GetClient(model.ServiceHost)) { if (!string.IsNullOrEmpty(model.UrlButton)) { response = client.ConvertUrl(model.Url, new HtmlConversionOptions() { Orientation = model.Landscape ? PageOrientation.Landscape : PageOrientation.Portrait }); } else if (!string.IsNullOrEmpty(model.HtmlButton)) { response = client.ConvertHtml(model.Html); } else if (!string.IsNullOrEmpty(model.DocumentButton) && model.Document1.ContentLength > 0) { response = client.ConvertFiles(model.Document1.InputStream, model.Document1.FileName, model.Document2.InputStream, model.Document2.FileName); } else if (!string.IsNullOrEmpty(model.ImageButton) && model.Image.ContentLength > 0) { ImageConversionOptions options = new ImageConversionOptions(); int imageWidth; int imageHeight; if (int.TryParse(model.ImageWidth, out imageWidth)) { options.ImageWidthPixelsMin = imageWidth; } if (int.TryParse(model.ImageHeight, out imageHeight)) { options.ImageHeightPixelsMin = imageHeight; } response = client.ConvertImage(model.Image.InputStream, model.Image.FileName, options); } } } catch (Exception ex) { TempData["error"] = ex.Message; return View(); } if (!response.Error) { return File(response.ResultPdf, "application/pdf"); } else { TempData["error"] = response.ErrorMessage; return View(); } }
public List <PageInfo> Convert(String inputFile, ImageConversionOptions options) { if (options.BinarisationAlgorithm == BinarisationAlgorithm.Default) { options.BinarisationAlgorithm = BinarisationAlgorithm.OtsuThreshold; } // Create and start a new task var task = Task <List <PageInfo> > .Factory.StartNew(() => { var pages = new List <PageInfo>(); using (XpsDocument xpsDoc = new XpsDocument(inputFile, FileAccess.Read)) { FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence(); for (int i = 0; i < docSeq.DocumentPaginator.PageCount; ++i) { using (DocumentPage docPage = docSeq.DocumentPaginator.GetPage(i)) { int width = (int)(((float)options.Resolution / 96f) * docPage.Size.Width); int height = (int)(((float)options.Resolution / 96f) * docPage.Size.Height); RenderTargetBitmap renderTarget = new RenderTargetBitmap(width, height, options.Resolution, options.Resolution, PixelFormats.Default); renderTarget.Render(docPage.Visual); using (MemoryStream ms = new MemoryStream()) { BitmapEncoder encoder = new TiffBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(renderTarget)); encoder.Save(ms); ms.Seek(0, SeekOrigin.Begin); pages.AddRange(ImageProcessingEngine.Instance.Convert(ms, options)); } } } xpsDoc.Close(); } return(pages); }, CancellationToken.None, TaskCreationOptions.None, scheduler); // Wait for the task to complete task.Wait(); // Return the list of pages return(task.Result); }
public List <PageInfo> Convert(String inputFile, ImageConversionOptions options) { if (IsSpreadsheetML(inputFile)) { return(ConvertWithCells(inputFile, options)); } else { return(ConvertWithWords(inputFile, options)); } }
public List <PageInfo> Convert(String inputFile, ImageConversionOptions options) { if (options.AutoDeskew == null) { options.AutoDeskew = true; } if (options.AutoRotate == null) { options.AutoRotate = true; } return(ImageProcessingEngine.Instance.Convert(inputFile, options)); }
public List <PageInfo> Convert(String inputFile, ImageConversionOptions options) { var ext = (options.ProcessAs ?? Path.GetExtension(inputFile).Trim('.', ' ')).ToUpper(); using (MemoryStream stream = new MemoryStream()) { Presentation ppt = new Presentation(inputFile); ppt.Save(stream, SaveFormat.Pdf); // SaveFormat.PdfNotes not yet implemented stream.Seek(0, SeekOrigin.Begin); return(ImageProcessingEngine.Instance.Convert(stream, options)); } }
private List <PageInfo> ConvertWithWords(String inputFile, ImageConversionOptions options) { Document doc = new Document(inputFile); // Adjust margins foreach (Section section in doc.Sections) { section.PageSetup.LeftMargin = (section.PageSetup.LeftMargin / 2.54) / 2; section.PageSetup.RightMargin = (section.PageSetup.RightMargin / 2.54) / 2; section.PageSetup.TopMargin = (section.PageSetup.TopMargin / 2.54) / 2; section.PageSetup.BottomMargin = (section.PageSetup.BottomMargin / 2.54) / 2; } if (options.BinarisationAlgorithm == BinarisationAlgorithm.Default) { options.BinarisationAlgorithm = BinarisationAlgorithm.OtsuThreshold; } var pages = new List <PageInfo>(); var saveOptions = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Png); saveOptions.Resolution = options.Resolution; saveOptions.PageCount = 1; for (int i = 0; i < doc.PageCount; i++) { saveOptions.PageIndex = i; using (MemoryStream ms = new MemoryStream()) { // Save the page to the memory stream doc.Save(ms, saveOptions); // Set the position back to the start of the stream ms.Seek(0, SeekOrigin.Begin); // Convert the page and add it to the list pages.AddRange(ImageProcessingEngine.Instance.Convert(ms, options)); } } return(pages); }
private List <PageInfo> ConvertWithCells(String inputFile, ImageConversionOptions options) { Workbook book = new Workbook(inputFile); ImageOrPrintOptions saveOptions = new ImageOrPrintOptions(); saveOptions.HorizontalResolution = options.Resolution; saveOptions.VerticalResolution = options.Resolution; saveOptions.ImageFormat = ImageFormat.Png; //saveOptions.IsCellAutoFit = true; //saveOptions.OnePagePerSheet = true; if (options.BinarisationAlgorithm == BinarisationAlgorithm.Default) { options.BinarisationAlgorithm = BinarisationAlgorithm.OtsuThreshold; } var pages = new List <PageInfo>(); foreach (Worksheet sheet in book.Worksheets) { // Adjust the margins sheet.PageSetup.TopMargin = 0.5; sheet.PageSetup.BottomMargin = 0.5; sheet.PageSetup.LeftMargin = 0.5; sheet.PageSetup.RightMargin = 0.5; SheetRender render = new SheetRender(sheet, saveOptions); for (int i = 0; i < render.PageCount; i++) { using (var bmp = render.ToImage(i)) { // Convert the page and add it to the list pages.AddRange(ImageProcessingEngine.Instance.Convert(bmp, options)); } } } return(pages); }
public List <PageInfo> Convert(String inputFile, ImageConversionOptions options) { var ext = (options.ProcessAs ?? Path.GetExtension(inputFile).Trim('.', ' ')).ToUpper(); var isTextFile = (ext == "TXT"); // Open the document Document doc = OpenDocument(inputFile, ext); // Specific processing for MHT, HTM and HTML if (ext == "MHT" || ext == "HTM" || ext == "HTML" || isTextFile) { // Adjust Margins, Paper Size and Orientation foreach (Section section in doc.Sections) { section.PageSetup.PaperSize = PaperSize.A4; section.PageSetup.Orientation = isTextFile ? Orientation.Landscape : Orientation.Portrait; section.PageSetup.LeftMargin = (section.PageSetup.LeftMargin / 2.54) / 2; section.PageSetup.RightMargin = (section.PageSetup.RightMargin / 2.54) / 2; section.PageSetup.TopMargin = (section.PageSetup.TopMargin / 2.54) / 2; section.PageSetup.BottomMargin = (section.PageSetup.BottomMargin / 2.54) / 2; if (isTextFile) { FontChanger changer = new FontChanger("Courier New"); doc.Accept(changer); } else { // Get the portrait and landscape widths var portraitWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin; var landscapeWidth = section.PageSetup.PageHeight - section.PageSetup.TopMargin - section.PageSetup.BottomMargin; // Adjust table fit foreach (Table table in section.Body.Tables) { var width = GetTableWidth(table); if (width > portraitWidth) { if (section.PageSetup.Orientation != Orientation.Landscape) { section.PageSetup.Orientation = Orientation.Landscape; } if (width > landscapeWidth) { table.AutoFit(AutoFitBehavior.AutoFitToWindow); } } } // Get the usable width/height var usableWidth = section.PageSetup.PageWidth - section.PageSetup.LeftMargin - section.PageSetup.RightMargin; var usableHeight = section.PageSetup.PageHeight - section.PageSetup.TopMargin - section.PageSetup.BottomMargin; // Adjust image size foreach (Shape shape in section.GetChildNodes(NodeType.Shape, true)) { if (shape.HasImage && (shape.Width > usableWidth || shape.Height > usableHeight)) { // Adjust shape size var scale = Math.Min((usableWidth / shape.Width), (usableHeight / shape.Height)); shape.Width = shape.Width * scale; shape.Height = shape.Height * scale; } } } } if (!isTextFile) { // Update table layout following adjustments doc.UpdateTableLayout(); } } if (options.BinarisationAlgorithm == BinarisationAlgorithm.Default) { options.BinarisationAlgorithm = BinarisationAlgorithm.OtsuThreshold; } var pages = new List <PageInfo>(); var saveOptions = new Aspose.Words.Saving.ImageSaveOptions(SaveFormat.Png); saveOptions.Resolution = options.Resolution; saveOptions.PageCount = 1; for (int i = 0; i < doc.PageCount; i++) { saveOptions.PageIndex = i; using (MemoryStream ms = new MemoryStream()) { // Save the page to the memory stream doc.Save(ms, saveOptions); // Set the position back to the start of the stream ms.Seek(0, SeekOrigin.Begin); try { // Convert the page and add it to the list pages.AddRange(ImageProcessingEngine.Instance.Convert(ms, options)); } catch (Exception e) { if (e.Message == "Decode: Unknown or wrong format [Stream][CiImage::Open]") { // This is a known error, there is an occasional issue with the ClearImage Open method // Loading the stream into a Bitmap object works around the issue for now... using (var stream = new MemoryStream(ms.ToArray())) using (var bitmap = (Bitmap)Bitmap.FromStream(stream, true, false)) { pages.AddRange(ImageProcessingEngine.Instance.Convert(bitmap, options)); } } else { throw; } } } } return(pages); }
public PedamorfResponse ConvertImages(string directoryPath, ImageConversionOptions options) { Dictionary<string, byte[]> files = GetDirectoryFiles(directoryPath); return ConvertImages(files, options); }
public PedamorfResponse ConvertImages(Stream imageStream1, string imageName1, Stream imageStream2, string imageName2, ImageConversionOptions options) { Dictionary<string, byte[]> files = new Dictionary<string, byte[]>(); byte[] file1 = GetStreamFile(imageStream1); byte[] file2 = GetStreamFile(imageStream2); files.Add(imageName1, file1); files.Add(imageName2, file2); return ConvertImages(files, options); }
public PedamorfResponse ConvertImage(Stream fileSteam, string fileName, ImageConversionOptions options) { byte[] file = GetStreamFile(fileSteam); return ConvertImage(file, fileName, options); }
public List <PageInfo> Convert(String inputFile, ImageConversionOptions options) { var ext = (options.ProcessAs ?? Path.GetExtension(inputFile).Trim('.', ' ')).ToUpper(); Workbook book = OpenWorkbook(inputFile, ext); ImageOrPrintOptions saveOptions = new ImageOrPrintOptions(); saveOptions.HorizontalResolution = options.Resolution; saveOptions.VerticalResolution = options.Resolution; saveOptions.ImageFormat = ImageFormat.Png; //saveOptions.IsCellAutoFit = true; //saveOptions.OnePagePerSheet = true; if (options.BinarisationAlgorithm == BinarisationAlgorithm.Default) { options.BinarisationAlgorithm = BinarisationAlgorithm.OtsuThreshold; } var pages = new List <PageInfo>(); foreach (Worksheet sheet in book.Worksheets) { if (ext == "CSV" || ext == "TSV") { sheet.PageSetup.Orientation = PageOrientationType.Landscape; foreach (Cell cell in sheet.Cells) { cell.PutValue(cell.StringValue.Replace("\0", "").Trim()); } sheet.AutoFitColumns(); sheet.AutoFitRows(); } // Adjust the margins sheet.PageSetup.TopMargin = 0.5; sheet.PageSetup.BottomMargin = 0.5; sheet.PageSetup.LeftMargin = 0.5; sheet.PageSetup.RightMargin = 0.5; SheetRender render = new SheetRender(sheet, saveOptions); for (int i = 0; i < render.PageCount; i++) { using (var bmp = render.ToImage(i)) { // Convert the page and add it to the list pages.AddRange(ImageProcessingEngine.Instance.Convert(bmp, options)); } } } // TODO: DONT THROW HERE, ALLOW THIS IF REMOVE BLANK PAGES IS ENABLED // BUT WILL NEED TO BE DONE BACK IN EMAILCONVERTER if (!pages.Any()) { throw new Exception("Aspose.Cells did not find anything to print."); } return(pages); }
public PedamorfResponse ConvertImage(string filePath, ImageConversionOptions options) { byte[] file = File.ReadAllBytes(filePath); return ConvertImage(file, Path.GetFileName(filePath), options); }
private List <PageInfo> ConverByAppose(string inputFile, ImageConversionOptions options) { var pages = new List <PageInfo>(); try { using (var doc = new Aspose.Pdf.Document(inputFile)) { // Dynamic XFA to Standard AcroForm PDF conversion currently has issues so don't even attempt if (doc.Form.Type == FormType.Dynamic) { throw new Exception("PDF file in XFA format is not supported."); } // Flatten the pdf doc.Flatten(); // Setup the device for rendering the PDF pages var device = new PngDevice(new Resolution(options.Resolution)); foreach (Aspose.Pdf.Page page in doc.Pages) { using (MemoryStream ms = new MemoryStream()) { // Render the PDF device.Process(page, ms); // Set the stream position back to 0 ms.Seek(0, SeekOrigin.Begin); try { // Convert and add the image to the list of pages pages.AddRange(ImageProcessingEngine.Instance.Convert(ms, options)); } catch (Exception e) { if (e.Message == "Decode: Unknown or wrong format [Stream][CiImage::Open]") { // This is a known error, there is an occasional issue with the ClearImage Open method // Loading the stream into a Bitmap object works around the issue for now... using (var stream = new MemoryStream(ms.ToArray())) using (var bitmap = (Bitmap)Bitmap.FromStream(stream, true, false)) { pages.AddRange(ImageProcessingEngine.Instance.Convert(bitmap, options)); } } else { throw; } } } } } } catch (InvalidPasswordException) { throw; } catch { // Reset the Page Index options.PageIndex = 0; // Try to process using ClearImage pages = ImageProcessingEngine.Instance.Convert(inputFile, options); } return(pages); }