using Comal.Classes; using InABox.Clients; using InABox.Core; using Syncfusion.SfPdfViewer.XForms; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Xamarin.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI.Dialogs; using PRSSecurity = InABox.Core.Security; namespace comal.timesheets { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class PDFViewer : ContentPage { Document doc = new Document(); bool externalOpened = false; public PDFViewer(Guid id, bool allowPrintShare = false) { InitializeComponent(); NavigationPage.SetHasBackButton(this, false); if (Device.RuntimePlatform.Equals(Device.Android)) { pdfViewerControl.CustomPdfRenderer = DependencyService.Get().AlternatePdfRenderer; pdfViewerControl.MaximumZoomPercentage = 600; } else pdfViewerControl.MaximumZoomPercentage = 50000; if (allowPrintShare) { shareBtn.IsVisible = true; printBtn.IsVisible = true; } LoadPDF(id); } public PDFViewer(MemoryStream stream, bool allowPrintShare = false) { InitializeComponent(); NavigationPage.SetHasBackButton(this, false); if (allowPrintShare) { shareBtn.IsVisible = true; printBtn.IsVisible = true; } pdfViewerControl.LoadDocument(stream); } void Exit_Clicked(object sedner, EventArgs e) { Navigation.PopAsync(); } protected override void OnAppearing() { base.OnAppearing(); if (externalOpened) Navigation.PopAsync(); } #region Load PDF public async void LoadPDF(Guid docid) { try { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { CoreTable table2 = new Client().Query ( new Filter(x => x.ID).IsEqualTo(docid), new Columns(x => x.FileName, x => x.Data) ); if (table2.Rows.Any()) { doc.FileName = table2.Rows.First().Values[0].ToString(); if (doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf") || doc.FileName.EndsWith("pdf")) { byte[] data = table2.Rows.First().Get(x => x.Data); if (Device.RuntimePlatform.Equals(Device.Android) && PRSSecurity.IsAllowed()) OpenNativeViewer(doc.FileName, data); else ShowPDF(data); } else { Device.BeginInvokeOnMainThread(() => { DisplayAlert("Error", "File type is not PDF", "OK"); Navigation.PopAsync(); }); } } } } catch { } } private async void OpenNativeViewer(string filename, byte[] data) { var filePath = Path.Combine(FileSystem.AppDataDirectory, filename); File.WriteAllBytes(filePath, data); await Launcher.OpenAsync(new OpenFileRequest { File = new ReadOnlyFile(filePath) }); } void ShowPDF(byte[] data) { MemoryStream memoryStream = new MemoryStream(data); Device.BeginInvokeOnMainThread(() => { pdfViewerControl.LoadDocument(memoryStream); }); } public void LoadPDF(List docids) { Task.Run(async () => { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { List bytes = new List(); foreach (var docid in docids) { CoreTable table2 = new Client().Query ( new Filter(x => x.ID).IsEqualTo(docid) ); if (table2.Rows.Any()) { Document doc = table2.Rows.First().ToObject(); byte[] data = table2.Rows.First().Get(x => x.Data); List bytes1 = data.ToList(); bytes.AddRange(bytes1); } } byte[] allData = bytes.ToArray(); MemoryStream memoryStream = new MemoryStream(allData); Device.BeginInvokeOnMainThread(() => { pdfViewerControl.LoadDocument(memoryStream); }); } }); } #endregion async void PrintBtn_Clicked(object sender, EventArgs e) { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { pdfViewerControl.Print(doc.FileName); } } async void ShareBtn_Clicked(object sender, EventArgs e) { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { var folder = Environment.GetFolderPath(Environment.SpecialFolder.Personal); string path = Path.Combine(folder, doc.FileName); File.WriteAllBytes(path, doc.Data); ShareFile file = new ShareFile(path); await Share.RequestAsync(new ShareFileRequest { Title = "Share PDF", File = file }); } } } }