Forum Xamarin.Mac

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

PDF WebView Custom Renderer for Mac with HTMLViewer not working

I had to display pdf files in the assets folder of my MAC application using Xamarin.Forms. For UWP platform it was straight forward using the https://stackoverflow.com/questions/54240261/displaying-pdf-files-from-internet-in-xamarin-forms-uwp

I have to provide similar support for MAC using the Custom Renderer.

I tried below code
class PDFWebRenderer : WebViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
PDFWebView pdfView = Element as PDFWebView;
pdfView.Source = new Uri(NSBundle.MainBundle.BundlePath + "/Contents/Resources/Assets/pdfs/" + WebUtility.UrlEncode(pdfView.FileName));
}
}
}

The above code loads the PDF file. But doesn't display have the toolbar similar to the UWP link as mentioned above. That is because I have to pass the PDF file path to the HTMLViewer.

In UWP, it is passed as below:
Control.Source = new Uri(string.Format("ms-appx-web:///Assets/pdfjs/web/viewer.html?file={0}", string.Format ("ms-appx-web:///Assets/Content/{0}", WebUtility.UrlEncode(customWebView.Uri))));

I tried a similar approach for MAC as well. But it's not loading the PDF with Viewer.html.

pdfView.Source = new Uri(NSBundle.MainBundle.BundlePath + "/Contents/Resources/Assets/pdfjs/web/viewer.html?file=file://" + NSBundle.MainBundle.BundlePath + "/Contents/Resources/Assets/pdfjs/web/" + WebUtility.UrlEncode(pdfView.FileName));

Any help would be appreciated.

Sign In or Register to comment.