SelectPdf Free Html To Pdf Converter for .NET Core - Pdf Security Settings with Html To Pdf Converter - C# / ASP.NET 10 Sample

This sample shows how to convert from html to pdf using SelectPdf, how to set a password to be able to view or modify the document and also specify user permissions for the pdf document (if the user can print, copy content, fill forms, modify, etc). The document is encrypted with AES using a 256-bit key.

Url:


Rendering Engine:

User Password:


Owner Password:


Allow Assemble Document
Allow Copy Content
Allow Edit Annotations
Allow Edit Content
Fill Form Fields
Allow Print

Encrypt the XMP metadata

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

namespace SelectPdf.Samples.Pages
{
    public class PdfConverterSecurityModel : PageModel
    {
        public void OnGet()
        {
            DdlRenderingEngine = "Chromium";
        }

        [BindProperty]
        public string TxtUrl { get; set; }

        [BindProperty]
        public string DdlRenderingEngine { get; set; }

        public List<SelectListItem> RenderingEngines { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Chromium", Text = "Chromium" },
        };

        [BindProperty]
        public string TxtUserPassword { get; set; }

        [BindProperty]
        public string TxtOwnerPassword { get; set; }

        [BindProperty]
        public bool ChkCanAssembleDocument { get; set; }

        [BindProperty]
        public bool ChkCanCopyContent { get; set; }

        [BindProperty]
        public bool ChkCanEditAnnotations { get; set; }

        [BindProperty]
        public bool ChkCanEditContent { get; set; }

        [BindProperty]
        public bool ChkCanFillFormFields { get; set; }

        [BindProperty]
        public bool ChkCanPrint { get; set; }

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


        public IActionResult OnPost()
        {
            RenderingEngine renderingEngine =
                SampleForm.ParseEnum(DdlRenderingEngine, RenderingEngine.Chromium);

            // instantiate a html to pdf converter object
            HtmlToPdf converter = new HtmlToPdf();
            converter.Options.NavigationTimeout = Startup.NavigationTimeout;
            converter.Options.RenderingEngine = renderingEngine;

            // set document passwords
            if (!string.IsNullOrEmpty(TxtUserPassword))
            {
                converter.Options.SecurityOptions.UserPassword = TxtUserPassword;
            }
            if (!string.IsNullOrEmpty(TxtOwnerPassword))
            {
                converter.Options.SecurityOptions.OwnerPassword = TxtOwnerPassword;
            }

            //set document permissions
            converter.Options.SecurityOptions.CanAssembleDocument = ChkCanAssembleDocument;
            converter.Options.SecurityOptions.CanCopyContent = ChkCanCopyContent;
            converter.Options.SecurityOptions.CanEditAnnotations = ChkCanEditAnnotations;
            converter.Options.SecurityOptions.CanEditContent = ChkCanEditContent;
            converter.Options.SecurityOptions.CanFillFormFields = ChkCanFillFormFields;
            converter.Options.SecurityOptions.CanPrint = ChkCanPrint;

            // the document is encrypted with AES-256 by default; when EncryptMetadata is false the
            // XMP metadata stays readable for indexing and archiving tools
            converter.Options.SecurityOptions.EncryptMetadata = ChkEncryptMetadata;

            // create a new pdf document converting an url
            PdfDocument doc = converter.ConvertUrl(TxtUrl);

            // 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;

        }
    }
}