DocumentViewer.xaml.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using InABox.Core;
  8. using InABox.Mobile;
  9. using Syncfusion.SfPdfViewer.XForms;
  10. using Xamarin.Forms;
  11. using Xamarin.Forms.Xaml;
  12. using XF.Material.Forms.UI.Dialogs;
  13. namespace PRS.Mobile
  14. {
  15. [XamlCompilation(XamlCompilationOptions.Compile)]
  16. public partial class DocumentViewer
  17. {
  18. public static async Task<DocumentViewer> Load(Guid id)
  19. {
  20. DocumentModel docmodel = new DocumentModel(App.Data,
  21. () => new Filter<Document>(x => x.ID).IsEqualTo(id)) { FileName = $"{id}.document" };
  22. using (await MaterialDialog.Instance.LoadingDialogAsync("Loading Document"))
  23. docmodel.Refresh(true);
  24. if (docmodel.Any())
  25. {
  26. var editor = new DocumentViewer();
  27. editor.Load(docmodel.First().FileName, docmodel.First().Data);
  28. return editor;
  29. }
  30. else
  31. await MaterialDialog.Instance.AlertAsync("Cannot Load Document!");
  32. return null;
  33. }
  34. public void Load(String filename, byte[] data)
  35. {
  36. _filename.Text = filename;
  37. _filename.IsVisible = ShowFileName && !String.IsNullOrWhiteSpace(filename);
  38. if (data?.Any() == true)
  39. {
  40. if (filename.ToUpper().EndsWith(".PDF"))
  41. LoadPDF(data);
  42. else
  43. LoadImage(data);
  44. _noimage.IsVisible = false;
  45. }
  46. else
  47. {
  48. _noimage.IsVisible = true;
  49. _pdf.IsVisible = false;
  50. _image.IsVisible = false;
  51. }
  52. }
  53. private bool _showfilename = false;
  54. public bool ShowFileName
  55. {
  56. get => _showfilename;
  57. set
  58. {
  59. _showfilename = value;
  60. _filename.IsVisible = value && !String.IsNullOrWhiteSpace(_filename.Text);
  61. }
  62. }
  63. public DocumentViewer()
  64. {
  65. InitializeComponent();
  66. BindingContext = this;
  67. if (Device.RuntimePlatform.Equals(Device.Android))
  68. {
  69. var service = DependencyService.Get<ICustomPdfRendererService>();
  70. var renderer = service?.AlternatePdfRenderer;
  71. _pdf.CustomPdfRenderer = renderer;
  72. }
  73. }
  74. private byte[]? _data;
  75. public event EventHandler DocumentLoaded;
  76. private void LoadPDF(byte[] data)
  77. {
  78. _data = data;
  79. _image.IsVisible = false;
  80. MemoryStream memoryStream = new MemoryStream(data);
  81. _pdf.LoadDocument(memoryStream);
  82. _pdf.IsVisible = true;
  83. DocumentLoaded?.Invoke(this, EventArgs.Empty);
  84. }
  85. private void LoadImage(byte[] data)
  86. {
  87. _data = data;
  88. _pdf.IsVisible = false;
  89. _image.Source = ImageSource.FromStream(() => new MemoryStream(data));
  90. _image.IsVisible = true;
  91. DocumentLoaded?.Invoke(this, EventArgs.Empty);
  92. }
  93. public byte[] GetThumbnail()
  94. {
  95. if (_data?.Any() != true)
  96. return new byte[] { };
  97. if (_image.IsVisible)
  98. {
  99. return MobileUtils.ImageTools.CreateThumbnail(_data, 256, 256);
  100. }
  101. else if (_pdf.IsVisible)
  102. {
  103. var stream = _pdf.ExportAsImage(0) as MemoryStream;
  104. if (stream?.Length > 0)
  105. {
  106. var imgdata = stream.ToArray();
  107. return MobileUtils.ImageTools.CreateThumbnail(imgdata, 256, 256);
  108. }
  109. }
  110. return new byte[] { };
  111. }
  112. }
  113. }