HiQPdf Documentation

PDF Document Security

Quickly Create High Quality PDFs

HiQPdf PDF Library for .NET offers you multiple possibilities to secure and to set the permitted operations on a PDF document. The security settings of the PDF document are controlled by the PdfDocumentSecurity 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 set the security of PDF document containg the conversion to PDF of a HTML content from a given URL. 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#
protected void buttonConvertToPdf_Click(object sender, EventArgs e)
{
    // create an empty PDF document
    PdfDocument document = new PdfDocument();

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

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

    // set PDF document permissions
    document.Security.AllowPrinting = checkBoxAllowPrint.Checked;
    document.Security.AllowCopyContent = checkBoxAllowCopy.Checked;
    document.Security.AllowEditContent = checkBoxAllowEdit.Checked;
    document.Security.AllowEditAnnotations = checkBoxAllowEditAnnotations.Checked;
    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 (document.Security.PermissionsPassword == String.Empty &&
        (document.Security.OpenPassword != String.Empty || !IsDefaultPermission(document.Security)))
    {
        document.Security.PermissionsPassword = "admin";
    }

    // add a page to document with the default size and orientation
    PdfPage page1 = document.AddPage(PdfPageSize.A4, new PdfDocumentMargins(0), PdfPageOrientation.Portrait);

    // an object to be set with HTML layout info after conversion
    PdfLayoutInfo htmlLayoutInfo = null;
    try
    {
        // create the HTML object from URL
        PdfHtml htmlObject = new PdfHtml(textBoxUrl.Text);

        // optionally wait an additional time before starting the conversion
        htmlObject.WaitBeforeConvert = 2;

        // layout the HTML object in PDF
        htmlLayoutInfo = page1.Layout(htmlObject);

        // write the PDF document to a memory buffer
        byte[] pdfBuffer = document.WriteToMemory();

        // 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 and the file name
        HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename=PdfDocumentSecuritySettings.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();
    }
    finally
    {
        document.Close();
    }
}
See Also

Other Resources