I try to found the way for displaying local pdf files on xamarin.forms. I found a solution with implementing custom implementation of webview and its render: pdf reader
The main code is:
public class CustomWebView : WebView
{
public static readonly BindableProperty UriProperty = BindableProperty.Create<CustomWebView, string>(p => p.Uri, default(string));
public string Uri
{
get { return (string)GetValue(UriProperty); }
set { SetValue(UriProperty, value); }
}
}
Render:
[assembly: ExportRenderer (typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace DisplayPDF.iOS
{
public class CustomWebViewRenderer : ViewRenderer<CustomWebView, UIWebView>
{
protected override void OnElementChanged (ElementChangedEventArgs<CustomWebView> e)
{
base.OnElementChanged (e);
if (Control == null) {
SetNativeControl (new UIWebView ());
}
if (e.OldElement != null) {
// Cleanup
}
if (e.NewElement != null) {
var customWebView = Element as CustomWebView;
string fileName = Path.Combine (NSBundle.MainBundle.BundlePath, string.Format ("Content/{0}", WebUtility.UrlEncode (customWebView.Uri)));
Control.LoadRequest (new NSUrlRequest (new NSUrl (fileName, false)));
Control.ScalesPageToFit = true;
}
}
}
}
And my page:
public class WebViewPageCS : ContentPage
{
public WebViewPageCS ()
{
webView = new CustomWebView
{
Uri = "ScalaReference.pdf",
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
}
But now I can't find the way to add anchor to this pdf file like described at this post: anchor to pdf
Also I tried to use this code for evaluate script:
private async void Scroll_Clicked(object sender, EventArgs e)
{
webView.Eval($"window.scrollTo(0, {x});");
}
it works fine with default webview but not with custom one.
Maybe someone know any another way to scroll / set anchor to pdf and link to this anchor via xamarin.forms? Thank you.