MobileDocument.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Syncfusion.Pdf;
  2. using Syncfusion.Pdf.Graphics;
  3. namespace InABox.Avalonia
  4. {
  5. public class MobileDocument
  6. {
  7. public string FileName { get; set; }
  8. public byte[] Data { get; set; }
  9. public MobileDocument()
  10. {
  11. FileName = "";
  12. Data = new byte[] { };
  13. }
  14. public MobileDocument(string filename, byte[] data)
  15. {
  16. FileName = filename;
  17. Data = data;
  18. }
  19. public void ConvertToPDF()
  20. {
  21. using (var img = new MemoryStream(Data))
  22. {
  23. var image = new PdfBitmap(img);
  24. var pdfDoc = new PdfDocument();
  25. var section = pdfDoc.Sections.Add();
  26. section.PageSettings.Margins.All = 0;
  27. section.PageSettings.Width = image.Width;
  28. section.PageSettings.Height = image.Height;
  29. var page = section.Pages.Add();
  30. page.Graphics.DrawImage(image, 0, 0, page.Size.Width, page.Size.Height);
  31. using (var ms = new MemoryStream())
  32. {
  33. pdfDoc.Save(ms);
  34. Data = ms.GetBuffer();
  35. FileName = Path.ChangeExtension(FileName, "pdf");
  36. }
  37. }
  38. }
  39. public bool IsPDF() => FileName.ToUpper().EndsWith(".PDF");
  40. public static async Task<MobileDocument> From<TSource, TOptions>(TSource source)
  41. where TSource : MobileDocumentSource
  42. where TOptions : MobileDocumentOptions<TSource>
  43. {
  44. var result = await source.From();
  45. return result;
  46. }
  47. public static async Task<MobileDocument> From<T>(MobileDocumentOptions<T> options) where T : MobileDocumentSource
  48. {
  49. var source = (T)Activator.CreateInstance(typeof(T), new object[] { options });
  50. var result = await source.From();
  51. return result;
  52. }
  53. }
  54. }