PDFViewer.xaml.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 table = QueryDocument(docid);
  69. while (table == null)
  70. table = QueryDocument(docid);
  71. if (table.Rows.Any())
  72. {
  73. doc.FileName = table.Rows.First().Values[0].ToString();
  74. if (doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf"))
  75. {
  76. byte[] data = table.Rows.First().Get<Document, byte[]>(x => x.Data);
  77. if (Device.RuntimePlatform.Equals(Device.Android) && PRSSecurity.IsAllowed<CanOpenMobileNativePDFViewer>())
  78. OpenNativeViewer(doc.FileName, data);
  79. else
  80. ShowPDF(data);
  81. }
  82. else
  83. {
  84. Device.BeginInvokeOnMainThread(() =>
  85. {
  86. DisplayAlert("Error", "File type is not PDF", "OK");
  87. Navigation.PopAsync();
  88. });
  89. }
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. var log = new MobileLogging(LogType.Query, typeof(Document).ToString(), ex.Message, this.GetType().Name);
  96. }
  97. }
  98. private CoreTable QueryDocument(Guid docid)
  99. {
  100. try
  101. {
  102. return new Client<Document>().Query
  103. (
  104. new Filter<Document>(x => x.ID).IsEqualTo(docid),
  105. new Columns<Document>(x => x.FileName, x => x.Data)
  106. );
  107. }
  108. catch (Exception ex)
  109. {
  110. var log = new MobileLogging(LogType.Query, "QueryDocument()", ex.Message + ex.StackTrace, this.GetType().Name);
  111. return null;
  112. }
  113. }
  114. private async void OpenNativeViewer(string filename, byte[] data)
  115. {
  116. var filePath = Path.Combine(FileSystem.AppDataDirectory, filename);
  117. File.WriteAllBytes(filePath, data);
  118. await Launcher.OpenAsync(new OpenFileRequest
  119. {
  120. File = new ReadOnlyFile(filePath)
  121. });
  122. }
  123. void ShowPDF(byte[] data)
  124. {
  125. MemoryStream memoryStream = new MemoryStream(data);
  126. Device.BeginInvokeOnMainThread(() =>
  127. {
  128. pdfViewerControl.LoadDocument(memoryStream);
  129. });
  130. }
  131. public void LoadPDF(List<Guid> docids)
  132. {
  133. Task.Run(async () =>
  134. {
  135. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  136. {
  137. try
  138. {
  139. List<byte> bytes = new List<byte>();
  140. foreach (var docid in docids)
  141. {
  142. CoreTable table2 = new Client<Document>().Query
  143. (
  144. new Filter<Document>(x => x.ID).IsEqualTo(docid)
  145. );
  146. if (table2.Rows.Any())
  147. {
  148. Document doc = table2.Rows.First().ToObject<Document>();
  149. byte[] data = table2.Rows.First().Get<Document, byte[]>(x => x.Data);
  150. List<Byte> bytes1 = data.ToList();
  151. bytes.AddRange(bytes1);
  152. }
  153. }
  154. byte[] allData = bytes.ToArray();
  155. MemoryStream memoryStream = new MemoryStream(allData);
  156. Device.BeginInvokeOnMainThread(() =>
  157. {
  158. try
  159. {
  160. pdfViewerControl.LoadDocument(memoryStream);
  161. }
  162. catch { }
  163. });
  164. }
  165. catch (Exception ex)
  166. {
  167. var log = new MobileLogging(LogType.Query, typeof(Document).ToString(), ex.Message, this.GetType().Name);
  168. }
  169. }
  170. });
  171. }
  172. #endregion
  173. async void PrintBtn_Clicked(object sender, EventArgs e)
  174. {
  175. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  176. {
  177. pdfViewerControl.Print(doc.FileName);
  178. }
  179. }
  180. async void ShareBtn_Clicked(object sender, EventArgs e)
  181. {
  182. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  183. {
  184. var folder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  185. string path = Path.Combine(folder, doc.FileName);
  186. File.WriteAllBytes(path, doc.Data);
  187. ShareFile file = new ShareFile(path);
  188. await Share.RequestAsync(new ShareFileRequest
  189. {
  190. Title = "Share PDF",
  191. File = file
  192. });
  193. }
  194. }
  195. }
  196. }