Exemple #1
0
 public ActionResult <PreviewResponse> PreviewPdf(Stream stream, int pageIndex)
 {
     try
     {
         if (stream.Length == 0)
         {
             throw new Exception("Поток не может быть пуст");
         }
         if (pageIndex < 0)
         {
             throw new Exception("Номер страницы не может быть меньше 0");
         }
         _logger.LogDebug($"Start create preview");
         PreviewResponse     responsePreview = new PreviewResponse();
         PreviewFileResponse previewResult   = _fileInfoPdf.Preview(stream, pageIndex);
         responsePreview.Values.Add(previewResult);
         _logger.LogDebug("PdfController.PreviewPdf() OK");
         return(Ok(responsePreview));
     }
     catch (Exception e)
     {
         _logger.LogError(e.Message);
         return(BadRequest(e.Message));
     }
 }
Exemple #2
0
        public ActionResult <ProcessResponse> Process(IFormFile file)
        {
            _logger.LogDebug($"Process response");
            ProcessResponse response      = new ProcessResponse();
            string          fileExtension = Path.GetExtension(file.FileName).Trim('.');

            try
            {
                using (Stream stream = file.OpenReadStream())
                {
                    if (file.Length == 0)
                    {
                        throw new Exception("Файл не может быть пуст");
                    }
                    if (CheckFileExtension.GetFileExtension(fileExtension) == FileExtension.Extensions.Unknown)
                    {
                        throw new Exception("Неподдерживаемый тип файла");
                    }
                    if (CheckFileExtension.GetFileExtension(fileExtension) == FileExtension.Extensions.Pdf)
                    {
                        ExtractTextFileResponse extractResult = _fileInfoPdf.Extract(stream);
                        PreviewFileResponse     previewResult = _fileInfoPdf.Preview(stream, 0);
                        ProcessFileResponse     result        = new ProcessFileResponse {
                            Image = previewResult.Image, TextValues = extractResult.Values
                        };
                        response.Values.Add(result);
                        return(response);
                    }
                    else
                    {
                        byte[] resultPdf = _fileInfoPdf.Convert(stream, file.FileName);
                        using (Stream pdfStream = new MemoryStream(resultPdf))
                        {
                            ExtractTextFileResponse extractResult = _fileInfoPdf.Extract(pdfStream);
                            PreviewFileResponse     previewResult = _fileInfoPdf.Preview(pdfStream, 0);
                            ProcessFileResponse     result        = new ProcessFileResponse {
                                Name = file.FileName, Pdf = resultPdf, TextValues = extractResult.Values, Image = previewResult.Image
                            };
                            response.Values.Add(result);
                        }
                    }
                }
                _logger.LogDebug("PdfController.Process() OK");
                return(Ok(response));
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(BadRequest($"Ошибка обработки файла '{file.FileName}'. Фаил неизвестного формата или файл поврежден"));
            }
        }
Exemple #3
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);
             }
         }
 }