예제 #1
0
        private async void Render(int page, bool isFirstTime = false)
        {
            if (pdfFile.Exists())
            {
                try
                {
                    rendering   = true;
                    pdfRenderer = new PdfRenderer(ParcelFileDescriptor.Open(pdfFile, ParcelFileMode.ReadOnly));
                    totalPages  = pdfRenderer.PageCount;
                    var previousPage = currentPage;
                    currentPage = page;
                    if (currentPage < 0)
                    {
                        currentPage = 0;
                    }
                    else if (currentPage >= pdfRenderer.PageCount)
                    {
                        currentPage = pdfRenderer.PageCount - 1;
                    }
                    if (currentPage == previousPage && !isFirstTime)
                    {
                        return;
                    }
                    pageCount.Text = $"{currentPage + 1} of {pdfRenderer.PageCount}";
                    var pdfPage = pdfRenderer.OpenPage(currentPage);
                    setVisibilities(false);
                    await
                    Task.Run(
                        () =>
                        pdfPage.Render(GetBitmap(pdfPage.Width, pdfPage.Height), rect, pdfView.Matrix,
                                       PdfRenderMode.ForDisplay));

                    pdfView.SetImageBitmap(bitmapPdf);
                    pdfView.Invalidate();
                    pdfRenderer.Dispose();
                    pdfRenderer = null;
                    bitmapPdf   = null;
                    GC.Collect();
                    setVisibilities(true);
                    rendering = false;
                }
                catch (Exception ex)
                {
                    // ignored
                }
            }
            else
            {
                if (page < 0)
                {
                    currentPage = 0;
                }
                else if (page < totalPages)
                {
                    currentPage = page;
                }
                DownloadPdfDocument();
            }
        }
        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);
            }
        }
예제 #3
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");
            }
        }