Skip to content

Instantly share code, notes, and snippets.

@jfversluis
Last active December 24, 2017 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfversluis/0a35e9e811542cc085093a74137d3c20 to your computer and use it in GitHub Desktop.
Save jfversluis/0a35e9e811542cc085093a74137d3c20 to your computer and use it in GitHub Desktop.
Code-behind for our UI page
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using pdfjs.Interfaces;
using Xamarin.Forms;
namespace pdfjs
{
public partial class pdfjsPage : ContentPage
{
public pdfjsPage(string url)
{
InitializeComponent();
var localPath = string.Empty;
if (Device.RuntimePlatform == Device.Android)
{
var dependency = DependencyService.Get<ILocalFileProvider>();
if (dependency == null)
{
DisplayAlert("Error loading PDF", "Computer says no", "OK");
return;
}
var fileName = Guid.NewGuid().ToString();
// Download PDF locally for viewing
using (var httpClient = new HttpClient())
{
var pdfStream = Task.Run(() => httpClient.GetStreamAsync(url)).Result;
localPath =
Task.Run(() => dependency.SaveFileToDisk(pdfStream, $"{fileName}.pdf")).Result;
}
if (string.IsNullOrWhiteSpace(localPath))
{
DisplayAlert("Error loading PDF", "Computer says no", "OK");
return;
}
}
if (Device.RuntimePlatform == Device.Android)
PdfView.Source = $"file:///android_asset/pdfjs/web/viewer.html?file={WebUtility.UrlEncode(localPath)}";
else
PdfView.Source = url;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment