Using: C# .NET 8.0 WinForms project / PDFSharp v6.2.0
We are opening an existing PDF file that was generated by MS-Word, adding permissions and a password and then saving it again.
It works for most users and it correctly opens the PDF document without prompting for a password. However for some users, it asks for a password when trying to open the document. I guess that they are using non standard PDF reader applications.
Is there anything we can do to make it compatible for them too?
Current code:
Code:
string ownerKey = Convert.ToBase64String(CryptoHelper.RandomKey());
using (PdfDocument pdf = PdfReader.Open(tempPdfPath, PdfDocumentOpenMode.Modify))
{
pdf.SecuritySettings.OwnerPassword = ownerKey;
pdf.SecuritySettings.UserPassword = string.Empty;
pdf.SecuritySettings.PermitPrint = true;
pdf.SecuritySettings.PermitModifyDocument = false;
pdf.SecuritySettings.PermitExtractContent = false;
pdf.Save(tempPdfPath);
}
internal static class CryptoHelper
{
// Return 16 random bytes (128-bit) every call.
internal static byte[] RandomKey()
{
byte[] key = new byte[16];
RandomNumberGenerator rng = RandomNumberGenerator.Create();
rng.GetBytes(key);
return key;
}
}