Getting Started with ExpertPdf PDF Security Library for .NET |
ExpertPdf PDF Security Library for .NET protects PDF documents with passwords and permissions, removes the protection of documents you have the password of, and signs documents with digital certificates. It can be used in any type of .NET application: console and Windows Forms applications, Windows services, ASP.NET and ASP.NET Core web applications.
The library is one assembly, epsecurepdf.dll. The main class is PdfSecurityManager; the protection to apply is described by a PdfSecurityOptions object given to its constructor.
A user password to open the document, and an owner password that gives all the rights.
Permissions: print, print in high quality, copy the content, copy for accessibility, edit the content, edit annotations, fill form fields, assemble the document.
RC4 or AES encryption, with 40, 128 or 256 bit keys.
Remove the security of a protected document, with its user or owner password.
Sign documents with a PKCS#12 (.pfx) certificate, with an optional time stamp from a time stamp authority, or certify them (see Digital Signatures).
Validate the signatures of a document: integrity, signer, certificate trust, time stamp and, optionally, revocation.
Read the security settings of a document without changing it: encryption, permissions, passwords and signatures.
Work with files, streams and byte arrays.
.NET Framework 4.0 or later. The download also has a build for .NET Framework 2.0 - 3.5 applications.
.NET Core 2.0 or later and .NET 5 to .NET 11, through the .NET Standard 2.0 build in the NuGet package.
Windows 10, Windows 11, Windows Server 2016 or later, 32-bit or 64-bit.
The easiest way to install the library is the ExpertPdf.PdfSecurity package (https://www.nuget.org/packages/ExpertPdf.PdfSecurity/). In Visual Studio open the NuGet Package Manager and install the package, or run:
dotnet add package ExpertPdf.PdfSecurity
The package works for .NET Framework and for .NET Core / .NET 5 to .NET 11 projects. It is for AnyCPU and x86 applications. ExpertPdf.PdfSecurity.x64 (https://www.nuget.org/packages/ExpertPdf.PdfSecurity.x64/) is the x64 optimized build, for applications that target the x64 platform and run in a 64-bit process.
The download contains:
Bin\.NET_4.0\epsecurepdf.dll - the library for .NET Framework 4.0 and later
Bin\epsecurepdf.dll - the library for .NET Framework 2.0 - 3.5
Samples - samples for .NET Framework 4.6.2, in C# and VB.NET
Samples_NET_10 - the same samples for .NET 10 and .NET 11, using the NuGet package
Add a reference to the epsecurepdf.dll for your framework to your project.
Set the passwords and the permissions in a PdfSecurityOptions object, create the security manager with it and save the protected document:
using ExpertPdf.PDFSecurity; PdfSecurityOptions options = new PdfSecurityOptions(); options.UserPassword = "user"; // needed to open the document options.OwnerPassword = "owner"; // opens the document with all the rights options.CanPrint = false; options.CanCopyContent = false; options.KeySize = EncryptionKeySize.EncryptKey256Bit; options.Algorithm = EncryptionAlgorithm.AES; PdfSecurityManager securityManager = new PdfSecurityManager(options); // securityManager.LicenseKey = "your license key"; securityManager.SaveSecuredPdfToFile(@"C:\Documents\report.pdf", @"C:\Documents\secured.pdf");
With a user password only, the document needs that password to open and the same password is the owner password: whoever opens the document has all the rights.
With an owner password only, the document opens without a password and the permissions apply to every reader; the owner password is needed to change them.
With both passwords, the user password opens the document with the permissions you set, the owner password opens it with all the rights.
The permissions are CanPrint, CanFullQualityPrint, CanCopyContent, CanAccessibilityCopyContent, CanEditContent, CanEditAnnotations, CanFillFormFields and CanAssembleDocument; all are allowed by default. The default encryption is AES with a 128 bit key (Algorithm, KeySize). Use AES with a 256 bit key for new documents; RC4 and 40 bit keys are only for very old PDF viewers.
// anyone can read the document; printing in low quality, form filling and accessibility only PdfSecurityOptions options = new PdfSecurityOptions(); options.OwnerPassword = "owner"; options.CanFullQualityPrint = false; options.CanCopyContent = false; options.CanAccessibilityCopyContent = true; options.CanEditContent = false; options.CanEditAnnotations = false; options.CanAssembleDocument = false; // from memory to memory byte[] securedBytes = new PdfSecurityManager(options).GetSecuredPdfBytes(File.ReadAllBytes(@"C:\Documents\report.pdf"));
SaveUnSecuredPdfToFile(String, String, String) and GetUnSecuredPdfBytes(Byte, String) remove the passwords, the permissions and the encryption of a document. They need its user or owner password; a wrong password throws a SecurePdfException.
GetSecurityInfo(String) returns a PdfSecurityInfo with the security settings of a document, without changing it: whether it is encrypted, the algorithm, the key length and the revision of the security handler, the permissions, whether a password is needed to open it (IsUserPasswordRequired) and whether the owner password is needed for full rights (IsOwnerPasswordRequired), and its signatures.
The encryption settings and the permissions are read even from a document that has an open password. The signature information needs the document opened: pass its user or owner password to GetSecurityInfo(String, String) (IsOpened tells if the document could be opened). A wrong password throws a SecurePdfException.
PdfSecurityManager securityManager = new PdfSecurityManager(); PdfSecurityInfo info = securityManager.GetSecurityInfo(@"C:\Documents\secured.pdf"); if (info.IsEncrypted) Console.WriteLine("{0} {1} bit, revision {2}", info.Algorithm, info.KeyLength, info.Revision); Console.WriteLine("Open password: {0}, owner password needed: {1}", info.IsUserPasswordRequired, info.IsOwnerPasswordRequired); Console.WriteLine("Print: {0}, copy: {1}, edit: {2}", info.CanPrint, info.CanCopyContent, info.CanEditContent); // the signatures of a document with an open password PdfSecurityInfo opened = securityManager.GetSecurityInfo(@"C:\Documents\secured.pdf", "user"); Console.WriteLine("Signatures: {0}, certified: {1}", opened.SignatureCount, opened.IsCertified);
The Samples (.NET Framework 4.6.2) and Samples_NET_10 (.NET 10 and .NET 11) folders have the same samples, in C# and VB.NET:
PdfSecurityCommandLine - a command line tool: passwords, every permission, key size and algorithm, removing the security, showing the security settings of a document.
PdfSecurityFeatures - an open password only, permissions without an open password, RC4 and AES with 40, 128 and 256 bit keys, files and memory, reading the security settings, removing the security with the owner or the user password.
PdfDigitalSignature - digital signatures from a file, from memory and from a stream with a password protected document, with optional time stamps; a certified document signed by an approver; the validation of all the signatures.
They work on the documents that come with them and need no arguments.
The PdfSecurityCommandLine sample is also a ready to use tool. The syntax is:
PdfSecurityCommandLine /pdf:source_file [/out:file]
[/userpswd:password] [/ownerpswd:password] [/keysize:40|128|256] [/algo:RC4|AES]
[/noprint] [/nofullqualityprint] [/nocopy] [/noaccessibilitycopy]
[/noeditcontent] [/noeditannot] [/nofillform] [/noassembly] [/removesecurity] [/info]With /removesecurity the owner or the user password of the source document is required. /info shows the security settings of the source document and changes nothing. Examples:
PdfSecurityCommandLine /pdf:report.pdf /noprint /keysize:256 /ownerpswd:owner /userpswd:user /out:Secured.pdf PdfSecurityCommandLine /pdf:Secured.pdf /ownerpswd:owner /removesecurity /out:Unsecured.pdf PdfSecurityCommandLine /pdf:Secured.pdf /info
ValidateSignatures(String): check the signatures of a document (see Digital Signatures).
GetSecurityInfo(String): the security settings of a document, without changing it.
CertificationLevel: certification signatures, and AddSignatureField(String, String, String, Int32, RectangleF): empty signature fields signed later.
Sign from a stream to a stream and from bytes to bytes (see Digital Signatures).
SourcePdfPassword: sign password protected documents.
Version 10 is part of the ExpertPdf 22.0 release and needs a version 10 license key.
New samples for .NET Framework 4.6.2, .NET 10 and .NET 11, in C# and VB.NET, with a digital signature sample.
A license key is emailed to you when you purchase the product. Set it with the LicenseKey property before you use the security manager. Without a license key the library adds an evaluation notice to the pages of the documents it secures, unsecures or signs and of the documents it adds a signature field to (not to a document that is signed already, whose signatures the notice would invalidate). Reading the security settings and validating the signatures is not limited. An expired license key, a key of an older major version or of another product is refused with a SecurePdfException by every method.