123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 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<ICustomPdfRendererService>().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<Document>().Query
- (
- new Filter<Document>(x => x.ID).IsEqualTo(docid),
- new Columns<Document>(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<Document, byte[]>(x => x.Data);
- if (Device.RuntimePlatform.Equals(Device.Android) && PRSSecurity.IsAllowed<CanOpenMobileNativePDFViewer>())
- 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<Guid> docids)
- {
- Task.Run(async () =>
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
- {
- List<byte> bytes = new List<byte>();
- foreach (var docid in docids)
- {
- CoreTable table2 = new Client<Document>().Query
- (
- new Filter<Document>(x => x.ID).IsEqualTo(docid)
- );
- if (table2.Rows.Any())
- {
- Document doc = table2.Rows.First().ToObject<Document>();
- byte[] data = table2.Rows.First().Get<Document, byte[]>(x => x.Data);
- List<Byte> 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
- });
- }
- }
- }
- }
|