How to get the current page number or check if PDF viewer is on the Last page of a PDF in WPF Webview2 control?

Praveena S Kumar 0 Reputation points
2025-01-23T07:01:02.52+00:00

I am working on a WPF application where I use Webview2 to display PDF files. I rely on Edge's built in PDF rendering mechanism and am not using any external PDF libraries. I've successfully loaded the PDF using the following code:

PdfWebView.CoreWebView2.Navigate($"file:///{pdfFilePath.Replace("\", "/")}#toolbar=0");

Now I need to

  1. Retrieve the current page number of the PDF being viewed.
  2. Determine if the user is viewing the last page of the PDF

I haven't found any direct way to achieve this. I am aware we can execute JavaScript inside Webview2, but I don't think Edge PDF viewer exposes properties or methods (currentPageNumber, pagecount) that can help. Could someone guide me on this.

Any guidance or code examples would be greatly appreciated.

Thank you

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,819 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,244 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 70,216 Reputation points
    2025-01-23T16:46:46.5033333+00:00

    the webview does not directly support pdf viewing. it loads a registered app to display PDF. the registered app reads the data out of a cache. there are no exposed methods to the webview and as its a separate app, no javascript can be run to access it.

    you could use a javascript application like pdf.js to render the PDF and capture display events:

    https://github.com/mozilla/pdf.js

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.