How to Open Local PDF file in WebView in .NET MAUI

Mane, PRASHANT GORAKH 101 Reputation points

I have created sample .NET MAUI app for display local pdf file. I have kept my kayprotect_training.pdf file in the Resources/Raw/folder and set build action of kayprotect_training.pdf is MauiAsset. after that I create a pdfjs folder in the Resources/Raw/, then copy build and web content from pdfjs to the Resources/Raw/pdfjs,


            <WebView x:Name="PdfWebView"/>
private void LoadPdfViewer()
        string pdfFilePath = "file:///android_asset/pdfjs/web/viewer.html?file=file:///android_asset/pdfjs/web/kayprotect_training.pdf";
        PdfWebView.Source = new UrlWebViewSource { Url = pdfFilePath };

but still unable to see the pdf file

instead of that I can see below error
User's image

even I tried to open below url in webview but still unable to see it.

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,206 questions
{count} vote

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 38,906 Reputation points Microsoft Vendor


    Your WebView is getting a file not found error because you can't use the Android file path format directly in MAUI.

    There is already a full discussion and sample of how to present a PDF in MAUI in GitHub, please refer to The Webview does not display PDF #7399.


    After testing your project with pdf.js to investigate. Please follow the steps below to display your local PDF file using pdf.js.

    Step 1. 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 2. Create a pdfjs folder in the Raw folder in your MAUI project and copy the web and build folders to the pdfjs folder.

    Step 3. Set your pdf file properties to MauiAssert.

    Step 4. Refer to the following code to display a pdf file using a WebView.

    <WebView x:Name="web_view" HeightRequest="500" WidthRequest="500"/>
            public MainPage()
                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;
    #elif IOS
            private void LoadPdfViewer()
                string pdfFilePath = $"file:///android_asset/pdfjs/web/viewer.html?file=file:///android_asset/test.pdf";
                web_view.Source = new UrlWebViewSource { Url = pdfFilePath };

    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.

0 additional answers

Sort by: Most helpful