Windows
ComPDFKit PDF SDK
Guides

Security

 

ComPDFKit PDF SDK protects the content of PDF documents from unauthorized access like copying or printing. It offers developers a way to encrypt and decrypt PDFs, add a password, insert watermark, and more. For controlling over document security in ComPDFKit PDF SDK, security handlers perform user authorization and sets various permissions over PDF documents.

 

PDF Permission

 

A PDF file can have two different passwords set, a permissions or owner password and an open or user password.

A user password (also known as an open password) requires a user to type a password to open the PDF. If you want to open a document with a user password programmatically, you can use the CPDFDocument.UnlockWithPassword(string password) API.

 

An owner password (also known as a permissions password) requires a password to change permission settings. When an owner password is set, you can configure a set of permissions. For example, you can configure an owner password and the “printing” permission when saving a document to make sure that users who don’t know that owner password can only print the document, but not modify it.

 

The PDF specification defines the permissions shown below:

- Printing — print the document.

- High-quality printing — print the document in high fidelity.

- Copying — copy content from the document.

- Document changes — modify the document contents except for document attributes.

- Document assembly — insert, delete, and rotate pages.

- Commenting — create or modify document annotations, including form field entries.

- Form Field Entry — modify form field entries even if you can't edit document annotations.

 

To access the corresponding permissions, use function CPDFDocument.GetPermissionsInfo().

/// <summary>
/// A Boolean value indicating whether the document allows printing.
/// </summary>
public bool AllowsPrinting { get; set; }

/// <summary>
/// A Boolean value indicating whether the document allows printing in high fidelity.
/// </summary>
public bool AllowsHighQualityPrinting { get; set; }

/// <summary>
/// A Boolean value indicating whether the document allows copying of content to the Pasteboard.
/// </summary>
public bool AllowsCopying { get; set; }

/// <summary>
/// A Boolean value indicating whether you can modify the document contents except for document attributes.
/// </summary>
public bool AllowsDocumentChanges { get; set; }

/// <summary>
/// A Boolean value indicating whether you can manage a document by inserting, deleting, and rotating pages.
/// </summary>
public bool AllowsDocumentAssembly { get; set; }

/// <summary>
/// A Boolean value indicating whether you can create or modify document annotations, including form field entries.
/// </summary>
public bool AllowsCommenting { get; set; }

/// <summary>
/// A Boolean value indicating whether you can modify form field entries even if you can't edit document annotations.
/// </summary>
public bool AllowsFormFieldEntry { get; set; }

 

Encrypt

 

ComPDFKit’s CPDFDocument API can generate a password-protected document. You can use CPDFDocument to create a new password-protected PDF document on disk based on a current document. The user password prevents users from viewing the PDF. If you specify it, you also need to specify an owner password.

 

To create a password-protected document:

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPermissionsInfo permission = new CPDFPermissionsInfo();
document.Encrypt("UserPassword", "ownerPassword", permission);
document.WriteToFilePath("savePath");

 

Decrypt

 

ComPDFKit PDF SDK fully supports the reading of secured and encrypted PDF documents.

 

- To check whether a document requires a password:

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
if(document != null && document.ErrorType == CPDFDocumentError.CPDFDocumentPasswordError)
{
    // Password required
}

- To read a PDF document with password protection, use functionCPDFDocument.UnlockWithPassword(string password). If the password is correct, this method returns true. Once unlocked, you cannot use this function to relock the document.

 

- To remove PDF security, call the CPDFDocument.Descrypt(string filePath) method:

CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
document.Descrypt("savePath");