PDFViewer.xaml.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using Syncfusion.SfPdfViewer.XForms;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using Xamarin.Essentials;
  11. using Xamarin.Forms;
  12. using Xamarin.Forms.Xaml;
  13. using XF.Material.Forms.UI.Dialogs;
  14. using PRSSecurity = InABox.Core.Security;
  15. namespace comal.timesheets
  16. {
  17. [XamlCompilation(XamlCompilationOptions.Compile)]
  18. public partial class PDFViewer : ContentPage
  19. {
  20. Document doc = new Document();
  21. bool externalOpened = false;
  22. public PDFViewer(Guid id, bool allowPrintShare = false)
  23. {
  24. InitializeComponent();
  25. NavigationPage.SetHasBackButton(this, false);
  26. if (Device.RuntimePlatform.Equals(Device.Android))
  27. {
  28. pdfViewerControl.CustomPdfRenderer = DependencyService.Get<ICustomPdfRendererService>().AlternatePdfRenderer;
  29. pdfViewerControl.MaximumZoomPercentage = 600;
  30. }
  31. else
  32. pdfViewerControl.MaximumZoomPercentage = 50000;
  33. if (allowPrintShare)
  34. {
  35. shareBtn.IsVisible = true;
  36. printBtn.IsVisible = true;
  37. }
  38. LoadPDF(id);
  39. }
  40. public PDFViewer(MemoryStream stream, bool allowPrintShare = false)
  41. {
  42. InitializeComponent();
  43. NavigationPage.SetHasBackButton(this, false);
  44. if (allowPrintShare)
  45. {
  46. shareBtn.IsVisible = true;
  47. printBtn.IsVisible = true;
  48. }
  49. pdfViewerControl.LoadDocument(stream);
  50. }
  51. void Exit_Clicked(object sedner, EventArgs e)
  52. {
  53. Navigation.PopAsync();
  54. }
  55. protected override void OnAppearing()
  56. {
  57. base.OnAppearing();
  58. if (externalOpened)
  59. Navigation.PopAsync();
  60. }
  61. #region Load PDF
  62. public async void LoadPDF(Guid docid)
  63. {
  64. try
  65. {
  66. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  67. {
  68. CoreTable table2 = new Client<Document>().Query
  69. (
  70. new Filter<Document>(x => x.ID).IsEqualTo(docid),
  71. new Columns<Document>(x => x.FileName, x => x.Data)
  72. );
  73. if (table2.Rows.Any())
  74. {
  75. doc.FileName = table2.Rows.First().Values[0].ToString();
  76. if (doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf"))
  77. {
  78. byte[] data = table2.Rows.First().Get<Document, byte[]>(x => x.Data);
  79. if (Device.RuntimePlatform.Equals(Device.Android) && PRSSecurity.IsAllowed<CanOpenMobileNativePDFViewer>())
  80. OpenNativeViewer(doc.FileName, data);
  81. else
  82. ShowPDF(data);
  83. }
  84. else
  85. {
  86. Device.BeginInvokeOnMainThread(() =>
  87. {
  88. DisplayAlert("Error", "File type is not PDF", "OK");
  89. Navigation.PopAsync();
  90. });
  91. }
  92. }
  93. }
  94. }
  95. catch { }
  96. }
  97. private async void OpenNativeViewer(string filename, byte[] data)
  98. {
  99. var filePath = Path.Combine(FileSystem.AppDataDirectory, filename);
  100. File.WriteAllBytes(filePath, data);
  101. await Launcher.OpenAsync(new OpenFileRequest
  102. {
  103. File = new ReadOnlyFile(filePath)
  104. });
  105. }
  106. void ShowPDF(byte[] data)
  107. {
  108. MemoryStream memoryStream = new MemoryStream(data);
  109. Device.BeginInvokeOnMainThread(() =>
  110. {
  111. pdfViewerControl.LoadDocument(memoryStream);
  112. });
  113. }
  114. public void LoadPDF(List<Guid> docids)
  115. {
  116. Task.Run(async () =>
  117. {
  118. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  119. {
  120. List<byte> bytes = new List<byte>();
  121. foreach (var docid in docids)
  122. {
  123. CoreTable table2 = new Client<Document>().Query
  124. (
  125. new Filter<Document>(x => x.ID).IsEqualTo(docid)
  126. );
  127. if (table2.Rows.Any())
  128. {
  129. Document doc = table2.Rows.First().ToObject<Document>();
  130. byte[] data = table2.Rows.First().Get<Document, byte[]>(x => x.Data);
  131. List<Byte> bytes1 = data.ToList();
  132. bytes.AddRange(bytes1);
  133. }
  134. }
  135. byte[] allData = bytes.ToArray();
  136. MemoryStream memoryStream = new MemoryStream(allData);
  137. Device.BeginInvokeOnMainThread(() =>
  138. {
  139. pdfViewerControl.LoadDocument(memoryStream);
  140. });
  141. }
  142. });
  143. }
  144. #endregion
  145. async void PrintBtn_Clicked(object sender, EventArgs e)
  146. {
  147. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  148. {
  149. pdfViewerControl.Print(doc.FileName);
  150. }
  151. }
  152. async void ShareBtn_Clicked(object sender, EventArgs e)
  153. {
  154. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  155. {
  156. var folder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  157. string path = Path.Combine(folder, doc.FileName);
  158. File.WriteAllBytes(path, doc.Data);
  159. ShareFile file = new ShareFile(path);
  160. await Share.RequestAsync(new ShareFileRequest
  161. {
  162. Title = "Share PDF",
  163. File = file
  164. });
  165. }
  166. }
  167. }
  168. }