public static IList <FileUploadResultInfo> ConvertPdfToImage(string startupPath, string fileName) { string inputFile = Path.Combine(Directory.GetCurrentDirectory(), startupPath, fileName); FileStream fileStream = new FileStream(inputFile, FileMode.Open); PdfRenderer pdfExportImage = new PdfRenderer(); //Loads the PDF document pdfExportImage.Load(fileStream, new Dictionary <string, string>()); //Exports the PDF document pages into images Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, pdfExportImage.PageCount - 1); var listResult = new List <FileUploadResultInfo>(); for (int i = 0; i < pdfExportImage.PageCount; i++) { var fileNameImage = string.Format("{1}_page_{0}", i, fileName.Split('.')[0]) + ".jpg"; var pathImage = Path.Combine(Directory.GetCurrentDirectory(), startupPath, fileNameImage); bitmapimage[i].Save(pathImage); listResult.Add(new FileUploadResultInfo() { FileName = fileNameImage, FilePath = Path.Combine(startupPath, fileNameImage) }); } return(listResult); }
public PreviewFileResponse Preview(Stream stream, int pageIndex) { _logger.LogDebug($"Create first page preview from file"); using (MemoryStream streamImage = new MemoryStream()) using (PdfRenderer renderer = new PdfRenderer()) { renderer.Load(stream); using (Bitmap bitmapimage = renderer.ExportAsImage(pageIndex)) { bitmapimage.Save(streamImage, ImageFormat.Jpeg); PreviewFileResponse previewResult = new PreviewFileResponse { Image = streamImage.ToArray() }; _logger.LogDebug($"FileInfoPDFService.Preview....OK"); return(previewResult); } } }
private List <string> ConvertToImages(FileStream fs, List <string> returnStrings, Syncfusion.DocIO.FormatType type) { DocIO.WordDocument wd = new DocIO.WordDocument(fs, type); //Instantiation of DocIORenderer for Word to PDF conversion DocIORenderer render = new DocIORenderer(); //Converts Word document into PDF document PdfDocument pdfDocument = render.ConvertToPDF(wd); //Releases all resources used by the Word document and DocIO Renderer objects render.Dispose(); wd.Dispose(); //Saves the PDF file MemoryStream outputStream = new MemoryStream(); pdfDocument.Save(outputStream); outputStream.Position = 0; //Closes the instance of PDF document object pdfDocument.Close(); PdfRenderer pdfExportImage = new PdfRenderer(); //Loads the PDF document pdfExportImage.Load(outputStream); //Exports the PDF document pages into images Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, pdfExportImage.PageCount - 1); foreach (Bitmap bitmap in bitmapimage) { using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); returnStrings.Add("data:image/png;base64," + Convert.ToBase64String(ms.ToArray())); } } return(returnStrings); }
public static void ConvertDocToImage(string startupPath, string fileName) { string inputFile = Path.Combine(Directory.GetCurrentDirectory(), startupPath, fileName); FileStream fileStream = new FileStream(inputFile, FileMode.Open); //Loads file stream into Word document WordDocument document = new WordDocument(fileStream, FormatType.Docx); fileStream.Dispose(); //Instantiation of DocIORenderer for Word to PDF conversion DocIORenderer render = new DocIORenderer(); //Converts Word document into PDF document PdfDocument pdfDocument = render.ConvertToPDF(document); //Releases all resources used by the Word document and DocIO Renderer objects render.Dispose(); document.Dispose(); //Saves the PDF file MemoryStream outputStream = new MemoryStream(); pdfDocument.Save(outputStream); outputStream.Position = 0; //Closes the instance of PDF document object pdfDocument.Close(); PdfRenderer pdfExportImage = new PdfRenderer(); //Loads the PDF document pdfExportImage.Load(outputStream, new Dictionary <string, string>()); //Exports the PDF document pages into images Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, pdfExportImage.PageCount - 1); for (int i = 0; i < pdfExportImage.PageCount; i++) { bitmapimage[i].Save(Path.Combine(Directory.GetCurrentDirectory(), startupPath, string.Format("{1}_page_{0}", i, fileName.Split('.')[0]) + ".png")); } }
public IActionResult GetPreviewImage(FileManagerFilterContent args) { string baseFolder = this.basePath + "\\wwwroot\\SharedFiles"; try { String fullPath = baseFolder + args.Path; string extension = Path.GetExtension(fullPath); Stream imageStream = null; if (extension == Constants.Pdf) { FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read); PdfRenderer pdfExportImage = new PdfRenderer(); //Loads the PDF document pdfExportImage.Load(fileStream); //Exports the PDF document pages into images Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, 0); imageStream = new MemoryStream(); bitmapimage[0].Save(imageStream, System.Drawing.Imaging.ImageFormat.Png); imageStream.Position = 0; pdfExportImage.Dispose(); fileStream.Close(); } else if (extension == Constants.Docx || extension == Constants.Rtf || extension == Constants.Doc) { FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read); //Loads file stream into Word document WordDocument document = new WordDocument(fileStream, Syncfusion.DocIO.FormatType.Automatic); fileStream.Dispose(); //Instantiation of DocIORenderer for Word to PDF conversion DocIORenderer render = new DocIORenderer(); //Converts Word document into PDF document PdfDocument pdfDocument = render.ConvertToPDF(document); //Releases all resources used by the Word document and DocIO Renderer objects render.Dispose(); document.Dispose(); //Saves the PDF file MemoryStream outputStream = new MemoryStream(); pdfDocument.Save(outputStream); outputStream.Position = 0; //Closes the instance of PDF document object pdfDocument.Close(); PdfRenderer pdfExportImage = new PdfRenderer(); //Loads the PDF document pdfExportImage.Load(outputStream); //Exports the PDF document pages into images Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, 0); imageStream = new MemoryStream(); bitmapimage[0].Save(imageStream, System.Drawing.Imaging.ImageFormat.Png); imageStream.Position = 0; fileStream.Close(); } else if (extension == Constants.Pptx) { IPresentation presentation = Presentation.Open(fullPath); //Initialize PresentationRenderer for image conversion presentation.PresentationRenderer = new PresentationRenderer(); //Convert the first slide to image imageStream = presentation.Slides[0].ConvertToImage(ExportImageFormat.Png); presentation.Dispose(); } FileStreamResult fileStreamResult = new FileStreamResult(imageStream, "APPLICATION/octet-stream"); return(fileStreamResult); } catch { return(null); } }
public string GetPreview([FromBody] FileManagerDirectoryContent args) { string baseFolder = this.basePath + "\\wwwroot\\Files"; try { String fullPath = baseFolder + args.Path; string extension = Path.GetExtension(fullPath); Stream imageStream = null; if (extension == Constants.Pdf) { try { FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read); PdfRenderer pdfExportImage = new PdfRenderer(); //Loads the PDF document pdfExportImage.Load(fileStream); //Exports the PDF document pages into images Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, 0); imageStream = new MemoryStream(); bitmapimage[0].Save(imageStream, System.Drawing.Imaging.ImageFormat.Png); imageStream.Position = 0; pdfExportImage.Dispose(); fileStream.Close(); } catch { imageStream = null; } } else if (extension == Constants.Docx || extension == Constants.Rtf || extension == Constants.Doc || extension == Constants.Txt) { try { FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read); //Loads file stream into Word document DocIO.WordDocument document = new DocIO.WordDocument(fileStream, GetDocIOFormatType(extension)); fileStream.Dispose(); //Instantiation of DocIORenderer for Word to PDF conversion DocIORenderer render = new DocIORenderer(); //Converts Word document into PDF document PdfDocument pdfDocument = render.ConvertToPDF(document); //Releases all resources used by the Word document and DocIO Renderer objects render.Dispose(); document.Dispose(); //Saves the PDF file MemoryStream outputStream = new MemoryStream(); pdfDocument.Save(outputStream); outputStream.Position = 0; //Closes the instance of PDF document object pdfDocument.Close(); PdfRenderer pdfExportImage = new PdfRenderer(); //Loads the PDF document pdfExportImage.Load(outputStream); //Exports the PDF document pages into images Bitmap[] bitmapimage = pdfExportImage.ExportAsImage(0, 0); imageStream = new MemoryStream(); bitmapimage[0].Save(imageStream, System.Drawing.Imaging.ImageFormat.Png); imageStream.Position = 0; fileStream.Close(); } catch { imageStream = null; } } else if (extension == Constants.Pptx) { try { IPresentation presentation = Presentation.Open(fullPath); //Initialize PresentationRenderer for image conversion presentation.PresentationRenderer = new PresentationRenderer(); //Convert the first slide to image imageStream = presentation.Slides[0].ConvertToImage(ExportImageFormat.Png); presentation.Dispose(); } catch { imageStream = null; } } if (imageStream != null) { byte[] bytes = new byte[imageStream.Length]; imageStream.Read(bytes); string base64 = Convert.ToBase64String(bytes); return("data:image/png;base64, " + base64); } else { return("Error"); } } catch { return("Error"); } }