Security, Viewer Preferences and Deployment |
Before a document is sent to the browser, the viewer can protect it and set how it opens. This topic also describes how the viewer serves documents, which matters when you deploy your application.
PdfSecurityOptions (Web Forms) and the security-options attribute (ASP.NET Core) hold the passwords and permissions written into the displayed copy of the document. The viewer asks the user for the UserPassword; it honours CanPrint (the print button and the printing are disabled) and CanCopyContent (the text cannot be selected and copied). When permissions are set without an OwnerPassword, a random owner password is used.
pdfViewer.PdfSourceBytes = pdfBytes; pdfViewer.PdfSecurityOptions.UserPassword = "secret"; pdfViewer.PdfSecurityOptions.CanPrint = false; pdfViewer.PdfSecurityOptions.CanCopyContent = false;
The users can still save the document (the download button can be hidden with AllowDownload / allow-download); the saved document keeps its password and permissions.
To change the security options or the viewer preferences of a document that is already protected, set OpenPassword / open-password to its password.
The page can give the password of a protected document instead of the user, with the passwordrequired event of the client API. The password stays in the browser: it is not part of the viewer address.
PdfViewerPreferences (Web Forms) and the viewer-preferences attribute (ASP.NET Core) set how the document opens: PageLayout and PageMode (the outline, the thumbnails or the attachments shown next to it). In Web Forms, PageLayoutMode sets the page layout preference too.
A document given as bytes (or one the viewer had to change or download) is kept in the memory of the web server and served to the viewer at an address only that page knows. It is kept for 30 minutes after it was last requested; change this with SlidingExpiration. In a web farm, route the requests of a user to the same server (session affinity), or display the documents from an address.
PdfViewerDocuments also lets you add a document yourself and get its token, for a viewer page you build yourself.
The viewer files are served by PdfViewerAspNet.dll itself - under /expertpdf-viewer in ASP.NET Core and ~/expertpdf-viewer.ashx in Web Forms - with long term browser caching. Nothing has to be copied to your web site. The viewer is displayed in a frame of the same site: a Content-Security-Policy of your site must allow frames from the site itself (frame-src 'self').
Version 10 replaces the viewer that needed Adobe Reader in the browser with a built-in viewer that works in every modern browser. The properties of the Web Forms control are kept. Note:
The minimum framework is .NET Framework 4.6.2 for Web Forms; ASP.NET Core (.NET 8 or later) is new.
The PdfSourceBytesHandler HTTP handler is no longer needed. It is kept, so a web.config that registers it still works; the registration can be removed.
New properties: AllowPrint, AllowDownload, ClientScriptUrl; the Attachments display mode; the JavaScript client API.