DocumentViewer.xaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 Syncfusion.SfPdfViewer.XForms;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. using XF.Material.Forms.UI.Dialogs;
  12. namespace PRS.Mobile
  13. {
  14. [XamlCompilation(XamlCompilationOptions.Compile)]
  15. public partial class DocumentViewer
  16. {
  17. public static async Task<DocumentViewer> Load(Guid id)
  18. {
  19. DocumentModel docmodel = new DocumentModel(App.Data,
  20. () => new Filter<Document>(x => x.ID).IsEqualTo(id)) { FileName = $"{id}.document" };
  21. using (await MaterialDialog.Instance.LoadingDialogAsync("Loading Document"))
  22. docmodel.Refresh(true);
  23. if (docmodel.Any())
  24. {
  25. var editor = new DocumentViewer();
  26. editor.Load(docmodel.First().FileName, docmodel.First().Data);
  27. return editor;
  28. }
  29. else
  30. await MaterialDialog.Instance.AlertAsync("Cannot Load Document!");
  31. return null;
  32. }
  33. public void Load(String filename, byte[] data)
  34. {
  35. _filename.Text = filename;
  36. _filename.IsVisible = ShowFileName && !String.IsNullOrWhiteSpace(filename);
  37. if (data?.Any() == true)
  38. {
  39. if (filename.ToUpper().EndsWith(".PDF"))
  40. LoadPDF(data);
  41. else
  42. LoadImage(data);
  43. _noimage.IsVisible = false;
  44. }
  45. else
  46. {
  47. _noimage.IsVisible = true;
  48. _pdf.IsVisible = false;
  49. _image.IsVisible = false;
  50. }
  51. }
  52. private bool _showfilename = false;
  53. public bool ShowFileName
  54. {
  55. get => _showfilename;
  56. set
  57. {
  58. _showfilename = value;
  59. _filename.IsVisible = value && !String.IsNullOrWhiteSpace(_filename.Text);
  60. }
  61. }
  62. public DocumentViewer()
  63. {
  64. InitializeComponent();
  65. BindingContext = this;
  66. //if (Device.RuntimePlatform.Equals(Device.Android))
  67. // _pdf.CustomPdfRenderer = DependencyService.Get<ICustomPdfRendererService>().AlternatePdfRenderer;
  68. }
  69. private void LoadPDF(byte[] data)
  70. {
  71. _image.IsVisible = false;
  72. MemoryStream memoryStream = new MemoryStream(data);
  73. _pdf.LoadDocument(memoryStream);
  74. _pdf.IsVisible = true;
  75. }
  76. private void LoadImage(byte[] data)
  77. {
  78. _pdf.IsVisible = false;
  79. _image.Source = ImageSource.FromStream(() => new MemoryStream(data));
  80. _image.IsVisible = true;
  81. }
  82. }
  83. }