our client have a lot of protected pdf documents uploaded in our system. They want from us to remove the protection and provided some passwords. I'm trying to achieve the goal with the following code:
- Code: Select all
foreach (var doc in dir.GetFiles("*.pdf"))
{
using (var pdf = new PdfDocument())
{
var ok = false;
for (int i = 0; i < pws.Count; i++)
{
try
{
pdf.LoadFromFile(doc.FullName, pws[i]);
pdf.Security.DecryptUserPassWord(pws[i]);
ok = true;
docOk++;
break;
}
catch { }
}
if(!ok)
docNotOk++;
}
}
Console.WriteLine($"ok: {docOk}, not ok: {docNotOk}");
For 1/3 of documents this works fine, but not for all of them. The client ensured us they use only provided passwords.
Is this the right approach? Any ideas or suggestions are welcome.
Some screenshots are attached.