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
{
///
/// Extracts content from PDF files using PdfPig ...
///
public class XPdfFileContentExtractor : IXPdfFileContentExtractor
{
///
/// Supported MIME Types ...
///
private static readonly string[] SupportedMimeTypes =
[
"application/pdf"
];
///
/// Minimum Length for Fallback Using Images ...
///
private const int MinTextLengthThreshold = 50;
///
/// Max Pages for Falling Back Using Images ...
///
private const int MaxPagesForImageFallback = 10;
///
/// Default DPI for Rendering Images ...
///
private const int RenderDpi = 150;
///
/// Max Rendered Image Width ...
///
private const int MaxImageWidth = 2000;
///
/// Api Configuration ...
///
private readonly XAiApiConfiguration configuration;
public XPdfFileContentExtractor(XAiApiConfiguration configuration)
{
this.configuration = configuration;
}
///
/// Check if this extractor supports the specified MIME type ...
///
public bool CanExtract(string mimeType)
{
//
return SupportedMimeTypes.Contains(
mimeType?.ToLowerInvariant() ?? string.Empty
);
}
///
/// Extract text content from file stream ...
///
public async Task ExtractAsync(
Stream fileStream,
string mimeType,
CancellationToken cancellationToken = default
)
{
//
var result = await ExtractRichAsync(
fileStream,
"document.pdf",
mimeType,
cancellationToken
);
//
return result.Text;
}
///
/// Extract content from stream as Rich Result ...
///
///
///
///
///
///
public async Task 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;
}
///
/// Extract Text From PDF Files using Common way ...
///
///
///
///
private async Task 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;
}
///
/// Check Extracted Content is Sufficient or not ...
/// if not try to Falling Back using Images ...
///
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;
}
///
/// Fallback Converts PDF Pages to Images ...
///
private async Task 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;
}
}
}