1

I have generated a pdf file with password protection by using the following code:

using (Stream input = new FileStream(InputFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
    {
        PdfReader reader = new PdfReader(input);
        PdfEncryptor.Encrypt(reader, output, true, strDob, "secret", PdfWriter.ALLOW_SCREENREADERS);
    }
}

I want to remove the password for the PDF file generated using the above code based on my certain condtions through code.

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
Harikasai
  • 1,287
  • 5
  • 15
  • 21
  • What library are you using to write the PDF? Does its manual mention anything about removing passwords from existing files? Isn't reading the password-protected file and writing the data in a new file without a password an option? – CodeCaster Jun 13 '12 at 11:48
  • I have used itextsharp dll. and folowwing namespaces to generate password protected pdf file.using iTextSharp.text; using iTextSharp.text.pdf; – Harikasai Jun 14 '12 at 05:34
  • possible duplicate of http://stackoverflow.com/a/10169551/298573 – VahidN Jun 13 '12 at 18:55
  • Hi,Thanks for the response.I have referred the above link.but PdfReader.unethicalreading is not coming in my intelliscence. and i haven't used any AES or Rijndael algorithms for provindig pasword protection.I have tried with Rijndael Decrypt() method also.But didn't work out :( – Harikasai Jun 14 '12 at 05:27
  • You need the latest version. unethicalreading is added recently. – VahidN Jun 14 '12 at 09:48

1 Answers1

3
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string InputFile = Path.Combine(WorkingFolder, "test.pdf");
string OutputFile = Path.Combine(WorkingFolder, "test_dec.pdf");//will be created automatically
//You must provide owner password but not the user password .
private void DecryptFile(string inputFile, string outputFile)
{

    string password = @"secret"; // Your Key Here           
    try
    {
        PdfReader reader = new PdfReader(inputFile, new System.Text.ASCIIEncoding().GetBytes(password));

            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfStamper stamper = new PdfStamper(reader, memoryStream);
                stamper.Close();
                reader.Close();
                File.WriteAllBytes(outputFile, memoryStream.ToArray());
            }

    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message);
    }
}
Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
Harikasai
  • 1,287
  • 5
  • 15
  • 21
  • //I got the solution finally :) ... Use the following code for removing password protection to a pdf file. //Nasmspaces using iTextSharp.text;// u must add itextsharp.dll as reference using iTextSharp.text.pdf; using System.IO; using System.Text; – Harikasai Jun 14 '12 at 11:07