Encrypt PDF in C# (.NET Core)

Encrypt PDFs, Secure PDFs in C#, .NET, .NET Core and Standard

PDF documents can be encrypted using the DynamicPDF Generator or Merger products. Listed below are steps and sample code for both the products.

How to Encrypt PDF

Aes128Security, Aes256Security, RC4128Security, and RC440Security classes present in ceTe.DynamicPDF.Cryptography namespace can be used to Encrypt PDF.

Below is a list of some security settings which can be set along with passwords while encrypting a PDF document.

  • If accessibility programs should be able to read the documents text and images for the user
  • If the document can be assembled and manipulated by the user
  • If form filling should be allowed by the user
  • If the document can be printed at a high resolution by the user
  • The documents components to be encrypted

Steps for Encrypting a PDF Document

  1. Create a Document object.
  2. Create a Page object and add it to Document.
  3. Create a Aes256Security object by setting the owner and user password.
  4. Set the required security properties and the security to the Document.
  5. Invoke the Draw method on the Document 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");
security.AllowAccessibility = true;
security.AllowFormFilling = false;            
document.Security = security;

document.Draw("output.pdf");

How to Encrypt an Existing PDF

DynamicPDF Merger product can be used to Encrypt an existing PDF document.

Steps for Encrypting an existing 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 required security properties and the security to the Document
  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");
security.AllowAccessibility = true;
security.AllowFormFilling = false;  
document.Security = security;

document.Draw("output.pdf");

Getting Started

NuGet Package

DynamicPDF Generator and Merger are available on NuGet and are part of the ceTe.DynamicPDF.CoreSuite.NET package. The Core Suite package includes Generator, Merger and ReportWriter.

Nuget Logo
NuGet Package ID: ceTe.DynamicPDF.CoreSuite.NET

Example Project

An example project showing this functionality can be cloned or viewed from GitHub:

GitHub Logo
Clone or View Example Project on GitHub

DynamicPDF Generator and Merger Information

More information can be found here:

Available in Other Platforms

Generator and Merger PDF Libraries are available for Java and COM/AxtiveX platforms. Refer to the respective product pages for more details.

Why Choose DynamicPDF?

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

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