There seems to be a bug in the Aspose.Pdf.Facades.PdfFileSecurity class or the documentation. According to the documentation, the code snippet below is supposed to encrypt the PDF document:
using Aspose.Pdf.Facades;const string path = @“C:\path\to\test.pdf”;
using (var security = new PdfFileSecurity())
using (var stream = new MemoryStream(File.ReadAllBytes(path)))
{
security.BindPdf(stream);
security.EncryptFile(“password”, null, DocumentPrivilege.Print, KeySize.x256, Algorithm.AES);
security.Save(path);
}
The resulting PDF does not seem to have its contents encrypted (maybe just the metadata is encrypted?). Adobe Acrobat prompts for the password on open, but other viewers (most notably the viewer on iOS and some Android viewers) display the PDF without prompting for a password. However, if you use the Aspose.Pdf.Document class to encrypt the document, it does seem to encrypt the entire document:
using Aspose.Pdf;const string path = @“C:\path\to\test.pdf”;
var document = new Document(path);
document.Encrypt(“password”, null, Permissions.PrintDocument, CryptoAlgorithm.AESx256);
document.Save(path);
The resulting PDF does prompt for a password on all viewers we have tested with so far (or fails entirely on some mobile viewers that do not support PDF encryption). This seems like a pretty serious problem. PdfFileSecurity.EncryptFile() should encrypt the entire contents of the PDF. If not, at a minimum, the documentation should be updated to make it clear that it does not encrypt the actual contents of the file.