Password-protect and restrict permissions
Encryption controls who can open a PDF, and permissions govern actions such as copying and printing in viewers that honor them. Protect PDFs with AES-256 encryption and owner and user passwords, and restrict copying and printing for an existing document. This example follows PasswordProtectPdf.cs: Create builds a new document, and Existing protects a copy of DocumentA.pdf and restricts its permissions. Each method saves its own output file.
Password protection encrypts the PDF's contents using AES-256. The user password allows readers to open the document, while the owner password provides full access. Permission settings additionally tell PDF viewers which actions to allow for user access, such as copying or printing.
What you’ll create
- With
Create, a password-protected PDF containing the text “Protected” using default page settings. - With
Existing, a password-protected copy of all pages fromDocumentA.pdf, with copying and printing disabled. - AES-256 encryption with
OwnerPasswordandUserPasswordas the example passwords. PasswordProtectPdf_create_output.pdffor the new document.PasswordProtectPdf_existing_output.pdffor the protected copy.
Run calls Create followed by Existing, saving both files in your chosen output directory.

Example code
Add the following class to a console project that references the DynamicPDF Flow API. See the quick start for an overview. The class creates or imports PDF content, applies password protection and permission restrictions, and saves the document to an existing output directory.
Ensure DocumentA.pdf is in the Resources folder.
using System;
using System.IO;
using DynamicPDF.Flow;
using DynamicPDF.Flow.Merger;
namespace FluentExamples.Examples;
internal static class PasswordProtectPdf
{
public static void Run(string outputRoot)
{
Create(outputRoot);
Existing(outputRoot);
}
public static void Create(string outputRoot)
{
var path = Path.Combine(
outputRoot, "PasswordProtectPdf_create_output.pdf");
DocumentBuilder.Create()
.Security(security => security
.Algorithm(SecurityAlgorithm.Aes256)
.OwnerPassword("OwnerPassword")
.UserPassword("UserPassword"))
.Pages(page => page
.Text("Protected"))
.Save(path);
Console.WriteLine($"Created {path}");
}
public static void Existing(string outputRoot)
{
var resources = Path.Combine(
AppContext.BaseDirectory, "Resources");
var documentA = Path.Combine(resources, "DocumentA.pdf");
var path = Path.Combine(
outputRoot, "PasswordProtectPdf_existing_output.pdf"
);
DocumentBuilder.Create()
.Security(security => security
.Algorithm(SecurityAlgorithm.Aes256)
.OwnerPassword("OwnerPassword")
.UserPassword("UserPassword")
.Accessibility(true)
.DocumentAssembly(true)
.Edit(true)
.FormFilling(true)
.UpdateAnnotationsAndFields(true)
.Copy(false)
.Print(false)
.HighResolutionPrint(false))
.Pdf(PdfSource.FromFile(documentA))
.Save(path);
Console.WriteLine($"Created {path}");
}
}
Imports System
Imports System.IO
Imports DynamicPDF.Flow
Imports DynamicPDF.Flow.Merger
Friend Module PasswordProtectPdf
Public Sub Run(outputRoot As String)
Create(outputRoot)
Existing(outputRoot)
End Sub
Public Sub Create(outputRoot As String)
Dim outputPath = Path.Combine(
outputRoot, "PasswordProtectPdf_create_output.pdf")
DocumentBuilder.Create().
Security(Sub(security) security.
Algorithm(SecurityAlgorithm.Aes256).
OwnerPassword("OwnerPassword").
UserPassword("UserPassword")).
Pages(Sub(page) page.
Text("Protected")).
Save(outputPath)
Console.WriteLine($"Created {outputPath}")
End Sub
Public Sub Existing(outputRoot As String)
Dim resources = Path.Combine(
AppContext.BaseDirectory, "Resources")
Dim documentA = Path.Combine(resources, "DocumentA.pdf")
Dim outputPath = Path.Combine(
outputRoot, "PasswordProtectPdf_existing_output.pdf")
DocumentBuilder.Create().
Security(Sub(security) security.
Algorithm(SecurityAlgorithm.Aes256).
OwnerPassword("OwnerPassword").
UserPassword("UserPassword").
Accessibility(True).
DocumentAssembly(True).
Edit(True).
FormFilling(True).
UpdateAnnotationsAndFields(True).
Copy(False).
Print(False).
HighResolutionPrint(False)).
Pdf(PdfSource.FromFile(documentA)).
Save(outputPath)
Console.WriteLine($"Created {outputPath}")
End Sub
End Module
How it works
1. Create a new protected PDF with Create
Path.Combine joins outputRoot with PasswordProtectPdf_create_output.pdf. Make sure the output directory exists before calling either method.
DocumentBuilder.Create() starts the document. .Security(...) configures protection directly in the fluent chain. .Algorithm(SecurityAlgorithm.Aes256) selects AES-256 encryption, while .OwnerPassword("OwnerPassword") and .UserPassword("UserPassword") set the example passwords.
The user password is required to open the PDF with user access. The owner password provides full access. Create does not configure individual permissions; Existing adds copying and printing restrictions.
Inside .Pages(...), .Text("Protected") adds the text using the default page and text settings. .Save(path) builds and saves the protected document directly through the fluent builder. Console.WriteLine prints the output path.
2. Protect an existing PDF and restrict permissions
The method locates DocumentA.pdf in the Resources folder and joins outputRoot with PasswordProtectPdf_existing_output.pdf for the output path.
DocumentBuilder.Create() starts the destination document. The security chain sets the same AES-256 encryption and owner and user passwords used in Create. The boolean permission methods explicitly enable accessibility, document assembly, editing, form filling, and annotation updates. .Copy(false), .Print(false), and .HighResolutionPrint(false) disable copying and printing.
PdfSource.FromFile(documentA) supplies the input PDF, and .Pdf(...) imports all of its pages in their original order using the default merge options. .Save(path) saves the protected copy, and Console.WriteLine prints its path. The original DocumentA.pdf remains at its separate resource path.
3. Save both results
Run calls Create and then Existing. The methods use different output filenames, so both results remain in outputRoot. Saving overwrites an existing file with the corresponding name.
Open either output with UserPassword to view the protected document. The existing-document output restricts copying and printing for user access; OwnerPassword provides full owner access.
Next steps
Change the passwords, adjust the boolean permission settings, or replace DocumentA.pdf with your own PDF. Explore Create a PDF to build new content, Combine PDFs to import multiple documents, or browse all examples.