Skip to content

Instantly share code, notes, and snippets.

@ilyaqznetsow
Created July 25, 2019 17:22
Show Gist options
  • Save ilyaqznetsow/079cd0b3e3bf7054bd24b60b15aadcde to your computer and use it in GitHub Desktop.
Save ilyaqznetsow/079cd0b3e3bf7054bd24b60b15aadcde to your computer and use it in GitHub Desktop.
WebView Pdf
public class WebViewExtended : WebView {
public static BindableProperty TotalPagesProperty =
BindableProperty.Create (nameof (TotalPages), typeof (int), typeof (WebViewExtended));
public int TotalPages {
get => (int) GetValue (TotalPagesProperty);
set => SetValue (TotalPagesProperty, value);
}
public static BindableProperty CurrentPageProperty =
BindableProperty.Create (nameof (CurrentPage), typeof (int), typeof (WebViewExtended));
public int CurrentPage {
get => (int) GetValue (CurrentPageProperty);
set => SetValue (CurrentPageProperty, value);
}
}
//Android
//Just add Pdf.js in assets folder and name its folder PdfViewer
public class WebViewExtendedRenderer : WebViewRenderer {
public WebViewExtendedRenderer (Context context) : base (context) {
}
private readonly string _documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
private string _pdfPath;
private readonly string _pdfFileName = "thePDFDocument.pdf";
private string _pdfFilePath;
private readonly WebClient _webClient = new WebClient ();
protected override void OnElementChanged (ElementChangedEventArgs<WebView> e) {
base.OnElementChanged (e);
var settings = Control.Settings;
settings.JavaScriptEnabled = true;
settings.AllowFileAccessFromFileURLs = true;
settings.AllowUniversalAccessFromFileURLs = true;
settings.BuiltInZoomControls = true;
settings.DefaultZoom = WebSettings.ZoomDensity.Far;
Control.SetWebChromeClient (new WebChromeClient ());
DownloadPdfDocument ();
}
private void DownloadPdfDocument () {
_pdfPath = _documentsPath + "/PDFView";
_pdfFilePath = Path.Combine (_pdfPath, _pdfFileName);
// Check if the PDFDirectory Exists
if (!Directory.Exists (_pdfPath)) {
Directory.CreateDirectory (_pdfPath);
} else {
// Check if the pdf is there, If Yes Delete It. Because we will download the fresh one just in a moment
if (File.Exists (_pdfFilePath)) {
File.Delete (_pdfFilePath);
}
}
// This will be executed when the pdf download is completed
_webClient.DownloadDataCompleted += OnPdfDownloadCompleted;
// Lets downlaod the PDF Document
var url = new Uri (Control.Url);
_webClient.DownloadDataAsync (url);
}
private void OnPdfDownloadCompleted (object sender, DownloadDataCompletedEventArgs e) {
try {
// Okay the download's done, Lets now save the data and reload the webview.
var pdfBytes = e.Result;
File.WriteAllBytes (_pdfFilePath, pdfBytes);
Control.LoadUrl ("file:///android_asset/PdfViewer/index.html?file=" + _pdfFilePath);
} catch (Exception ep) {
Console.WriteLine (ep);
}
}
}
//iOS
public class WebViewExtendedRenderer : WkWebViewRenderer {
protected override void OnElementChanged (VisualElementChangedEventArgs e) {
base.OnElementChanged (e);
if (NativeView != null && e.NewElement != null) {
if (!(NativeView is WKWebView wKWebView))
return;
BackgroundColor = Colors.Gray.ToUIColor ();
ScrollView.Scrolled += ScrollViewOnScrolled;
ScrollView.BackgroundColor = Color.Gray.ToUIColor ();
wKWebView.BackgroundColor = Color.Gray.ToUIColor ();
}
}
//Get current page
void ScrollViewOnScrolled (object sender, EventArgs e) {
HidePageNumberView (this);
GetPages (this);
}
void GetPages (UIView view) {
if (!view.Subviews.Any ()) return;
foreach (var subView in view.Subviews) {
if (subView is UILabel label) {
if (!(Element is WebViewExtended webViewExtended)) return;
var text = label.Text;
if (!string.IsNullOrEmpty (text)) {
if (text.Length > text.LastIndexOf (" ", StringComparison.Ordinal))
webViewExtended.TotalPages =
Convert.ToInt32 (text.Substring (text.LastIndexOf (" ", StringComparison.Ordinal))
.Trim ());
webViewExtended.CurrentPage = Convert.ToInt32 (text.Substring (0,
text.IndexOf (" ", StringComparison.Ordinal))
.Trim ());
}
} else GetPages (subView);
}
}
void HidePageNumberView (UIView view) {
foreach (var subView in view.Subviews) {
if (subView.Description.Contains ("Backdrop")) {
subView.Hidden = true;
} else {
HidePageNumberView (subView);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment