I am using iTextSharp 5 and trying to password protect already existing PDF file but it is not working. When I double click on new password protected file then Microsoft Edge simply opens without asking for password. Here is my code:
public void PasswordProtectPDF(string source, string destionation, string pass)
{
using (Stream input = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(destionation, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(input);
PdfReader.unethicalreading = true;
PdfEncryptor.Encrypt(reader, output, true, null, pass, PdfWriter.ALLOW_SCREENREADERS);
}
}
}
What am I doing wrong?
EDIT
I tried iText8 but same issue. Able to open PDF without password. Here is my code:
public void PasswordProtectPDF(string source, string destionation, string pass)
{
using (Stream input = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (Stream output = new FileStream(destionation, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfReader reader = new PdfReader(input);
reader.SetUnethicalReading(true);
EncryptionProperties encryptionProperties = new EncryptionProperties();
encryptionProperties.SetStandardEncryption(null, Encoding.ASCII.GetBytes(pass), EncryptionConstants.ALLOW_PRINTING, EncryptionConstants.STANDARD_ENCRYPTION_128);
PdfEncryptor.Encrypt(reader, output, encryptionProperties);
}
}
}
EDIT
Fixed the issue. Just need to change one line as below:
encryptionProperties.SetStandardEncryption(Encoding.ASCII.GetBytes(pass), null, EncryptionConstants.ALLOW_PRINTING, EncryptionConstants.STANDARD_ENCRYPTION_128);
Here is correct code for iText 8