EVO HTML to PDF Converter allows you set the permissions of generated PDF document like printing and editing and to password protect the generated PDF document with separate user and owner passwords.
The PDF security options can be set in DocumentSecurity object. The possible options are detailed below.
PDF Security Options
User Password. You can use this option to set an user password to be required when the PDF document is opened in a PDF viewer. The property you can set in your code for this option is PdfSecurityOptionsUserPassword
Owner Password. You can use this option to set an owner password. If the PDF document is password protected you can use the owner password to open the document and change its permissions . The property you can set in your code for this option is PdfSecurityOptionsOwnerPassword
Encryption Type. Use this option to set the encryption algorithm used to encrypt the generated PDF document. The converter supports RC4 and AES encryption algorithms. The property you can set in your code for this option is PdfSecurityOptionsEncryptionAlgorithm
Encryption Key. Use this option to set the encryption key size used to encrypt the generated PDF document. The converter supports 40-bit, 128-bit and 256-bit encryption keys, function of encryption algorithm. The property you can set in your code for this option is PdfSecurityOptionsKeySize
Print Enabled. YUse this option to enable or disable the document printing option when the PDF document is opened in a PDF viewer. The property you can set in your code for this option is PdfSecurityOptionsCanPrint
High Resolution Print Enabled. Use this option to enable or disable the document printing in high resolution. The property you can set in your code for this option is PdfSecurityOptionsCanPrintHighResolution
Copy Content Enabled. Use this option to enable or disable the content copying when the PDF document is opened in a PDF viewer. The property you can set in your code for this option is PdfSecurityOptionsCanCopyContent
Copy Accessibility Content Enabled. Use this option to enable or disable the accessibility content copying when the PDF document is opened in a PDF viewer. The property you can set in your code for this option is PdfSecurityOptionsCanCopyAccessibilityContent
Edit Content Enabled. Use this option to enable or disable the content editing when the PDF document is opened in a PDF editor. The property you can set in your code for this option is PdfSecurityOptionsCanEditContent
Edit Annotations Enabled. Use this option to enable or disable the annotations editing when the PDF document is opened in a PDF editor. The property you can set in your code for this option is PdfSecurityOptionsCanEditAnnotations
Fill Form Fields Enabled. Use this option to enable or disable the form fields filling. The property you can set in your code for this option is PdfSecurityOptionsCanFillFormFields. In order to disable the form fields filling you have to disable the Edit Annotations Enabled and Edit Content Enabled options too.
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; // Use EVO PDF Namespace using EvoPdfClient; namespace EvoHtmlToPdfDemo.Controllers.PDF_Creator.PDF_Security { public class PDF_Creator_PDF_Permissions_and_PasswordController : Controller { [HttpPost] public ActionResult CreatePdf(IFormCollection collection) { // Get the server options string serverIP = collection["textBoxServerIP"]; uint serverPort = uint.Parse(collection["textBoxServerPort"]); string servicePassword = collection["textBoxServicePassword"]; bool useServicePassword = servicePassword.Length > 0; bool useTcpService = collection["ServerType"] == "radioButtonUseTcpService"; string webServiceUrl = collection["textBoxWebServiceUrl"]; // Create a PDF document Document pdfDocument = null; if (useTcpService) pdfDocument = new Document(serverIP, serverPort); else pdfDocument = new Document(true, webServiceUrl); // Set optional service password if (useServicePassword) pdfDocument.ServicePassword = servicePassword; // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // Add a page to PDF document PdfPage pdfPage = pdfDocument.AddPage(); // Set the encryption algorithm and the encryption key size if they are not the default ones if (collection["EncryptionKey"] != "bit128RadioButton" || collection["EncryptionType"] != "rc4RadioButton") { // set the encryption algorithm pdfDocument.Security.EncryptionAlgorithm = collection["EncryptionType"] == "rc4RadioButton" ? EncryptionAlgorithm.RC4 : EncryptionAlgorithm.AES; // set the encryption key size if (collection["EncryptionKey"] == "bit40RadioButton") pdfDocument.Security.KeySize = EncryptionKeySize.EncryptKey40Bit; else if (collection["EncryptionKey"] == "bit128RadioButton") pdfDocument.Security.KeySize = EncryptionKeySize.EncryptKey128Bit; else if (collection["EncryptionKey"] == "bit256RadioButton") pdfDocument.Security.KeySize = EncryptionKeySize.EncryptKey256Bit; } // Set user and owner passwords if (collection["userPasswordTextBox"][0].Length > 0) pdfDocument.Security.UserPassword = collection["userPasswordTextBox"]; if (collection["ownerPasswordTextBox"][0].Length > 0) pdfDocument.Security.OwnerPassword = collection["ownerPasswordTextBox"]; // Set PDF document permissions pdfDocument.Security.CanPrint = collection["printEnabledCheckBox"].Count > 0; pdfDocument.Security.CanPrintHighResolution = collection["highResolutionPrintEnabledCheckBox"].Count > 0; pdfDocument.Security.CanCopyContent = collection["copyContentEnabledCheckBox"].Count > 0; pdfDocument.Security.CanCopyAccessibilityContent = collection["copyAccessibilityContentEnabledCheckBox"].Count > 0; pdfDocument.Security.CanEditContent = collection["editContentEnabledCheckBox"].Count > 0; pdfDocument.Security.CanEditAnnotations = collection["editAnnotationsEnabledCheckBox"].Count > 0; pdfDocument.Security.CanFillFormFields = collection["fillFormFieldsEnabledCheckBox"].Count > 0; if ((PermissionsChanged(pdfDocument) || pdfDocument.Security.UserPassword.Length > 0) && pdfDocument.Security.OwnerPassword.Length == 0) { // A user password is set but the owner password is not set or the permissions are not the default ones // Set a default owner password pdfDocument.Security.OwnerPassword = "owner"; } // Create a HTML to PDF element to add to document HtmlToPdfElement htmlToPdfElement = new HtmlToPdfElement(collection["urlTextBox"]); // Optionally set a delay before conversion to allow asynchonous scripts to finish htmlToPdfElement.ConversionDelay = 2; // Add the HTML to PDF element to document pdfPage.AddElement(htmlToPdfElement); // Save the PDF document in a memory buffer byte[] outPdfBuffer = pdfDocument.Save(); // Send the PDF file to browser FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf"); fileResult.FileDownloadName = "Set_Permissions_Password.pdf"; return fileResult; } private bool PermissionsChanged(Document pdfDocument) { return !pdfDocument.Security.CanPrint || !pdfDocument.Security.CanPrintHighResolution || !pdfDocument.Security.CanCopyContent || !pdfDocument.Security.CanCopyAccessibilityContent || !pdfDocument.Security.CanEditContent || !pdfDocument.Security.CanEditAnnotations || !pdfDocument.Security.CanFillFormFields; } } }