PDF Security Settings

HiQPdf HTML to PDF Converter for .NET offers multiple options to secure the generated PDF document and to define the permitted operations on it. The security settings of the PDF document are controlled by the PdfDocumentControlSecurity property. You can password-protect the PDF document with separate passwords for opening the document and for editing permissions. You can also enable or disable PDF document printing, editing and content copying. When you set a password or change the permissions, the PDF document content is encrypted using an encryption algorithm. The currently supported encryption algorithms are RC4 and AES. For each encryption algorithm, you can also set the encryption key size, which defines the encryption level. The AES algorithm supports high 128-bit and very high 256-bit encryption key sizes, while the RC4 algorithm supports low 40-bit and high 128-bit encryption key sizes.

PDF Security Settings Source Code

In this demo, you can see how to convert a URL or an HTML file to PDF and how to configure the security of the generated PDF document. You can set a password required to open the PDF document in a PDF viewer, a password to edit the PDF document permissions in a PDF editor, enable or disable PDF document printing, content copying, editing or filling PDF forms in a PDF viewer or editor, and set the encryption mode and encryption level of the PDF document.

Demo Source Code

C#
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc;
using HiQPdf_Next_AspNetDemo.Models;

using HiQPdf.Next;

namespace HiQPdf_Next_AspNetDemo.Controllers
{
    public class PdfSecuritySettingsController : Controller
    {
        // GET: PdfSecuritySettings
        public ActionResult Index()
        {
            var model = SetViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult ConvertToPdf(PdfSecuritySettingsViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var errorMessage = ModelStateHelper.GetModelErrors(ModelState);
                throw new ValidationException(errorMessage);
            }

            // Set the serial number received after purchase to use the library in licensed mode; leave it commented for demo mode
            // Licensing.SerialNumber = "your-serial-number";

            // create the HTML to PDF converter
            HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

            // set encryption mode
            htmlToPdfConverter.Document.Security.EncryptionMode = GetSelectedEncryptionMode(model.EncryptionMode);
            // set encryption level
            htmlToPdfConverter.Document.Security.EncryptionLevel = GetSelectedEncryptionLevel(model.EncryptionLevel);

            // set open password
            htmlToPdfConverter.Document.Security.OpenPassword = model.OpenPassword;
            // set permissions password
            htmlToPdfConverter.Document.Security.PermissionsPassword = model.PermissionsPassword;

            // set PDF document permissions
            htmlToPdfConverter.Document.Security.AllowPrinting = model.AllowPrint;
            htmlToPdfConverter.Document.Security.AllowCopyContent = model.AllowCopy;
            htmlToPdfConverter.Document.Security.AllowEditContent = model.AllowEdit;
            htmlToPdfConverter.Document.Security.AllowEditAnnotations = model.AllowEditAnnotations;
            htmlToPdfConverter.Document.Security.AllowFormFilling = model.AllowFillForms;

            // convert URL to a PDF memory buffer
            string url = model.Url;

            byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);

            FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
            fileResult.FileDownloadName = "PdfSecuritySettings.pdf";

            return fileResult;
        }

        private PdfEncryptionMode GetSelectedEncryptionMode(string encryptionMode)
        {
            switch (encryptionMode)
            {
                case "RC4":
                    return PdfEncryptionMode.RC4;
                case "AES":
                    return PdfEncryptionMode.AES;
                default:
                    return PdfEncryptionMode.RC4;
            }
        }

        private PdfEncryptionLevel GetSelectedEncryptionLevel(string encryptionLevel)
        {
            switch (encryptionLevel)
            {
                case "Low":
                    return PdfEncryptionLevel.Low;
                case "High":
                    return PdfEncryptionLevel.High;
                case "Very High":
                    return PdfEncryptionLevel.VeryHigh;
                default:
                    return PdfEncryptionLevel.High;
            }
        }

        private PdfSecuritySettingsViewModel SetViewModel()
        {
            var model = new PdfSecuritySettingsViewModel();
            return model;
        }
    }
}

See Also