public string[] ConvertToPDF([FromBody] FileManagerDirectoryContent args)
        {
            string fileLocation = this.baseLocation + args.Path.Replace("/", "\\");

            // If document get open from zip file, we have maintained the extracted document path in TargetPath property.
            if (args.TargetPath != null)
            {
                fileLocation = args.TargetPath;
            }
            List <string> returnArray = new List <string>();

            using FileStream fs = new FileStream(fileLocation, FileMode.Open, FileAccess.Read);
            //Open the existing presentation
            IPresentation presentation = Syncfusion.Presentation.Presentation.Open(fs);
            //Convert the PowerPoint document to PDF document.
            PdfDocument pdfDocument = PresentationToPdfConverter.Convert(presentation);
            //Save the document as a stream and retrun the stream
            MemoryStream stream = new MemoryStream();

            //Save the created PowerPoint document to MemoryStream
            pdfDocument.Save(stream);
            stream.Position = 0;
            returnArray.Add("data:application/pdf;base64," + Convert.ToBase64String(stream.ToArray()));
            //Dispose the document objects.
            presentation.Dispose();
            pdfDocument.Dispose();
            stream.Dispose();
            return(returnArray.ToArray());
        }
Пример #2
0
 /// <summary>
 /// Dispose the elements
 /// </summary>
 private void Dispose()
 {
     DeleteLocalFolder();
     if (_presentation != null)
     {
         _presentation.Dispose();
         _presentation = null;
     }
     if (!_isWindowsPhone)
     {
         Image image = MainViewImageGrid.Children[0] as Image;
         if (image != null)
         {
             image.ClearValue(Image.SourceProperty);
             image.ClearValue(Image.StretchProperty);
         }
         MainViewImageGrid.Children.Clear();
         ThumbnailGrid.Children.Clear();
     }
     if (_isWindowsPhone)
     {
         if (ThumbnailStackPanel != null)
         {
             ThumbnailStackPanel.Children.Clear();
         }
     }
 }
        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);
            }
        }
Пример #4
0
        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");
            }
        }