PDFViewer.xaml.cs 6.3 KB

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