Search code examples
c#webviewuwppdf.jsstoragefile

UWP send file data to WebView


I have an app coded in C#, but I would like to show a PDF file in a WebView with pdf.js. The APIs that the UWP provides for viewing PDFs are too limited for me, they do not support things like text selection and it can be very slow with large files. Is it possible (if so, what would be the best way to) open a file (StorageFile) using pdf.js inside a WebView inside a C# UWP app?


Solution

  • Is it possible (if so, what would be the best way to) open a file (StorageFile) using pdf.js inside a WebView inside a C# UWP app?

    The short answer is yes, and there is already such control, but it only works in Xamarin.Forms. You can port it to UWP easily.

    Step 1

    Import pdfjs JavaScript package to your UWP project.

    enter image description here

    Step 2

    Instantiate a WebView control that used to display pdfjs index page.

    <WebView Name="PDFViewer"/>
    

    Step 3

    Create PDFViewer source that contain pdf file absolute path.

    PDFViewer.Source = new Uri(string.Format("ms-appx-web:///pdfjs/web/viewer.html?file={0}", string.Format("ms-appx-web:///Assets/{0}", WebUtility.UrlEncode("hello.pdf"))));
    

    enter image description here