291 lines
9.2 KiB
C#
291 lines
9.2 KiB
C#
using System;
|
|
using System.IO;
|
|
using SkiaSharp;
|
|
using PDFtoImage;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using xAiModels.Models;
|
|
using xAiApi.Configurations;
|
|
using System.Threading.Tasks;
|
|
using xAiApi.Interfaces.Extractors;
|
|
|
|
namespace xAiApi.Providers.Extractors
|
|
{
|
|
/// <summary>
|
|
/// Extracts content from PDF files using PdfPig ...
|
|
/// </summary>
|
|
public class XPdfFileContentExtractor : IXPdfFileContentExtractor
|
|
{
|
|
/// <summary>
|
|
/// Supported MIME Types ...
|
|
/// </summary>
|
|
private static readonly string[] SupportedMimeTypes =
|
|
[
|
|
"application/pdf"
|
|
];
|
|
|
|
/// <summary>
|
|
/// Minimum Length for Fallback Using Images ...
|
|
/// </summary>
|
|
private const int MinTextLengthThreshold = 50;
|
|
|
|
/// <summary>
|
|
/// Max Pages for Falling Back Using Images ...
|
|
/// </summary>
|
|
private const int MaxPagesForImageFallback = 10;
|
|
|
|
/// <summary>
|
|
/// Default DPI for Rendering Images ...
|
|
/// </summary>
|
|
private const int RenderDpi = 150;
|
|
|
|
/// <summary>
|
|
/// Max Rendered Image Width ...
|
|
/// </summary>
|
|
private const int MaxImageWidth = 2000;
|
|
|
|
/// <summary>
|
|
/// Api Configuration ...
|
|
/// </summary>
|
|
private readonly XAiApiConfiguration configuration;
|
|
|
|
public XPdfFileContentExtractor(XAiApiConfiguration configuration)
|
|
{
|
|
this.configuration = configuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if this extractor supports the specified MIME type ...
|
|
/// </summary>
|
|
public bool CanExtract(string mimeType)
|
|
{
|
|
//
|
|
return SupportedMimeTypes.Contains(
|
|
mimeType?.ToLowerInvariant() ?? string.Empty
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract text content from file stream ...
|
|
/// </summary>
|
|
public async Task<string> ExtractAsync(
|
|
Stream fileStream,
|
|
string mimeType,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = await ExtractRichAsync(
|
|
fileStream,
|
|
"document.pdf",
|
|
mimeType,
|
|
cancellationToken
|
|
);
|
|
|
|
//
|
|
return result.Text;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract content from stream as Rich Result ...
|
|
/// </summary>
|
|
/// <param name="fileStream"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public async Task<XFileExtractionResult> ExtractRichAsync(
|
|
Stream fileStream,
|
|
string fileName,
|
|
string mimeType,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = new XFileExtractionResult
|
|
{
|
|
FileName = fileName,
|
|
MimeType = mimeType
|
|
};
|
|
|
|
//
|
|
using var memoryStream = new MemoryStream();
|
|
await fileStream.CopyToAsync(memoryStream, cancellationToken);
|
|
memoryStream.Position = 0;
|
|
|
|
//
|
|
var extractedText = await ExtractTextAsync(
|
|
memoryStream,
|
|
cancellationToken
|
|
);
|
|
if (IsTextSufficient(extractedText))
|
|
{
|
|
// PDF متنی است
|
|
result.Text = extractedText;
|
|
}
|
|
else
|
|
{
|
|
// PDF اسکنشده است - Fallback به تصویر
|
|
memoryStream.Position = 0;
|
|
result = await FallbackToImageExtractionAsync(
|
|
memoryStream,
|
|
fileName,
|
|
mimeType,
|
|
cancellationToken
|
|
);
|
|
result.UsedImageFallback = true;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract Text From PDF Files using Common way ...
|
|
/// </summary>
|
|
/// <param name="pdfStream"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
private async Task<string> ExtractTextAsync(
|
|
Stream pdfStream,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
//
|
|
var result = await Task.Run(() =>
|
|
{
|
|
//
|
|
var sb = new StringBuilder();
|
|
using var document = UglyToad.PdfPig.PdfDocument.Open(pdfStream);
|
|
foreach (var page in document.GetPages())
|
|
{
|
|
//
|
|
var text = page.Text?.Trim();
|
|
if (!string.IsNullOrEmpty(text))
|
|
{
|
|
//
|
|
sb.AppendLine(text);
|
|
sb.AppendLine();
|
|
}
|
|
}
|
|
|
|
//
|
|
return sb.ToString();
|
|
}, cancellationToken);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Extracted Content is Sufficient or not ...
|
|
/// if not try to Falling Back using Images ...
|
|
/// </summary>
|
|
private bool IsTextSufficient(string text)
|
|
{
|
|
//
|
|
if (string.IsNullOrWhiteSpace(text))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
//
|
|
// حذف فاصلهها و بررسی طول
|
|
var cleanText = new string(text.Where(c => !char.IsWhiteSpace(c)).ToArray());
|
|
|
|
//
|
|
return cleanText.Length >= MinTextLengthThreshold;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fallback Converts PDF Pages to Images ...
|
|
/// </summary>
|
|
private async Task<XFileExtractionResult> FallbackToImageExtractionAsync(
|
|
Stream pdfStream,
|
|
string fileName,
|
|
string mimeType,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
//
|
|
var result = new XFileExtractionResult
|
|
{
|
|
FileName = fileName,
|
|
MimeType = mimeType,
|
|
UsedImageFallback = true
|
|
};
|
|
|
|
//
|
|
await Task.Run(() =>
|
|
{
|
|
//
|
|
try
|
|
{
|
|
//
|
|
var images = Conversion.ToImages(
|
|
pdfStream: pdfStream,
|
|
options: new RenderOptions
|
|
{
|
|
Dpi = configuration.PDFToImageDescriptor.Dpi,
|
|
Width = configuration.PDFToImageDescriptor.Width,
|
|
Height = configuration.PDFToImageDescriptor.Height,
|
|
UseTiling = configuration.PDFToImageDescriptor.UseTiling,
|
|
WithFormFill = configuration.PDFToImageDescriptor.WithFormFill,
|
|
WithAspectRatio = configuration.PDFToImageDescriptor.WithAspectRatio,
|
|
WithAnnotations = configuration.PDFToImageDescriptor.WithAnnotations,
|
|
DpiRelativeToBounds = configuration.PDFToImageDescriptor.DpiRelativeToBounds,
|
|
}
|
|
).ToList();
|
|
|
|
//
|
|
var allPagesCount = Conversion.GetPageCount(pdfStream);
|
|
var pageCount = Math.Min(
|
|
allPagesCount,
|
|
MaxPagesForImageFallback
|
|
);
|
|
|
|
//
|
|
for (int pageIndex = 0; pageIndex < allPagesCount; pageIndex++)
|
|
{
|
|
//
|
|
// Check Cancellation Token ...
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
break;
|
|
}
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
var skBitmap = images[pageIndex];
|
|
using (skBitmap)
|
|
using (var image = SKImage.FromBitmap(skBitmap))
|
|
using (var data = image.Encode(SKEncodedImageFormat.Png, 100))
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
//
|
|
data.SaveTo(ms);
|
|
result.Images.Add(new XExtractedImage
|
|
{
|
|
Bytes = ms.ToArray(),
|
|
MimeType = "image/png",
|
|
Description = $"Page {pageIndex}, rendered from {fileName}"
|
|
});
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.ErrorMessage = $"PDF image extraction failed: {ex.Message}";
|
|
}
|
|
}, cancellationToken);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |