Updated on Kisan Patel
Problem:
How to remove password protection from PDF document in C#?
How can I remove PDF password in C#?
How to open the password protected pdf using C#?
Solution:
To remove password or open password protected pdf document using C#, you need to use c# PDFsharp library.
Here is the C# example using PDFsharp:
private void GeneratePdf(string InputFile) { PdfDocument maindoc = PdfReader.Open(InputFile, PdfDocumentOpenMode.Import); PdfDocument OutputDoc = new PdfDocument(); foreach (PdfPage page in maindoc.Pages) { OutputDoc.AddPage(page); } OutputDoc.Save(folderPath + "\\" + fileName); maindoc.Dispose(); OutputDoc.Dispose(); }
Following C# example will generate pdf with the user password.
private void GeneratePdf(string InputFile) { PdfDocument maindoc = PdfReader.Open(InputFile, "password",PdfDocumentOpenMode.Import); //Use the property HasOwnerPermissions to decide whether the used password // was the user or the owner password. bool hasOwnerAccess = maindoc.SecuritySettings.HasOwnerPermissions; PdfDocument OutputDoc = new PdfDocument(); foreach (PdfPage page in maindoc.Pages) { OutputDoc.AddPage(page); } OutputDoc.Save(folderPath + "\\" + fileName); maindoc.Dispose(); OutputDoc.Dispose(); }
Source: http://www.pdfsharp.com/PDFsharp/index.php?option=com_content&task=view&id=39&Itemid=50