2

I have written HTML file which contains link to some other websites. I used

HtmlWebViewSource html = new HtmlWebViewSource();

to render and display HTML contents.

Now, I have links of URLs in that HTML and when I click the link, it opens default browser and opens the website. I used:

Device.OpenUri(new Uri(args.Url));

Now, the problem is I would like to have a link in that HTML and upon clicking that link, it should open embedded PDF file.

Is there any way I could achieve this from my code design OR do I have to re-write different approach. And if any best approach available, can you please suggest it?

enter image description here

Tuk Tuk
  • 337
  • 1
  • 2
  • 11
  • You appear to already know how to render HTML and intercept a link click from a webview. So are you just asking how to open a PDF? – Jason Apr 29 '20 at 16:48
  • Yes. How do i put link to PDF file inside HTML? I could just put the link there but Device.Uri will thrown an error if I do that. – Tuk Tuk Apr 29 '20 at 16:49
  • your link handler can check if the url is a PDF and call a PDF viewer instead. There are already many existing questions that address different methods to display a PDF in your app – Jason Apr 29 '20 at 16:53

2 Answers2

3

In iOS, you can use WKWebView or QLPreviewController or load pdf files.

In Android, you can open the pdf file through:

    Android.Net.Uri uri = Android.Net.Uri.FromFile(file);
    Intent intent = new Intent(Intent.ActionView);
    intent.SetDataAndType(uri, application);
    intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);

    Forms.Context.StartActivity(intent);

You need to create a dependency-service and implement the open pdf method in each platform project. Then call it when click openpdf link in Xamarin.forms project.

Here are several threads you can check:

opening-a-pdf-with-xamarin-forms

how-to-open-pdf-or-txt-file-in-default-app-on-xamarin-forms

how-to-view-pdf-file-using-xamarin-forms

nevermore
  • 15,432
  • 1
  • 12
  • 30
1

I suggest you to use a webview in order to not change your code too much, and works very good, just see this post first. Otherwise, there are some third party paid frameworks like Telerik with great controls.

manuelrb98
  • 344
  • 2
  • 8