Esempio n. 1
0
        //Post action for Loading the PDF documents
        public async Task <IActionResult> Load([FromBody] Dictionary <string, string> jsonObject)
        {
            Console.WriteLine("Load called");
            //Initialize the PDF Viewer object with memory cache object
            PdfRenderer  pdfviewer  = new PdfRenderer(_cache);
            MemoryStream stream     = new MemoryStream();
            object       jsonResult = new object();

            if (jsonObject != null && jsonObject.ContainsKey("document"))
            {
                if (bool.Parse(jsonObject["isFileName"]))
                {
                    if (!string.IsNullOrWhiteSpace(jsonObject["document"]))
                    {
                        stream = await _pdfService.DownloadFileAsync(jsonObject["document"]);
                    }
                    else
                    {
                        return(this.Content(jsonObject["document"] + " is not found"));
                    }
                }
                else
                {
                    byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
                    stream = new MemoryStream(bytes);
                }
            }
            jsonResult = pdfviewer.Load(stream, jsonObject);
            return(Content(JsonConvert.SerializeObject(jsonResult)));
        }
        public IActionResult Load([FromBody] Dictionary <string, string> jsonObject)
        {
            PdfRenderer  pdfviewer  = new PdfRenderer(_cache);
            MemoryStream stream     = new MemoryStream();
            object       jsonResult = new object();

            if (jsonObject != null && jsonObject.ContainsKey("document"))
            {
                if (bool.Parse(jsonObject["isFileName"]))
                {
                    string documentPath = GetDocumentPath(jsonObject["document"]);
                    if (!string.IsNullOrEmpty(documentPath))
                    {
                        byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
                        stream = new MemoryStream(bytes);
                    }
                    else
                    {
                        return(this.Content(jsonObject["document"] + " is not found"));
                    }
                }
                else
                {
                    byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
                    stream = new MemoryStream(bytes);
                }
            }
            jsonResult = pdfviewer.Load(stream, jsonObject);
            return(Content(JsonConvert.SerializeObject(jsonResult)));
        }
Esempio n. 3
0
        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);
        }
        void PdfFileView_OnLoaded(object sender, RoutedEventArgs e)
        {
            var pdfDocument = PdfDocument.Load(File);

            if (pdfDocument.PageCount == 0)
            {
                throw new PresentationLoadResultedInZeroPageDocumentException(File);
            }

            TotalPages = pdfDocument.PageCount;
            _pdfRenderer.Load(pdfDocument);

            AsSoonAsWinformsHostLoadsShowSlide(Page, _pdfRenderer);
        }
Esempio n. 5
0
 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);
             }
         }
 }
Esempio n. 6
0
        public ExtractTextFileResponse Extract(Stream pdfStream)
        {
            _logger.LogDebug($"Extract text from file");

            pdfStream.Position = 0;
            PdfRenderer pdfRenderer = new PdfRenderer();

            pdfRenderer.Load(pdfStream);
            ExtractTextFileResponse extractResult = new ExtractTextFileResponse();

            for (int i = 0; i < pdfRenderer.PageCount; i++)
            {
                string text = pdfRenderer.ExtractText(i, out _);
                extractResult.Values.Add(new KeyValuePair <int, string>(i, text));
            }

            _logger.LogDebug("FileInfoPDFService.Extract....OK");
            return(extractResult);
        }
Esempio n. 7
0
        public async Task <IActionResult> Load([FromBody] Dictionary <string, string> jsonObject, CancellationToken cancellationToken)
        {
            //Initialize the PDF viewer object with memory cache object
            PdfRenderer pdfViewer = new PdfRenderer(Cache);

            MemoryStream stream;

            using (var client = new WebClient())
            {
                var bookAbsoluteUrl =
                    await _bookService.GetBookFileAbsoluteUrl(jsonObject["document"].ToInt(), cancellationToken);

                var bookFileBytes = await client.DownloadDataTaskAsync(bookAbsoluteUrl);

                stream = new MemoryStream(bookFileBytes);
            }

            var jsonResult = pdfViewer.Load(stream, jsonObject);

            return(Content(JsonConvert.SerializeObject(jsonResult)));
        }
        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);
        }
Esempio n. 9
0
        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);
            }
        }
Esempio n. 11
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");
            }
        }