Hello,
There are four reasons for this problem.
- The pdfjs version used is incorrect.
- The MAUI file URL is not converted to an Android-specific URI.
- Javascript is not enabled for webview.
- The file path of pdfjs is incorrect.
You could follow the steps below to display your local files in MAUI.
Step 1. Enable JavaScript for webview
, which you can do in the page's constructor.
public MainPage()
{
InitializeComponent();
Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
{
#if ANDROID
handler.PlatformView.Settings.JavaScriptEnabled = true;
handler.PlatformView.Settings.AllowFileAccess = true;
handler.PlatformView.Settings.AllowFileAccessFromFileURLs = true;
handler.PlatformView.Settings.AllowUniversalAccessFromFileURLs = true;
#endif
});
}
Step 2. Download the PDF.js file for version 2.14.305
. After testing, the latest version of PDF.js will have an error that the PDF file cannot be displayed.
Step 3. Check that your pdfjs path is correct. For example, if you move the build
and web
folder directly to the Raw
folder, pdfjs
is not needed in the path. file:///android_asset/web/viewer.html?
Step 4. Change the URL to the URI of the Android platform.
#if ANDROID
Android.Net.Uri uri = Android.Net.Uri.FromFile(new Java.IO.File(path));
string pdfpath = string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", uri);
pdfviewer.Source = new UrlWebViewSource { Url = pdfpath };
#endif
Best Regards,
Alec Liu.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.