| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using InABox.Core;
- namespace InABox.Avalonia.Platform.iOS;
- public class iOS_PdfRenderer : IPdfRenderer
- {
- public Logger? Logger { get; set; }
- private byte[] PdfDocToImage(CGPDFDocument document, int pageIndex, int dpi)
- {
- float scale = 2F;
- 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);
- // Step 2: Render the page to an image
- UIGraphics.BeginImageContextWithOptions(scaledSize, false, 1.0f);
- using (var context = UIGraphics.GetCurrentContext())
- {
- context.SaveState();
- context.TranslateCTM(0, scaledSize.Height);
- context.ScaleCTM(scale, -scale); // Flip and scale
- context.DrawPDFPage(page);
- context.RestoreState();
- }
- using var image = UIGraphics.GetImageFromCurrentImageContext();
- UIGraphics.EndImageContext();
- // 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 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;
- }
- public Task<byte[]?> PdfToImageAsync(byte[]? pdf, int page, int dpi)
- {
- var tcs = new TaskCompletionSource<byte[]?>();
- UIApplication.SharedApplication.BeginInvokeOnMainThread(() =>
- {
- try
- {
- var result = PdfToImage(pdf, page, dpi);
- tcs.SetResult(result);
- }
- catch (Exception ex)
- {
- tcs.SetException(ex);
- }
- });
- 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)
- {
- return null;
- }
- public Task<byte[]?> ImageToPdfAsync(byte[]? image)
- {
- var tcs = new TaskCompletionSource<byte[]?>();
- UIApplication.SharedApplication.BeginInvokeOnMainThread(() =>
- {
- try
- {
- var result = ImageToPdf(image);
- tcs.SetResult(result);
- }
- catch (Exception ex)
- {
- tcs.SetException(ex);
- }
- });
- return tcs.Task;
- }
- }
|