XDoc.PDF
Features
Tech Specs
How-to C#
Pricing
C# PDF: How to PDF Create PDF Export File and Page Process PDF Read PDF Write Form Process Document Protect Annotation & Drawing PDF Print Work with Other SDKs Barcode Read Barcode Create OCR Twain

C# PDF Password Library
How to add, create, generate, 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










  • 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.







Open a PDFDocument with password using c#


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 plain 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 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");
}


Overwrite the original 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 plain file stream 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 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");
}




Change password for an encrypted 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 for an encrypted file stream 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 for an encrypted document


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 for an encrypted file stream 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");
}




Save a PDFDocument with password using c#


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

//  load a plain file
PDFDocument doc = new PDFDocument(intputFilePath);

String userPassword = "fill";
String ownerPassword = "watch";
//  create password setting
PasswordSetting setting = new PasswordSetting(userPassword, ownerPassword);

//  save PDFDocument object with password
doc.Save(outputFilePath, setting);




Verify if a file is encrypted 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);