MobileDocument.cs 2.1 KB

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