PDF Creator ASP.NET Core samples · .NET 10 and .NET 11

Security and Digital Signatures

Encrypts the PDF document with a user and an owner password, sets its permissions and signs it with the certificate of a PKCS#12 (.pfx) file. The signature is displayed over the logo image.

C# code of this sample

Pages/SecurityDemo.cshtml.cs
using System;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using ExpertPdf.PdfCreator;

namespace ExpertPdf.PdfCreator.Samples.Pages
{
    public class SecurityDemoModel : PageModel
    {
        [BindProperty, DisplayFormat(ConvertEmptyStringToNull = false)]
        public string UserPassword { get; set; } = "";

        [BindProperty, DisplayFormat(ConvertEmptyStringToNull = false)]
        public string OwnerPassword { get; set; } = "owner";

        [BindProperty]
        public bool CanPrint { get; set; } = false;

        [BindProperty]
        public bool CanCopy { get; set; } = false;

        [BindProperty]
        public bool CanEdit { get; set; } = true;

        [BindProperty]
        public int KeySize { get; set; } = 0;

        [BindProperty]
        public bool Sign { get; set; } = true;

        public static readonly string[] KeySizeNames = { "128 bit", "40 bit" };

        public string Message { get; set; }

        public void OnGet()
        {
        }

        public IActionResult OnPost()
        {
            try
            {
                return File(CreatePdf(), "application/pdf", "Security.pdf");
            }
            catch (Exception ex)
            {
                Message = "The PDF document was not created. " + ex.Message;
                return Page();
            }
        }

        private byte[] CreatePdf()
        {
            // LicensingManager.LicenseKey = "your license key";

            Document document = new Document();

            // The user password is needed to open the document (empty: anyone can open it). The owner password is needed
            // to change the permissions; when a password is set, the document is encrypted and the permissions apply.
            document.Security.UserPassword = UserPassword;
            document.Security.OwnerPassword = OwnerPassword;
            document.Security.CanPrint = CanPrint;
            document.Security.CanCopyContent = CanCopy;
            document.Security.CanEditContent = CanEdit;
            document.Security.KeySize = KeySize == 0 ? EncryptionKeySize.EncryptKey128Bit : EncryptionKeySize.EncryptKey40Bit;

            PdfPage page = document.AddPage();
            PdfFont font = document.AddFont(new Font("Arial", 12, GraphicsUnit.Point), true);

            // the signature is displayed over an element of the page, here a logo image
            AddElementResult result = page.AddElement(new ImageElement(0, 0, 200, Path.Combine(Helper.FilesFolder, @"img\logo.jpg")));
            page.AddElement(new TextElement(0, result.EndPageBounds.Bottom + 20, page.ClientRectangle.Width,
                "This document is encrypted and its permissions are set. Click the logo to see the digital signature.", font));

            if (Sign)
            {
                // the certificate and its private key, from a password protected PKCS#12 file
                DigitalCertificatesCollection certificates =
                    DigitalCertificatesStore.GetCertificates(Path.Combine(Helper.FilesFolder, "oss.pfx"), "osspswd");
                DigitalCertificate certificate = certificates[0];

                DigitalSignatureElement signature = new DigitalSignatureElement(result.EndPageBounds, certificate);
                signature.Reason = "Protect the document from unwanted changes";
                signature.ContactInfo = "office@html-to-pdf.net";
                signature.Location = "Development server";
                page.AddElement(signature);
            }

            byte[] pdf = document.Save();
            document.Close();
            return pdf;
        }
    }
}