|
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#
- 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.
- Create a new PDFDocument with an existing PDF file loaded with password provided
- 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);
|