Examples

Password Protect PDF

Use DynamicPDF Core Suite to add password protection to a new or existing PDF document. The steps and sample code below illustrates password protecting both a new and an existing PDF.

Watch the Video

How to Password Protect PDF

Setting password protection and encrypting a PDF requires only a few lines of code. When a user password is specified, the PDF viewer will require this password to open the PDF document. If left blank the viewer will not prompt for a password when opening the PDF. When an owner password is specified, the PDF viewer requires the password to modify the PDF. The following classes are used when setting passwords or encrypting PDFs with different security settings.

Steps to Password Protect PDF

  1. Create a Document object.
  2. Create a Page object and add it to the Document instance.
  3. Create a Aes256Security object by setting the owner and user password.
  4. Set the Document object's security.
  5. Invoke the Draw method on the Document instance to output the PDF.

Sample Code - C#

Document document = new Document();
            
Page page = new Page();
document.Pages.Add(page);
           
Aes256Security security = new Aes256Security("owner", "user");
document.Security = security;

document.Draw(@"Output.pdf");

How to Add Password to PDF

The above security classes can be used to add password protection to an existing PDF document as well.

Steps to Add Password to PDF

  1. Create a MergeDocument object with the path to the PDF file.
  2. Create a Aes256Security object by setting the owner and user password.
  3. Set the Document object's security.
  4. Invoke the Draw method on the Document to output the PDF.

Sample code - C#

MergeDocument document = new MergeDocument("pdf-a.pdf");

Aes256Security security = new Aes256Security("owner", "user");
document.Security = security;

document.Draw(@"Output.pdf");

GitHub Project

Clone or view the example project at GitHub. This example code is contained in the Examples/PasswordProtectPDF.cs file.

Clone or View Example Project on GitHub

Getting Started

NuGet Package

DynamicPDF Core Suite is available on NuGet and is part of the ceTe.DynamicPDF.CoreSuite.NET package. The easiest way to install the package is through the Visual Studio Package Manager. You can also download the package from NuGet.

NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

DynamicPDF Core Suite Information

More information on DynamicPDF Core Suite can be found from its webpage.

Available on Other Platforms

DynamicPDF Core Suite is also available for the Java and COM/ActiveX platforms. Refer to the respective product pages for more details.

Why Choose DynamicPDF?

  • Transparent Pricing
  • Lots of Features
  • Easy to Use
  • Great Support
  • Efficient Performance
  • Product Maturity (Over 18 Years)
  • Free Evaluation
  • .NET Core Support (Most Products)
  • Flexible Licensing

We’re Not The Only Ones That Think We’re Great!