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 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"); } }