PDFViewer.xaml.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 InABox.Mobile;
  11. using Xamarin.Essentials;
  12. using Xamarin.Forms;
  13. using Xamarin.Forms.Xaml;
  14. using XF.Material.Forms.UI.Dialogs;
  15. using LogType = InABox.Core.LogType;
  16. using PRSSecurity = InABox.Core.Security;
  17. namespace comal.timesheets
  18. {
  19. [XamlCompilation(XamlCompilationOptions.Compile)]
  20. public partial class PDFViewer : ContentPage
  21. {
  22. Document doc = new Document();
  23. bool externalOpened = false;
  24. public PDFViewer(Guid id, bool allowPrintShare = false)
  25. {
  26. InitializeComponent();
  27. NavigationPage.SetHasBackButton(this, false);
  28. if (Device.RuntimePlatform.Equals(Device.Android))
  29. {
  30. pdfViewerControl.CustomPdfRenderer = DependencyService.Get<ICustomPdfRendererService>().AlternatePdfRenderer;
  31. pdfViewerControl.MaximumZoomPercentage = 600;
  32. }
  33. else
  34. pdfViewerControl.MaximumZoomPercentage = 50000;
  35. if (allowPrintShare)
  36. {
  37. shareBtn.IsVisible = true;
  38. printBtn.IsVisible = true;
  39. }
  40. LoadPDF(id);
  41. }
  42. public PDFViewer(MemoryStream stream, bool allowPrintShare = false)
  43. {
  44. InitializeComponent();
  45. NavigationPage.SetHasBackButton(this, false);
  46. if (allowPrintShare)
  47. {
  48. shareBtn.IsVisible = true;
  49. printBtn.IsVisible = true;
  50. }
  51. pdfViewerControl.LoadDocument(stream);
  52. }
  53. void Exit_Clicked(object sedner, EventArgs e)
  54. {
  55. Navigation.PopAsync();
  56. }
  57. protected override void OnAppearing()
  58. {
  59. base.OnAppearing();
  60. if (externalOpened)
  61. Navigation.PopAsync();
  62. }
  63. #region Load PDF
  64. public async void LoadPDF(Guid docid)
  65. {
  66. try
  67. {
  68. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  69. {
  70. CoreTable table = QueryDocument(docid);
  71. while (table == null)
  72. table = QueryDocument(docid);
  73. if (table.Rows.Any())
  74. {
  75. doc.FileName = table.Rows.First().Values[0].ToString();
  76. if (doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf"))
  77. {
  78. byte[] data = table.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 (Exception ex)
  96. {
  97. MobileLogging.Log(ex);
  98. }
  99. }
  100. private CoreTable QueryDocument(Guid docid)
  101. {
  102. try
  103. {
  104. return new Client<Document>().Query
  105. (
  106. new Filter<Document>(x => x.ID).IsEqualTo(docid),
  107. new Columns<Document>(x => x.FileName, x => x.Data)
  108. );
  109. }
  110. catch (Exception ex)
  111. {
  112. MobileLogging.Log(ex);
  113. return null;
  114. }
  115. }
  116. private async void OpenNativeViewer(string filename, byte[] data)
  117. {
  118. var filePath = Path.Combine(FileSystem.AppDataDirectory, filename);
  119. File.WriteAllBytes(filePath, data);
  120. await Launcher.OpenAsync(new OpenFileRequest
  121. {
  122. File = new ReadOnlyFile(filePath)
  123. });
  124. }
  125. void ShowPDF(byte[] data)
  126. {
  127. MemoryStream memoryStream = new MemoryStream(data);
  128. Device.BeginInvokeOnMainThread(() =>
  129. {
  130. pdfViewerControl.LoadDocument(memoryStream);
  131. });
  132. }
  133. public void LoadPDF(List<Guid> docids)
  134. {
  135. Task.Run(async () =>
  136. {
  137. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  138. {
  139. try
  140. {
  141. List<byte> bytes = new List<byte>();
  142. foreach (var docid in docids)
  143. {
  144. CoreTable table2 = new Client<Document>().Query
  145. (
  146. new Filter<Document>(x => x.ID).IsEqualTo(docid)
  147. );
  148. if (table2.Rows.Any())
  149. {
  150. Document doc = table2.Rows.First().ToObject<Document>();
  151. byte[] data = table2.Rows.First().Get<Document, byte[]>(x => x.Data);
  152. List<Byte> bytes1 = data.ToList();
  153. bytes.AddRange(bytes1);
  154. }
  155. }
  156. byte[] allData = bytes.ToArray();
  157. MemoryStream memoryStream = new MemoryStream(allData);
  158. Device.BeginInvokeOnMainThread(() =>
  159. {
  160. try
  161. {
  162. pdfViewerControl.LoadDocument(memoryStream);
  163. }
  164. catch { }
  165. });
  166. }
  167. catch (Exception ex)
  168. {
  169. MobileLogging.Log(ex);
  170. }
  171. }
  172. });
  173. }
  174. #endregion
  175. async void PrintBtn_Clicked(object sender, EventArgs e)
  176. {
  177. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  178. {
  179. pdfViewerControl.Print(doc.FileName);
  180. }
  181. }
  182. async void ShareBtn_Clicked(object sender, EventArgs e)
  183. {
  184. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  185. {
  186. var folder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  187. string path = Path.Combine(folder, doc.FileName);
  188. File.WriteAllBytes(path, doc.Data);
  189. ShareFile file = new ShareFile(path);
  190. await Share.RequestAsync(new ShareFileRequest
  191. {
  192. Title = "Share PDF",
  193. File = file
  194. });
  195. }
  196. }
  197. }
  198. }