Exemple #1
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 #2
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);
        }
Exemple #3
0
 public ActionResult <ExtractTextResponse> ExtractText(Stream stream)
 {
     try
     {
         if (stream.Length == 0)
         {
             throw new Exception("Поток не может быть пуст");
         }
         _logger.LogDebug($"Start extract text");
         ExtractTextResponse     responseExtract = new ExtractTextResponse();
         ExtractTextFileResponse extractResult   = _fileInfoPdf.Extract(stream);
         responseExtract.Values.Add(extractResult);
         _logger.LogDebug("PdfController.ExtractText() OK");
         return(Ok(responseExtract));
     }
     catch (Exception e)
     {
         _logger.LogError(e.Message);
         return(BadRequest(e.Message));
     }
 }