private static PdfPublicKeyEncryptionHandler createEncryptionHandler() { // TODO: // Change the constants, or the sample won't work. string keyStoreOwner = "key-store-owner.p12"; string passwordOwner = "password"; string keyStoreUser = "******"; string passwordUser = "******"; // You can also use an X509Certificate2 certificate to construct the handler PdfPublicKeyEncryptionHandler handler = new PdfPublicKeyEncryptionHandler(keyStoreOwner, passwordOwner); // Add another recipient with non-owner permissions PdfPermissions permissions = new PdfPermissions(); permissions.Flags = PdfPermissionFlags.FillFormFields | PdfPermissionFlags.PrintDocument; handler.AddRecipient(keyStoreUser, passwordUser, permissions); return(handler); }
public static void Main() { // NOTE: // When used in trial mode, the library imposes some restrictions. // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx // for more information. string pathToFile = "ProtectDocumentWithCertificate.pdf"; using (PdfDocument pdf = new PdfDocument()) { pdf.Pages[0].Canvas.DrawString("Hello World!"); PdfPublicKeyEncryptionHandler handler = createEncryptionHandler(); handler.Algorithm = PdfEncryptionAlgorithm.Aes256Bit; var saveOptions = new PdfSaveOptions { EncryptionHandler = handler }; pdf.Save(pathToFile, saveOptions); } Console.WriteLine($"The output is located in {Environment.CurrentDirectory}"); }