PDFViewer.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. ShowPDF(data);
  79. }
  80. else
  81. {
  82. Device.BeginInvokeOnMainThread(() =>
  83. {
  84. DisplayAlert("Error", "File type is not PDF", "OK");
  85. Navigation.PopAsync();
  86. });
  87. }
  88. }
  89. }
  90. }
  91. catch { }
  92. }
  93. void ShowPDF(byte[] data)
  94. {
  95. MemoryStream memoryStream = new MemoryStream(data);
  96. Device.BeginInvokeOnMainThread(() =>
  97. {
  98. pdfViewerControl.LoadDocument(memoryStream);
  99. });
  100. }
  101. public void LoadPDF(List<Guid> docids)
  102. {
  103. Task.Run(async () =>
  104. {
  105. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  106. {
  107. List<byte> bytes = new List<byte>();
  108. foreach (var docid in docids)
  109. {
  110. CoreTable table2 = new Client<Document>().Query
  111. (
  112. new Filter<Document>(x => x.ID).IsEqualTo(docid)
  113. );
  114. if (table2.Rows.Any())
  115. {
  116. Document doc = table2.Rows.First().ToObject<Document>();
  117. byte[] data = table2.Rows.First().Get<Document, byte[]>(x => x.Data);
  118. List<Byte> bytes1 = data.ToList();
  119. bytes.AddRange(bytes1);
  120. }
  121. }
  122. byte[] allData = bytes.ToArray();
  123. MemoryStream memoryStream = new MemoryStream(allData);
  124. Device.BeginInvokeOnMainThread(() =>
  125. {
  126. pdfViewerControl.LoadDocument(memoryStream);
  127. });
  128. }
  129. });
  130. }
  131. #endregion
  132. async void PrintBtn_Clicked(object sender, EventArgs e)
  133. {
  134. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  135. {
  136. pdfViewerControl.Print(doc.FileName);
  137. }
  138. }
  139. async void ShareBtn_Clicked(object sender, EventArgs e)
  140. {
  141. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  142. {
  143. var folder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  144. string path = Path.Combine(folder, doc.FileName);
  145. File.WriteAllBytes(path, doc.Data);
  146. ShareFile file = new ShareFile(path);
  147. await Share.RequestAsync(new ShareFileRequest
  148. {
  149. Title = "Share PDF",
  150. File = file
  151. });
  152. }
  153. }
  154. }
  155. }