DocumentViewer.xaml.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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),
  22. () => CoreRepository.CacheFileName<Document>($"{id}")
  23. );
  24. using (await MaterialDialog.Instance.LoadingDialogAsync("Loading Document"))
  25. docmodel.Refresh(true);
  26. if (docmodel.Any())
  27. {
  28. var editor = new DocumentViewer();
  29. editor.Load(docmodel.First().FileName, docmodel.First().Data);
  30. return editor;
  31. }
  32. else
  33. await MaterialDialog.Instance.AlertAsync("Cannot Load Document!");
  34. return null;
  35. }
  36. public void Load(String filename, byte[] data)
  37. {
  38. _filename.Text = filename;
  39. _filename.IsVisible = ShowFileName && !String.IsNullOrWhiteSpace(filename);
  40. if (data?.Any() == true)
  41. {
  42. if (filename.ToUpper().EndsWith(".PDF"))
  43. LoadPDF(data);
  44. else
  45. LoadImage(data);
  46. _noimage.IsVisible = false;
  47. }
  48. else
  49. {
  50. _noimage.IsVisible = true;
  51. _pdf.IsVisible = false;
  52. _image.IsVisible = false;
  53. }
  54. }
  55. private bool _showfilename = false;
  56. public bool ShowFileName
  57. {
  58. get => _showfilename;
  59. set
  60. {
  61. _showfilename = value;
  62. _filename.IsVisible = value && !String.IsNullOrWhiteSpace(_filename.Text);
  63. }
  64. }
  65. public DocumentViewer()
  66. {
  67. InitializeComponent();
  68. BindingContext = this;
  69. if (Device.RuntimePlatform.Equals(Device.Android))
  70. {
  71. var service = DependencyService.Get<ICustomPdfRendererService>();
  72. var renderer = service?.AlternatePdfRenderer;
  73. _pdf.CustomPdfRenderer = renderer;
  74. }
  75. }
  76. private byte[]? _data;
  77. public event EventHandler DocumentLoaded;
  78. private void LoadPDF(byte[] data)
  79. {
  80. _data = data;
  81. _image.IsVisible = false;
  82. MemoryStream memoryStream = new MemoryStream(data);
  83. _pdf.LoadDocument(memoryStream);
  84. _pdf.IsVisible = true;
  85. DocumentLoaded?.Invoke(this, EventArgs.Empty);
  86. }
  87. private void LoadImage(byte[] data)
  88. {
  89. _data = data;
  90. _pdf.IsVisible = false;
  91. _image.Source = ImageSource.FromStream(() => new MemoryStream(data));
  92. _image.IsVisible = true;
  93. DocumentLoaded?.Invoke(this, EventArgs.Empty);
  94. }
  95. public byte[] GetThumbnail()
  96. {
  97. if (_data?.Any() != true)
  98. return new byte[] { };
  99. if (_image.IsVisible)
  100. {
  101. return MobileUtils.ImageTools.CreateThumbnail(_data, 256, 256);
  102. }
  103. else if (_pdf.IsVisible)
  104. {
  105. var stream = _pdf.ExportAsImage(0) as MemoryStream;
  106. if (stream?.Length > 0)
  107. {
  108. var imgdata = stream.ToArray();
  109. return MobileUtils.ImageTools.CreateThumbnail(imgdata, 256, 256);
  110. }
  111. }
  112. return new byte[] { };
  113. }
  114. }
  115. }