PM > Install-Package XDoc.PDF

How to Start Tutorials Troubleshooting Main Operations Convert PDF Read PDF Edit PDF PDF Report Generator Work with PDF Modules PDF Document PDF Pages Text Image Graph & Path Annotation, Markup & Drawing Redaction Security Digital Signature Forms Watermark Bookmark Link File Attachment File Metadata Printing Work with Other SDKs Barcode read Barcode create OCR Twain

C# PDF Password Library
How to create, generate, open, remove owner password from protected PDF file using C# .net


Help C# Developers to Improve the Security of Your PDF Document by Setting Password in C# .NET Application





In this C# tutorial, you learn how to create, update, remove passowrd on a protected PDF file in your .NET WinForms, WPF, ASP.NET applications.

  • Create, add a new password to PDF
  • Update, remove password from PDF file
  • Verify PDF password protected or not
  • Encrypt PDF using RC4, AES
  • Use Unicode in password

How to manage a password protected PDF file using C#

  1. Download XDoc.PDF password manager C# library
  2. Install C# library to add, create, remove PDF password
  3. Step by Step Tutorial












  • Best .NET PDF document manipulation SDK library for PDF document protecting in Visual C# .NET framework project
  • Support .NET WinForms, ASP.NET MVC in IIS, ASP.NET Ajax, Azure cloud service, DNN (DotNetNuke), SharePoint
  • A professional PDF encryption and decryption control able to be integrated in C#.NET WinForm and ASP.NET WebForm application
  • Able to perform PDF file password adding, deleting and changing in Visual Studio .NET project use C# source code in .NET class
  • Allow to decrypt PDF password and open a password protected document in C#.NET framework
  • Support to add password to PDF document online or in C#.NET WinForms for PDF file protection
  • Able to create a password protected PDF contains file permission limitation
  • An advanced PDF password remover component can help users unlock PDF file without password
  • Able to change password on adobe PDF document in C#.NET


To help protect your PDF document in C# project, XDoc.PDF provides some PDF security settings. On this page, we will talk about how to achieve this via password. In general, you can do following manipulations.







How to open a password protected PDF file using C#


The following contents and C# example source code will show how to read, open a password protected PDF file in C# code.

  1. Create a new PDFDocument with an existing PDF file loaded with password provided
  2. Call PDFDocument.Save() to save the PDF file to a new location without password protected


String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String userPassword = @"you";
//  open an encrypted document
PDFDocument doc = PDFDocument.Open(intputFilePath, userPassword);

String outputFilePath = Program.RootPath + "\\" + "Output.pdf";
//  remove the password
doc.Save(outputFilePath);




Add password to a PDF file using C#


The following C# source code will add a password to an existing PDF file (without password protected), and save to a new location.



String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_with_pw.pdf";
String userPassword = "you";
String ownerPassword = "me";
//  create password setting
PasswordSetting setting = new PasswordSetting(userPassword, ownerPassword);
//  add password to plain file
int errorCode = PDFDocument.AddPassword(intputFilePath, outputFilePath, setting);
if (errorCode == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine("Failed");
}


The following C# source code will add a password to PDF file.



String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String userPassword = "you";
String ownerPassword = "me";
//  create password setting
PasswordSetting setting = new PasswordSetting(userPassword, ownerPassword);
//  add password to plain file
int errorCode = PDFDocument.AddPassword(intputFilePath, setting);
if (errorCode == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine("Failed");
}


Add password to a PDF in stream object or byte arrary using c#


The following C# source code will add a password to PDF in Stream object and save to a new Stream object.



String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_with_pw.pdf";

FileStream inputStream = File.Open(intputFilePath, FileMode.Open, FileAccess.Read);
FileStream outputStream = File.Open(intputFilePath, FileMode.Create, FileAccess.ReadWrite);
String userPassword = "you";
String ownerPassword = "me";
//  create password setting
PasswordSetting setting = new PasswordSetting(userPassword, ownerPassword);
//  add password to a plain file stream
int errorCode = PDFDocument.AddPassword(inputStream, outputStream, setting);
if (errorCode == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine("Failed");
}




Update password on a protected PDF file using c#


Output to a new file



String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_with_pw.pdf";
String userPassword = "you";
String newUserPassword = "fill";
String newOwnerPassword = "watch";
//  create setting for the new password
PasswordSetting setting = new PasswordSetting(newUserPassword, newOwnerPassword);
//  change password for an encrypted file
int errorCode = PDFDocument.ChangePassword(intputFilePath, outputFilePath, userPassword, setting);
if (errorCode == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine("Failed");
}


Overwrite the original file



String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String userPassword = "you";
String newUserPassword = "fill";
String newOwnerPassword = "watch";
//  create setting for the new password
PasswordSetting setting = new PasswordSetting(newUserPassword, newOwnerPassword);
//  change password for an encrypted file
int errorCode = PDFDocument.ChangePassword(intputFilePath, userPassword, setting);
if (errorCode == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine("Failed");
}




Change password on PDF in stream object or byte array using c#


String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "1_with_pw.pdf";

FileStream inputStream = File.Open(intputFilePath, FileMode.Open, FileAccess.Read);
FileStream outputStream = File.Open(intputFilePath, FileMode.Create, FileAccess.ReadWrite);
String userPassword = "you";
String newUserPassword = "fill";
String newOwnerPassword = "watch";
//  create setting for the new password
PasswordSetting setting = new PasswordSetting(newUserPassword, newOwnerPassword);
//  change password for an encrypted file
int errorCode = PDFDocument.ChangePassword(inputStream, outputStream, userPassword, setting);
if (errorCode == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine("Failed");
}




Remove the password from a protected PDF file using C#


Output to a new file



String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String userPassword = @"you";
String outputFilePath = Program.RootPath + "\\" + "Remove.pdf";
//  remove password in the input file and output to a new file
int errorCode = PDFDocument.RemovePassword(intputFilePath, outputFilePath, userPassword);
if (errorCode == 0) Console.WriteLine("Success");
else Console.WriteLine("Failed");


Overwrite the original file



String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String userPassword = @"you";
//  remove password in the file
int errorCode = PDFDocument.RemovePassword(intputFilePath, userPassword);
if (errorCode == 0) Console.WriteLine("Success");
else Console.WriteLine("Failed");




Remove the password from PDF in stream object or byte array using c#


String intputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFilePath = Program.RootPath + "\\" + "Remove.pdf";

FileStream inputStream = File.Open(intputFilePath, FileMode.Open, FileAccess.Read);
FileStream outputStream = File.Open(intputFilePath, FileMode.Create, FileAccess.ReadWrite);
String userPassword = @"you";

int errorCode = PDFDocument.RemovePassword(inputStream, outputStream, userPassword);
if (errorCode == 0)
{
    Console.WriteLine("Success");
}
else
{
    Console.WriteLine("Failed");
}






How to verify if a PDF file is password protected or not using c#


Remarks: If a PDF file does not been encrypted, it should never have any password.



String intputFilePath = Program.RootPath + "\\" + "1.pdf";

bool isEncrypted = PDFDocument.IsEncrypted(intputFilePath);
bool hasUserPassword = PDFDocument.HasUserPassword(intputFilePath);

Console.WriteLine(@"This file is encrypted: " + isEncrypted);
Console.WriteLine(@"Password is not empty: " + hasUserPassword);






PDF passsword more encryption methods in C#


The C# source code below shows how to encrypt a PDF using

  • RC4 with 40-bit key
  • RC4 with 128-bit key
  • AES with 128-bit key
  • AES with 256-bit key



String inputFilePath = @"C:\1.pdf";
String outputFolder = @"C:\";

String userPassword = "1234";
String ownerPassword = "5678";

//  Encrypt file by using RC4 with 40-bit key
PasswordSetting settings1 = new PasswordSetting(userPassword, ownerPassword);
settings1.Level = EncryptionLevel.RC4_40bit;
PDFDocument.AddPassword(inputFilePath, outputFolder + "1.rc4_40.pdf", settings1);

//  Encrypt file by using RC4 with 128-bit key
PasswordSetting settings2 = new PasswordSetting(userPassword, ownerPassword);
settings2.Level = EncryptionLevel.RC4_128bit;
PDFDocument.AddPassword(inputFilePath, outputFolder + "1.rc4_128.pdf", settings2);

//  Encrypt file by using AES with 128-bit key
PasswordSetting settings3 = new PasswordSetting(userPassword, ownerPassword);
settings3.Level = EncryptionLevel.AES_128bit;
PDFDocument.AddPassword(inputFilePath, outputFolder + "1.aes_128.pdf", settings3);

//  Encrypt file by using AES with 256-bit key
PasswordSetting settings4 = new PasswordSetting(userPassword, ownerPassword);
settings4.Level = EncryptionLevel.AES_256bit;
PDFDocument.AddPassword(inputFilePath, outputFolder + "1.aes_256.pdf", settings4);






How to add password using Unicode to PDF file in C#.NET


The following C# source code will show how to add a new password using Unicode (Chinese characters) to a PDF file using C#.

To encode Unicode in PDF password, you need encrypt PDF file using AES with 256-bit key.



String inputFilePath = Program.RootPath + "\\" + "1.pdf";
String outputFolder = Program.RootPath + "\\";

String userPassword = "用户密码";
String ownerPassword = "拥有者密码";

//  Encrypt file by using AES with 256-bit key
//  Note:
//  Only AES-256 support Unicode in passwords
PasswordSetting settings = new PasswordSetting(userPassword, ownerPassword);
settings.Level = EncryptionLevel.AES_256bit;
PDFDocument.AddPassword(inputFilePath, outputFolder + "1.aes_256.pdf", settings);








Common Asked Questions

How can I get the password for a PDF file?

You can open a PDF file using Acrobar, and add a file open password on the PDF document. Using RasterEdge C# PDF library, you can add, remove PDF file password in one line of C# code in your ASP.NET, WinForms, WPF web and desktop applications.

How do you remove password from PDF?

Open the PDF file using a PDF editor program, such as Adobe Acrobat. Choose "Tools" from toolbar. Click "Protect" from "Protect & Standardize" group. Choose "Remove Security" from "Encrypt". Using XDoc.PDF C# library, you can easily remove file open password, or owner password using one line C# source code in your .NET projects.

How do I remove password protection from a PDF without Adobe Pro?

You can use C# PDF library free trial package to remove password protection from a PDF file without Adobe Pro.

How do I open a PDF that has a password?

You need the file protected password to open a PDF which has a password. All PDF reader or editor applications support open PDF file with a password. You can easily open, read the PDF file with password locked in your C# asp.net core web app using C# PDF library.

How to lock a PDF file with a password?

PDF supports two kinds of password. One is the file open password. When a user wants to open and read a PDF file with file open password protected, he needs enter password before he can view the pdf content. The second is the file permission password. The PDF document owner can set the permission password with security options applied. Such as content edit, print. Using RasterEdge XDoc.PDF C# library, you can easily set and apply the two passwords on the PDFs in your ASP.NET, MVC, Windows applications.

What is the owner password to unlock a PDF document?

According to PDF specification, the owner's password is the same as PDF file permission password. The PDF content creator and owner is use owner password to restrict documents. Using C# PDF library, you can add or remove owner's password on the PDF document in C# Visual Studio .NET projects.

What are the different types of PDF passwords?

There are two types of PDF passwords. User Password: user needs input password to open and view the PDF document. Owner Password: the owner used to restrict the document. RasterEdge C# PDF component supports both passwords on PDF file in your .NET projects.