|
|
@@ -7,22 +7,11 @@ public class iOS_PdfRenderer : IPdfRenderer
|
|
|
|
|
|
public Logger? Logger { get; set; }
|
|
|
|
|
|
- public byte[]? PdfToImage(byte[]? pdf, int pageIndex, int dpi)
|
|
|
+ private byte[] PdfDocToImage(CGPDFDocument document, int pageIndex, int dpi)
|
|
|
{
|
|
|
-
|
|
|
float scale = 2F;
|
|
|
|
|
|
- // Step 1: Load the PDF document from byte array
|
|
|
- using var data = NSData.FromArray(pdf);
|
|
|
- using var provider = new CGDataProvider(data);
|
|
|
- using var pdfDoc = new CGPDFDocument(provider);
|
|
|
-
|
|
|
- // Validate page index
|
|
|
- int pageCount = (int)pdfDoc.Pages;
|
|
|
- if (pageIndex < 0 || pageIndex >= pageCount)
|
|
|
- throw new ArgumentOutOfRangeException(nameof(pageIndex), "Invalid PDF page index.");
|
|
|
-
|
|
|
- using var page = pdfDoc.GetPage(pageIndex + 1); // Pages are 1-indexed
|
|
|
+ using var page = document.GetPage(pageIndex + 1); // Pages are 1-indexed
|
|
|
var pageRect = page.GetBoxRect(CGPDFBox.Media);
|
|
|
var scaledSize = new CGSize(pageRect.Width * scale, pageRect.Height * scale);
|
|
|
|
|
|
@@ -43,13 +32,60 @@ public class iOS_PdfRenderer : IPdfRenderer
|
|
|
// Step 3: Convert UIImage to PNG byte array
|
|
|
using var pngData = image.AsPNG();
|
|
|
return pngData.ToArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ public byte[]? PdfToImage(byte[]? pdf, int pageIndex, int dpi)
|
|
|
+ {
|
|
|
+ if (pdf is null || pdf.Length == 0)
|
|
|
+ return null;
|
|
|
+
|
|
|
+ // Step 1: Load the PDF document from byte array
|
|
|
+ using var data = NSData.FromArray(pdf);
|
|
|
+ using var provider = new CGDataProvider(data);
|
|
|
+ using var pdfDoc = new CGPDFDocument(provider);
|
|
|
+
|
|
|
+ // Validate page index
|
|
|
+ int pageCount = (int)pdfDoc.Pages;
|
|
|
+ if (pageIndex < 0 || pageIndex >= pageCount)
|
|
|
+ throw new ArgumentOutOfRangeException(nameof(pageIndex), "Invalid PDF page index.");
|
|
|
|
|
|
+ return PdfDocToImage(pdfDoc, pageIndex, dpi);
|
|
|
}
|
|
|
|
|
|
- public Task<byte[]?> PdfToImageAsync(byte[]? pdf, int page, int dpi)
|
|
|
+ public byte[][]? PdfToImages(byte[]? pdf, int dpi)
|
|
|
+ {
|
|
|
+ if (pdf is null || pdf.Length == 0)
|
|
|
+ return null;
|
|
|
+ float scale = 2F;
|
|
|
+
|
|
|
+ // Step 1: Load the PDF document from byte array
|
|
|
+ using var data = NSData.FromArray(pdf);
|
|
|
+ using var provider = new CGDataProvider(data);
|
|
|
+ using var pdfDoc = new CGPDFDocument(provider);
|
|
|
+
|
|
|
+ // Validate page index
|
|
|
+ int pageCount = (int)pdfDoc.Pages;
|
|
|
+
|
|
|
+ return Enumerable.Range(0, pageCount).Select(pageIndex =>
|
|
|
+ {
|
|
|
+ return PdfDocToImage(pdfDoc, pageIndex, dpi);
|
|
|
+ }).ToArray();
|
|
|
+ }
|
|
|
+
|
|
|
+ public int? PdfPageCount(byte[]? pdf)
|
|
|
{
|
|
|
+ if (pdf is null || pdf.Length == 0)
|
|
|
+ return null;
|
|
|
+ // Step 1: Load the PDF document from byte array
|
|
|
+ using var data = NSData.FromArray(pdf);
|
|
|
+ using var provider = new CGDataProvider(data);
|
|
|
+ using var pdfDoc = new CGPDFDocument(provider);
|
|
|
+ return (int)pdfDoc.Pages;
|
|
|
+ }
|
|
|
|
|
|
- var tcs = new TaskCompletionSource<byte[]>();
|
|
|
+ public Task<byte[]?> PdfToImageAsync(byte[]? pdf, int page, int dpi)
|
|
|
+ {
|
|
|
+ var tcs = new TaskCompletionSource<byte[]?>();
|
|
|
|
|
|
UIApplication.SharedApplication.BeginInvokeOnMainThread(() =>
|
|
|
{
|
|
|
@@ -65,8 +101,46 @@ public class iOS_PdfRenderer : IPdfRenderer
|
|
|
});
|
|
|
|
|
|
return tcs.Task;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task<byte[][]?> PdfToImagesAsync(byte[]? pdf, int dpi)
|
|
|
+ {
|
|
|
+ var tcs = new TaskCompletionSource<byte[][]?>();
|
|
|
+
|
|
|
+ UIApplication.SharedApplication.BeginInvokeOnMainThread(() =>
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var result = PdfToImages(pdf, dpi);
|
|
|
+ tcs.SetResult(result);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ tcs.SetException(ex);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return tcs.Task;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Task<int?> PdfPageCountAsync(byte[]? pdf)
|
|
|
+ {
|
|
|
+ var tcs = new TaskCompletionSource<int?>();
|
|
|
|
|
|
+ UIApplication.SharedApplication.BeginInvokeOnMainThread(() =>
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var result = PdfPageCount(pdf);
|
|
|
+ tcs.SetResult(result);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ tcs.SetException(ex);
|
|
|
+ }
|
|
|
+ });
|
|
|
|
|
|
+ return tcs.Task;
|
|
|
}
|
|
|
|
|
|
public byte[]? ImageToPdf(byte[]? image)
|
|
|
@@ -76,7 +150,7 @@ public class iOS_PdfRenderer : IPdfRenderer
|
|
|
|
|
|
public Task<byte[]?> ImageToPdfAsync(byte[]? image)
|
|
|
{
|
|
|
- var tcs = new TaskCompletionSource<byte[]>();
|
|
|
+ var tcs = new TaskCompletionSource<byte[]?>();
|
|
|
|
|
|
UIApplication.SharedApplication.BeginInvokeOnMainThread(() =>
|
|
|
{
|