PDFViewer.xaml.cs 7.1 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 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 PRS.Mobile
  18. {
  19. [XamlCompilation(XamlCompilationOptions.Compile)]
  20. public partial class PDFViewer
  21. {
  22. Document doc = new Document();
  23. bool externalOpened = false;
  24. public bool AllowPrintShare { get; set; }
  25. public PDFViewer(Guid id)
  26. {
  27. InitializeComponent();
  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. LoadPDF(id);
  36. }
  37. public PDFViewer(MemoryStream stream)
  38. {
  39. InitializeComponent();
  40. pdfViewerControl.LoadDocument(stream);
  41. }
  42. private void SetupPrintShare()
  43. {
  44. _share.IsVisible = AllowPrintShare;
  45. _print.IsVisible = AllowPrintShare;
  46. }
  47. void Exit_Clicked(object sedner, EventArgs e)
  48. {
  49. Navigation.PopAsync();
  50. }
  51. protected override void OnAppearing()
  52. {
  53. base.OnAppearing();
  54. if (externalOpened)
  55. Navigation.PopAsync();
  56. }
  57. #region Load PDF
  58. public async void LoadPDF(Guid docid)
  59. {
  60. try
  61. {
  62. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  63. {
  64. CoreTable table = QueryDocument(docid);
  65. while (table == null)
  66. table = QueryDocument(docid);
  67. if (table.Rows.Any())
  68. {
  69. doc.FileName = table.Rows.First().Values[0].ToString();
  70. if (doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf"))
  71. {
  72. byte[] data = table.Rows.First().Get<Document, byte[]>(x => x.Data);
  73. //if (Device.RuntimePlatform.Equals(Device.Android) && PRSSecurity.IsAllowed<CanOpenMobileNativePDFViewer>())
  74. // OpenNativeViewer(doc.FileName, data);
  75. //else
  76. ShowPDF(data);
  77. }
  78. else
  79. {
  80. Device.BeginInvokeOnMainThread(() =>
  81. {
  82. DisplayAlert("Error", "File type is not PDF", "OK");
  83. Navigation.PopAsync();
  84. });
  85. }
  86. }
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. MobileLogging.Log(ex);
  92. }
  93. }
  94. private CoreTable QueryDocument(Guid docid)
  95. {
  96. try
  97. {
  98. return new Client<Document>().Query
  99. (
  100. new Filter<Document>(x => x.ID).IsEqualTo(docid),
  101. new Columns<Document>(ColumnTypeFlags.None).Add(x => x.FileName, x => x.Data)
  102. );
  103. }
  104. catch (Exception ex)
  105. {
  106. MobileLogging.Log(ex);
  107. return null;
  108. }
  109. }
  110. private async void OpenNativeViewer(string filename, byte[] data)
  111. {
  112. var filePath = Path.Combine(FileSystem.AppDataDirectory, filename);
  113. File.WriteAllBytes(filePath, data);
  114. await Launcher.OpenAsync(new OpenFileRequest
  115. {
  116. File = new ReadOnlyFile(filePath)
  117. });
  118. }
  119. void ShowPDF(byte[] data)
  120. {
  121. MemoryStream memoryStream = new MemoryStream(data);
  122. Device.BeginInvokeOnMainThread(() =>
  123. {
  124. pdfViewerControl.LoadDocument(memoryStream);
  125. });
  126. }
  127. public void LoadPDF(List<Guid> docids)
  128. {
  129. Task.Run(async () =>
  130. {
  131. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  132. {
  133. try
  134. {
  135. List<byte> bytes = new List<byte>();
  136. foreach (var docid in docids)
  137. {
  138. CoreTable table2 = new Client<Document>().Query
  139. (
  140. new Filter<Document>(x => x.ID).IsEqualTo(docid)
  141. );
  142. if (table2.Rows.Any())
  143. {
  144. Document doc = table2.Rows.First().ToObject<Document>();
  145. byte[] data = table2.Rows.First().Get<Document, byte[]>(x => x.Data);
  146. List<Byte> bytes1 = data.ToList();
  147. bytes.AddRange(bytes1);
  148. }
  149. }
  150. byte[] allData = bytes.ToArray();
  151. MemoryStream memoryStream = new MemoryStream(allData);
  152. Device.BeginInvokeOnMainThread(() =>
  153. {
  154. try
  155. {
  156. pdfViewerControl.LoadDocument(memoryStream);
  157. }
  158. catch { }
  159. });
  160. }
  161. catch (Exception ex)
  162. {
  163. MobileLogging.Log(ex);
  164. }
  165. }
  166. });
  167. }
  168. #endregion
  169. async void PrintBtn_Clicked(object sender, EventArgs e)
  170. {
  171. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  172. {
  173. pdfViewerControl.Print(doc.FileName);
  174. }
  175. }
  176. async void ShareBtn_Clicked(object sender, EventArgs e)
  177. {
  178. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  179. {
  180. var folder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  181. string path = Path.Combine(folder, doc.FileName);
  182. File.WriteAllBytes(path, doc.Data);
  183. ShareFile file = new ShareFile(path);
  184. await Share.RequestAsync(new ShareFileRequest
  185. {
  186. Title = "Share PDF",
  187. File = file
  188. });
  189. }
  190. }
  191. }
  192. }