Loading Documents |
The viewer displays a document from a file, a stream or a byte array. Loading is asynchronous: the Load methods return at once, and the DocumentLoaded event is raised when the document is displayed.
Method | Use it for |
|---|---|
A PDF file on disk or on a network share. | |
Any readable stream - a resource, a download, a database BLOB. The stream is read when the method is called; you can close it afterwards. | |
A document created in memory, for example by ExpertPdf HTML to PDF Converter or ExpertPdf PDF Creator. Nothing is written to disk. |
// a report converted from HTML is displayed without saving it PdfConverter converter = new PdfConverter(); byte[] pdfBytes = converter.GetPdfBytesFromUrl("https://www.example.com/report"); pdfViewer.LoadBytes(pdfBytes);
In WPF you can also set the Source property to the path of a file, in XAML or through data binding.
CloseDocument closes the displayed document. ReloadPdfDocument loads the same document again, for example after you change the security options or the viewer preferences.
Member | Description |
|---|---|
The document is displayed. PageCount, CurrentPage and ZoomPercentage have their values. | |
The page shown changed - the user scrolled, followed a link, or your code navigated. | |
A document could not be opened (not a PDF, damaged, a wrong password), or the WebView2 Runtime is not installed. The event arguments carry the message. | |
True while a document is displayed. | |
The number of pages of the displayed document, or -1. |
pdfViewer.DocumentLoaded += delegate { statusLabel.Text = string.Format("{0} pages", pdfViewer.PageCount); }; pdfViewer.CurrentPageChanged += delegate { pageLabel.Text = string.Format("Page {0} of {1}", pdfViewer.CurrentPage, pdfViewer.PageCount); }; pdfViewer.LoadError += delegate (object sender, PdfViewerErrorEventArgs e) { statusLabel.Text = "Error: " + e.Message; };
To know the number of pages of a document without displaying it, use the static GetPageCount(String) method.
When a document needs a password to be opened, the viewer asks the user for it. To open it without asking, set OpenPassword before loading it:
pdfViewer.OpenPassword = "the document password"; pdfViewer.LoadFile(@"C:\Documents\protected.pdf");
The open password is also needed to apply security options or viewer preferences to a document that is already protected (see Security and viewer preferences).
To give the password only when it is needed - from a database, a key store or a dialog of your application - handle the PasswordRequired event. It is raised when the document being opened needs a password and no OpenPassword is set, and again with IsPasswordIncorrect set when the password given was wrong (yours, or one the user typed). Set Password to open the document, or Cancel to stop opening it (LoadError is raised). When no password is set, the viewer asks the user, as without the event.
View settings made before a document is loaded apply to it when it opens. For example, to open a document at page 3, fitting the page width, with the thumbnails shown:
pdfViewer.CurrentPage = 3;
pdfViewer.PageFitMode = PageFitMode.FitH;
pdfViewer.DocumentDisplayMode = DocumentDisplayMode.Thumbs;
pdfViewer.LoadFile(path);