Fix Windows Relation on Audio and PDF Content Extraction ...
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using SkiaSharp;
|
||||
using PDFtoImage;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using PdfiumViewer;
|
||||
using System.Threading;
|
||||
using xAiModels.Models;
|
||||
using xAiApi.Configurations;
|
||||
using System.Threading.Tasks;
|
||||
using xAiApi.Interfaces.Extractors;
|
||||
|
||||
@@ -43,6 +45,16 @@ namespace xAiApi.Providers.Extractors
|
||||
/// </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>
|
||||
@@ -211,18 +223,33 @@ namespace xAiApi.Providers.Extractors
|
||||
try
|
||||
{
|
||||
//
|
||||
using var document = PdfDocument.Load(pdfStream);
|
||||
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(
|
||||
document.PageCount,
|
||||
allPagesCount,
|
||||
MaxPagesForImageFallback
|
||||
);
|
||||
|
||||
//
|
||||
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++)
|
||||
for (int pageIndex = 0; pageIndex < allPagesCount; pageIndex++)
|
||||
{
|
||||
//
|
||||
// Check Cancellation Token ...
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
@@ -232,52 +259,23 @@ namespace xAiApi.Providers.Extractors
|
||||
try
|
||||
{
|
||||
//
|
||||
var pageSize = document.PageSizes[pageIndex];
|
||||
|
||||
//
|
||||
var scaleFactor = RenderDpi / 72.0;
|
||||
var renderWidth = (int)Math.Ceiling(pageSize.Width * scaleFactor);
|
||||
var renderHeight = (int)Math.Ceiling(pageSize.Height * scaleFactor);
|
||||
|
||||
//
|
||||
if (renderWidth > MaxImageWidth)
|
||||
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())
|
||||
{
|
||||
//
|
||||
var ratio = (double)MaxImageWidth / renderWidth;
|
||||
renderWidth = MaxImageWidth;
|
||||
renderHeight = (int)(renderHeight * ratio);
|
||||
data.SaveTo(ms);
|
||||
result.Images.Add(new XExtractedImage
|
||||
{
|
||||
Bytes = ms.ToArray(),
|
||||
MimeType = "image/png",
|
||||
Description = $"Page {pageIndex}, rendered from {fileName}"
|
||||
});
|
||||
}
|
||||
|
||||
//
|
||||
using var bitmap = document.Render(
|
||||
dpiX: RenderDpi,
|
||||
dpiY: RenderDpi,
|
||||
page: pageIndex,
|
||||
width: renderWidth,
|
||||
forPrinting: false,
|
||||
height: renderHeight
|
||||
);
|
||||
|
||||
//
|
||||
byte[] imageBytes;
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
//
|
||||
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
|
||||
imageBytes = memoryStream.ToArray();
|
||||
}
|
||||
|
||||
//
|
||||
result.Images.Add(new XExtractedImage
|
||||
{
|
||||
Bytes = imageBytes,
|
||||
MimeType = "image/png",
|
||||
PageNumber = pageIndex + 1,
|
||||
Description = $"Page {pageIndex + 1} of {fileName} ({renderWidth}x{renderHeight})"
|
||||
});
|
||||
}
|
||||
catch (Exception)
|
||||
{ }
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user