SelectPdf for .NET 10 - Pdf Security Settings - C# / .NET 10 Sample

This sample shows how to create a new PDF document using SelectPdf, how to set a password to be able to view or modify the document (password: 'test1' and 'test2') and also specifies user permissions for the pdf document (if the user can print, copy content, fill forms, modify, etc).

Encryption:


Encrypt the XMP metadata

Sample code · C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class PdfSecurityModel : PageModel
    {
        public void OnGet()
        {

        }

        [BindProperty]
        public string DdlEncryption { get; set; } = "AES256R6";

        public List<SelectListItem> Encryptions { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "RC4_40", Text = "RC4, 40-bit key" },
            new SelectListItem { Value = "RC4_128", Text = "RC4, 128-bit key" },
            new SelectListItem { Value = "AES128", Text = "AES, 128-bit key" },
            new SelectListItem { Value = "AES256", Text = "AES, 256-bit key, revision 5" },
            new SelectListItem { Value = "AES256R6", Text = "AES, 256-bit key, revision 6 (default)" },
            new SelectListItem { Value = "AESGCM", Text = "AES-256-GCM (PDF 2.0)" },
        };

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

        public IActionResult OnPost()
        {
            // create a new pdf document
            PdfDocument doc = new PdfDocument();

            // add a new page to the document
            PdfPage page = doc.AddPage();

            // create a new pdf font
            PdfFont font = doc.Fonts.Add(PdfStandardFont.Helvetica);
            font.Size = 20;

            // create a new text element and add it to the page
            PdfTextElement text = new PdfTextElement(50, 50,
                "Pdf Security Settings Sample.", font);
            page.Add(text);

            // set document passwords
            doc.Security.OwnerPassword = "test1";
            doc.Security.UserPassword = "test2";

            // set the encryption strength: the key size and the algorithm
            switch (DdlEncryption)
            {
                case "RC4_40":
                    doc.Security.KeySize = PdfEncryptionKeySize.EncryptKey40Bit;
                    doc.Security.Algorithm = PdfEncryptionAlgorithm.RC4;
                    break;
                case "RC4_128":
                    doc.Security.KeySize = PdfEncryptionKeySize.EncryptKey128Bit;
                    doc.Security.Algorithm = PdfEncryptionAlgorithm.RC4;
                    break;
                case "AES128":
                    doc.Security.KeySize = PdfEncryptionKeySize.EncryptKey128Bit;
                    doc.Security.Algorithm = PdfEncryptionAlgorithm.AES;
                    break;
                case "AES256":
                    doc.Security.KeySize = PdfEncryptionKeySize.EncryptKey256Bit;
                    doc.Security.Algorithm = PdfEncryptionAlgorithm.AES;
                    break;
                case "AESGCM":
                    // AES-256-GCM, defined by PDF 2.0: the document is written as PDF 2.0
                    doc.Security.KeySize = PdfEncryptionKeySize.EncryptKey256Bit;
                    doc.Security.Algorithm = PdfEncryptionAlgorithm.AESGCM;
                    break;
                default:
                    // AES with a 256-bit key, revision 6, is the default: setting a password is enough
                    // (the explicit assignment is shown for completeness)
                    doc.Security.KeySize = PdfEncryptionKeySize.EncryptKey256BitRevision6;
                    doc.Security.Algorithm = PdfEncryptionAlgorithm.AES;
                    break;
            }

            // encrypt the XMP metadata stream too (the default); when false, the metadata stays readable
            // for indexing and archiving tools while the content is encrypted
            doc.Security.EncryptMetadata = ChkEncryptMetadata;

            //set document permissions
            doc.Security.CanAssembleDocument = false;
            doc.Security.CanCopyContent = true;
            doc.Security.CanAccessibilityCopyContent = true;
            doc.Security.CanEditAnnotations = true;
            doc.Security.CanEditContent = true;
            doc.Security.CanFillFormFields = true;
            doc.Security.CanPrint = true;

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;

        }
    }
}