HiQPdf Documentation

PDF Security Settings

Quickly Create High Quality PDFs

HiQPdf HTML to PDF Converter for .NET offers you multiple possibilities to secure and to set the permitted operations on the generated PDF document. The security settings of the PDF document are controlled by the PdfDocumentControlSecurity property. You can password protect the PDF document with separate passwords to open the PDF document and to edit the permissions. You can also enable or disable the 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 type of encryption algorithm you can also set the encryption key size which gives the encryption level. AES algorithm supports high 128-bit and very high 256-bit encryption key sizes and the RC4 algorithm supports low 40-bit and high 128-bit encryption key sizes.

PDF Security Settings

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.

Demo Source Code

C#
private void buttonConvertToPdf_Click(object sender, EventArgs e)
{
    // create the HTML to PDF converter
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

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

    Cursor = Cursors.WaitCursor;

    // convert HTML to PDF
    string pdfFile = Application.StartupPath + @"\DemoOut\PdfSecuritySettings.pdf";
    try
    {
        // convert URL
        string url = textBoxUrl.Text;

        // ConvertUrlToFile() is called to convert the HTML document and save the resulted PDF into a file on disk
        // Alternatively, ConvertUrlToMemory() can be called to save the resulted PDF in a buffer in memory
        htmlToPdfConverter.ConvertUrlToFile(url, pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Conversion failed. {0}", ex.Message));
        return;
    }
    finally
    {
        Cursor = Cursors.Arrow;
    }

    // open the PDF document
    try
    {
        System.Diagnostics.Process.Start(pdfFile);
    }
    catch (Exception ex)
    {
        MessageBox.Show(String.Format("Conversion succeeded but cannot open '{0}'. {1}", pdfFile, ex.Message));
    }
}
See Also

Other Resources