Skip Navigation Links.
In this demo you can see how to convert an URL or HTML file to a PDF and how to set the security of 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, you can enable or disable the PDF document printing, content copying, editing or filling PDF forms in a PDF viewer or editor, set the encryption mode and the encryption level of the PDF document.
Server Settings
IP: Port: Password:
URL
PDF Security Settings
Encryption Mode: Encryption Level:
Open Password: Permissions Password:
Skip Navigation Links
C# Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HiQPdfClient;

namespace HiQPdf_Demo
{
    public partial class PdfSecuritySettings : System.Web.UI.Page
    {
        protected void buttonConvertToPdf_Click(object sender, EventArgs e)
        {
            string serverIP = textBoxServerIP.Text;
            uint serverPort = uint.Parse(textBoxServerPort.Text);
            string serverPassword = textBoxServerPassword.Text;

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

            // use server password if necessary
            if (serverPassword.Length > 0)
                htmlToPdfConverter.ServerPassword = serverPassword;

            // set a demo serial number
            htmlToPdfConverter.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

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

            // set open password
            htmlToPdfConverter.Document.Security.OpenPassword = textBoxOpenPassword.Text;
            // set permissions password
            htmlToPdfConverter.Document.Security.PermissionsPassword = textBoxPermissionsPassword.Text;

            // set PDF document permissions
            htmlToPdfConverter.Document.Security.AllowPrinting = checkBoxAllowPrint.Checked;
            htmlToPdfConverter.Document.Security.AllowCopyContent = checkBoxAllowCopy.Checked;
            htmlToPdfConverter.Document.Security.AllowEditContent = checkBoxAllowEdit.Checked;
            htmlToPdfConverter.Document.Security.AllowEditAnnotations = checkBoxAllowEditAnnotations.Checked;
            htmlToPdfConverter.Document.Security.AllowFormFilling = checkBoxAllowFillForms.Checked;

            // set a default permissions password if an open password was set without settings a permissions password
            // or if any of the permissions does not have the default value
            if (htmlToPdfConverter.Document.Security.PermissionsPassword == String.Empty &&
               (htmlToPdfConverter.Document.Security.OpenPassword != String.Empty || !IsDefaultPermission(htmlToPdfConverter.Document.Security)))
            {
                htmlToPdfConverter.Document.Security.PermissionsPassword = "admin";
            }

            // convert URL to a PDF memory buffer
            string url = textBoxUrl.Text;

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

            // inform the browser about the binary data format
            HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

            // let the browser know how to open the PDF document, attachment or inline, and the file name
            HttpContext.Current.Response.AddHeader("Content-Disposition", 
                String.Format("attachment; filename=PdfSecuritySettings.pdf; size={0}", pdfBuffer.Length.ToString()));

            // write the PDF buffer to HTTP response
            HttpContext.Current.Response.BinaryWrite(pdfBuffer);

            // call End() method of HTTP response to stop ASP.NET page processing
            HttpContext.Current.Response.End();
        }

        private bool IsDefaultPermission(PdfSecurity pdfSecurity)
        {
            if (!pdfSecurity.AllowAssembling)
                return false;
            if (!pdfSecurity.AllowCopyAccessibilityContent)
                return false;
            if (!pdfSecurity.AllowCopyContent)
                return false;
            if (!pdfSecurity.AllowEditAnnotations)
                return false;
            if (!pdfSecurity.AllowEditContent)
                return false;
            if (!pdfSecurity.AllowFormFilling)
                return false;
            if (!pdfSecurity.AllowHighResolutionPrinting)
                return false;
            if (!pdfSecurity.AllowPrinting)
                return false;

            return true;
        }

        private PdfEncryptionMode GetSelectedEncryptionMode()
        {
            if (dropDownListEncryptionMode.SelectedValue == null)
                return PdfEncryptionMode.RC4;

            switch (dropDownListEncryptionMode.SelectedValue)
            {
                case "RC4":
                    return PdfEncryptionMode.RC4;
                case "AES":
                    return PdfEncryptionMode.AES;
                default:
                    return PdfEncryptionMode.RC4;
            }
        }

        private PdfEncryptionLevel GetSelectedEncryptionLevel()
        {
            if (dropDownListEncryptionLevel.SelectedValue == null)
                return PdfEncryptionLevel.High;

            switch (dropDownListEncryptionLevel.SelectedValue)
            {
                case "Low":
                    return PdfEncryptionLevel.Low;
                case "High":
                    return PdfEncryptionLevel.High;
                case "Very High":
                    return PdfEncryptionLevel.VeryHigh;
                default:
                    return PdfEncryptionLevel.High;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dropDownListEncryptionMode.SelectedValue = "RC4";
                dropDownListEncryptionLevel.SelectedValue = "High";

                Master.SelectNode("pdfSecuritySettings");

                Master.LoadCodeSample("PdfSecuritySettings");
            }
        }

        protected void dropDownListEncryptionMode_SelectedIndexChanged(object sender, EventArgs e)
        {
            PdfEncryptionMode encryptionMode = GetSelectedEncryptionMode();
            PdfEncryptionLevel encryptionLevel = GetSelectedEncryptionLevel();

            if (encryptionMode == PdfEncryptionMode.RC4 && encryptionLevel == PdfEncryptionLevel.VeryHigh)
                dropDownListEncryptionLevel.SelectedValue = "High"; // very high mode not supported for RC4
            if (encryptionMode == PdfEncryptionMode.AES && encryptionLevel == PdfEncryptionLevel.Low)
                dropDownListEncryptionLevel.SelectedValue = "High"; // low mode not supported for AES
        }

        protected void dropDownListEncryptionLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            PdfEncryptionMode encryptionMode = GetSelectedEncryptionMode();
            PdfEncryptionLevel encryptionLevel = GetSelectedEncryptionLevel();

            if (encryptionMode == PdfEncryptionMode.RC4 && encryptionLevel == PdfEncryptionLevel.VeryHigh)
                dropDownListEncryptionLevel.SelectedValue = "High"; // very high mode not supported for RC4
            if (encryptionMode == PdfEncryptionMode.AES && encryptionLevel == PdfEncryptionLevel.Low)
                dropDownListEncryptionLevel.SelectedValue = "High"; // low mode not supported for AES
        }

        protected void checkBoxAllowFillForms_CheckedChanged(object sender, EventArgs e)
        {
            if (!checkBoxAllowFillForms.Checked)
            {
                checkBoxAllowEdit.Checked = false;
                checkBoxAllowEditAnnotations.Checked = false;
            }
        }

        protected void checkBoxAllowEdit_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxAllowEdit.Checked)
                checkBoxAllowFillForms.Checked = true;
        }

        protected void checkBoxAllowEditAnnotations_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxAllowEditAnnotations.Checked)
                checkBoxAllowFillForms.Checked = true;
        }
    }
}