Click or drag to resize

PdfViewer Class

The PDF Viewer control for WPF.
Inheritance Hierarchy
SystemObject
  System.Windows.ThreadingDispatcherObject
    System.WindowsDependencyObject
      System.Windows.MediaVisual
        System.WindowsUIElement
          System.WindowsFrameworkElement
            System.Windows.ControlsControl
              System.Windows.ControlsContentControl
                System.Windows.ControlsUserControl
                  PdfViewer4WinNet.WpfPdfViewer

Namespace: PdfViewer4WinNet.Wpf
Assembly: PdfViewerWinNet (in PdfViewerWinNet.dll) Version: 10.0
Syntax
public class PdfViewer : UserControl, IDisposable

The PdfViewer type exposes the following members.

Constructors
 NameDescription
Public methodPdfViewer Creates a PdfViewer object
Top
Properties
 NameDescription
Public propertyBrowserExecutableFolder The folder of a fixed version WebView2 Runtime to use instead of the installed Evergreen runtime. Set it before the control is loaded. When not set, a fixed version runtime in a "WebView2Runtime" folder next to the application is used (WebView2Runtime\x64 or WebView2Runtime\x86 for the bitness of the process, or the folder Microsoft's .cab file expands to inside it); without one, the installed Evergreen runtime.
Public propertyCanAssembleDocument Can assemble the document (create bookmarks, thumbnail images or insert, rotate, delete pages). This property has effect when a 128 bit key is used. The default value is true.
Public propertyCanCopyContent Can copy the document content. The viewer honours this permission. The default value is true.
Public propertyCanEditAnnotations Can edit PDF document annotations. The default value is true.
Public propertyCanEditContent Can edit PDF document content. The default value is true.
Public propertyCanFillFormFields Can fill the form fields. This property has effect when a 128 bit key is used. The default value is true.
Public propertyCanPrint Can print the document. The viewer honours this permission. The default value is true.
Public propertyCoreWebView2 The Microsoft Edge WebView2 instance that displays the viewer, for advanced customization (for example browser settings). It is null until the viewer has started; the viewer itself is driven through the members of this control.
Public propertyCurrentPage Gets the page shown in the viewer, or goes to the specified page. The first page in a document is page 1. Set before a document is loaded, the document opens at that page. -1 means the viewer shows the first page.
Public propertyDocumentDisplayMode Gets or sets the display mode of the document to show the page thumbnails, the outline (bookmarks) or the attachments next to the document. The default value is NotSet (the document's own page mode).
Public propertyInstallWebView2RuntimeIfMissing Installs the Evergreen Microsoft Edge WebView2 Runtime when it is missing on the computer: the viewer runs Microsoft's installer (WebView2BootstrapperPath) for the current user, silently, then starts. The default value is false: the viewer shows a message with the download address instead. Set it before the control is loaded.
Public propertyIsDocumentLoaded True when a document is loaded and displayed in the viewer (after DocumentLoaded).
Public propertyKeySize The dimension of the encryption key. By default a 128 bit key is used.
Public propertyLicenseKey Gets or sets the license key
Public propertyOpenPassword The password of a password protected PDF document. It opens the document without asking the user, and it is used to change the viewer preferences and security options of a protected document. When it is not set (or not correct), PasswordRequired is raised; when the application does not give the password there, the viewer asks the user for it.
Public propertyOwnerPassword The password required to change permissions of the PDF document like printing or editing. The default value is an empty string. If the OwnerPassword property is an empty string and the UserPassword is not empty, an owner password is generated, so the permissions cannot be changed by opening the document with the user password.
Public propertyPageCount Gets the page count of the currently loaded PDF document, or -1 when no document is loaded.
Public propertyPageFitMode Gets or sets the fit mode of the PDF page in the viewer. The default value is NotSet.
Public propertyPageLayoutMode Gets or sets the layout mode for a page view. The default value is NotSet.
Public propertyPdfSecurityOptions Gets a reference to the object controlling the PDF document security settings like user and owner password, and restricting printing or copying of the displayed document. The viewer honours the printing and copying permissions. The settings take effect for the next document loaded (or after ReloadPdfDocument).
Public propertyPdfViewerPreferences Gets a reference to the object controlling the PDF document viewer preferences. The properties of this object allow you to hide the toolbar and the navigation panel, and to change the page mode and the page layout of the document. They are written into the displayed document, and take effect for the next document loaded (or after ReloadPdfDocument).
Public propertyShowDocumentPropertiesButton Show or hide the document properties command of the viewer. The default value is true.
Public propertyShowDownloadButton Show or hide the download (save a copy) button of the toolbar (and Ctrl+S). The default value is true.
Public propertyShowFindButton Show or hide the find button of the toolbar and its find bar (and Ctrl+F). Find(String, Boolean, Boolean) works either way. The default value is true.
Public propertyShowNavigationPanel Show or hide the navigation panel on the left side of the viewer, with the page thumbnails, the document outline (bookmarks) and the attachments. The default value of this property is true. This property is a shortcut for the PdfViewerPreferences.HideWindowUI.
Public propertyShowOpenButton Show or hide the open file command of the viewer (in the tools menu of the toolbar; Ctrl+O): the user can open another PDF file from the computer in the viewer. A document the user opens is only displayed: ReloadPdfDocument reloads the document loaded by the application. The default value is false.
Public propertyShowPageNavigationButtons Show or hide the page navigation of the toolbar (previous page, next page, the page number box and the first and last page commands). The default value is true.
Public propertyShowPresentationModeButton Show or hide the presentation mode (full screen) command of the viewer. The default value is true.
Public propertyShowPrintButton Show or hide the print button of the toolbar (and Ctrl+P). The print methods of the control work either way; printing also depends on the document permissions (CanPrint). The default value is true.
Public propertyShowScrollbars Show or hide the viewer scrollbars. The document can still be scrolled with the mouse wheel and the keyboard. The default value is true.
Public propertyShowSidebarButton Show or hide the toolbar button that opens and closes the navigation panel (the panel itself can still be shown with DocumentDisplayMode). The default value is true.
Public propertyShowToolbarMode Show or hide the viewer toolbar. The default value is NotSet (the toolbar is shown).
Public propertyShowZoomButtons Show or hide the zoom buttons and the zoom list of the toolbar. The default value is true.
Public propertySource The path of the PDF file displayed in the viewer. Setting it loads the file (like LoadFile(String)).
Public propertyUserDataFolder The folder where the viewer keeps its browser data (cache, settings). Set it before the control is loaded. When not set, a folder under the local application data folder (ExpertPdf\PdfViewer\WebView2) is used. A viewer that uses a fixed version runtime gets a folder of its own (WebView2-<hash> next to it): WebView2 cannot share one folder between runtimes. When the folder is in use by WebView2 with other settings (another application), the viewer uses a folder of the application's own, and a viewer whose window has another DPI awareness than the first viewer of the process (a Windows Forms application becomes DPI aware when it first shows WPF content) uses WebView2-dpi-<awareness> next to it. A folder set here is always used as it is: give viewers with different settings different folders.
Public propertyUserPassword The password required to open the PDF document. The viewer asks the user for it. The default value is an empty string.
Public propertyViewerTheme Gets or sets the color theme of the viewer: light, dark, or as set in Windows (the default). The pages of the document keep their own colors.
Public propertyStatic memberWebView2BootstrapperPath The Microsoft Edge WebView2 Runtime installer (bootstrapper) used by InstallWebView2Runtime and InstallWebView2RuntimeIfMissing. The default value is MicrosoftEdgeWebview2Setup.exe in the application folder, where the NuGet package of the viewer puts it.
Public propertyZoomPercentage Gets or sets the zoom percentage of the viewer. For example, 100 represents a magnification of 100%. Reading it gives the zoom of the viewer (it changes when the user zooms, or with the fit modes). A negative value means the default zoom is used. The default value is -1.
Top
Methods
 NameDescription
Public methodClose Releases the resources used by this control
Public methodCloseDocument Closes the document displayed in the viewer.
Public methodDispose Releases the resources used by this control (the browser instance of the viewer).
Public methodFind(String) Searches the displayed document for a text (not case sensitive, also inside words). See Find(String, Boolean, Boolean).
Public methodFind(String, Boolean, Boolean) Searches the displayed document for a text, highlights every match and scrolls the first match from the current page on into view. SearchCompleted is raised with the number of matches when the whole document is searched. An empty text removes the highlights of the previous search.
Public methodFindNext Selects the next match of the last search (after the last match, the first one) and scrolls it into view. SearchCompleted is raised with the selected match.
Public methodFindPrevious Selects the previous match of the last search (before the first match, the last one) and scrolls it into view. SearchCompleted is raised with the selected match.
Public methodStatic memberGetPageCount(Stream) A fast method to obtain the page count of a specified PDF document.
Public methodStatic memberGetPageCount(String) A fast method to obtain the page count of a specified PDF document.
Public methodGoBackwardStack Goes to the previous view in the navigation history (for example back from a link), if it exists.
Public methodGoForwardStack Goes to the next view in the navigation history, if it exists.
Public methodGoToFirstPage Goes to the first page in the document.
Public methodGoToLastPage Goes to the last page in the document.
Public methodGoToNextPage Goes to the next page in the document, if it exists.
Public methodGoToPreviousPage Goes to the previous page in the document, if it exists.
Public methodStatic memberInstallWebView2Runtime Installs the Evergreen Microsoft Edge WebView2 Runtime for the current user with Microsoft's installer (WebView2BootstrapperPath, run silently; it downloads the runtime) and waits for it, for up to 5 minutes. Returns true when the installer succeeded and the runtime is installed. Call it from a background thread: it blocks while the runtime is installed.
Public methodLoadBytes Opens and displays the PDF document in the specified byte array.
Public methodLoadFile Opens and displays the specified PDF file.
Public methodLoadStream Opens and displays the PDF document in the specified stream. The stream is read when the method is called.
Protected methodOnCurrentPageChanged Raises the CurrentPageChanged event.
Protected methodOnDocumentLoaded Raises the DocumentLoaded event.
Protected methodOnLoadError Raises the LoadError event.
Protected methodOnPasswordRequired Raises the PasswordRequired event.
Protected methodOnSearchCompleted Raises the SearchCompleted event.
Public methodPrint Prints the document according to the options selected in the print dialog. This method returns immediately, even if the printing has not completed. NOTE: If security settings do not allow printing, this method will be ignored.
Public methodPrintAll Prints the entire document on the default printer without displaying a dialog. This method returns immediately, even if the printing has not completed. Use PrintAsync(Int32, Int32, String) to know the result.
Public methodPrintAllFit Prints the entire document on the default printer without displaying a dialog. This method returns immediately, even if the printing has not completed.
Public methodPrintAsync Prints pages of the document without displaying a dialog.
Public methodPrintPages Prints the specified pages on the default printer without displaying a dialog. This method returns immediately, even if the printing has not completed.
Public methodPrintPagesFit Prints the specified pages on the default printer without displaying a dialog. This method returns immediately, even if the printing has not completed.
Public methodPrintWithDialog Prints the document according to the options selected in the print dialog. This method returns immediately, even if the printing has not completed. NOTE: If security settings do not allow printing, this method will be ignored.
Public methodReloadPdfDocument Call this method to reload the currently loaded PDF document after changing the PdfViewerPreferences or the PdfSecurityOptions.
Public methodSetCurrentHighlight Highlights the specified rectangle on the current page. A rectangle with no width or height removes the highlight.
Public methodSetCurrentPage Goes to the specified page in the document.
Public methodSetLayoutMode Sets the layout mode for a page view according to the specified string.
Public methodSetNamedDest Changes the page view to the named destination in the specified string.
Public methodSetPageMode Sets the page mode according to the specified string.
Public methodSetShowScrollbars Determines whether scrollbars will appear in the document view.
Public methodSetShowToolbar Determines whether the toolbar will appear in the viewer.
Public methodSetView Sets the view of a page according to the specified string.
Public methodSetViewRect Scrolls and zooms the current page so that the specified rectangle fills the view.
Public methodSetViewScroll Sets the view of a page according to the specified string, and scrolls the page by the specified offset.
Public methodSetZoom Sets the magnification according to the specified value.
Public methodSetZoomScroll Sets the magnification according to the specified value, and scrolls the current page so that the specified point is at the top left corner of the view.
Top
Events
 NameDescription
Public eventCurrentPageChanged Raised when the page shown in the viewer changes (CurrentPage).
Public eventDocumentLoaded Raised when a document loaded with LoadFile(String), LoadStream(Stream) or LoadBytes(Byte) is displayed in the viewer. PageCount holds the number of pages.
Public eventLoadError Raised when the viewer cannot load or display a document, or when the WebView2 Runtime is missing.
Public eventPasswordRequired Raised when the document being opened is password protected and the viewer needs its password: when no OpenPassword is set, and again when the password given was not correct (IsPasswordIncorrect). Set Password to open the document, or Cancel to stop opening it. When no password is set (or the event is not handled), the viewer asks the user for the password.
Public eventSearchCompleted Raised when a search is complete: after Find(String, Boolean, Boolean), FindNext and FindPrevious, and after a search made by the user in the find bar of the viewer. The event data has the number of matches in the document and the selected match.
Top
Fields
 NameDescription
Public fieldStatic memberSourceProperty Identifies the Source dependency property.
Top
Remarks

The control displays PDF documents with a modern, built-in viewer: page thumbnails, outline (bookmarks) and attachments panels, text search and selection, zoom, page layouts and printing. Adobe Reader is not needed. The viewer runs on the Microsoft Edge WebView2 Runtime, which is part of Windows 10 and Windows 11 (on older systems, and on Windows Server, install the Evergreen WebView2 Runtime or point BrowserExecutableFolder at a fixed version runtime).

Load a document with Source (a file path, bindable from XAML), LoadFile(String), LoadStream(Stream) or LoadBytes(Byte). Loading is asynchronous: DocumentLoaded is raised when the document is displayed. The navigation, zoom and layout settings can be set at any time; set before a document is loaded, they apply to the next document.

Like every control hosting a native window in WPF, the viewer is always drawn on top of WPF content in the same area (it cannot be overlapped by other WPF elements, made transparent or transformed).

See Also