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.
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.
- 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()
.
///
/// A Boolean value indicating whether the document allows printing.
///
public bool AllowsPrinting { get; set; }
///
/// A Boolean value indicating whether the document allows printing in high fidelity.
///
public bool AllowsHighQualityPrinting { get; set; }
///
/// A Boolean value indicating whether the document allows copying of content to the Pasteboard.
///
public bool AllowsCopying { get; set; }
///
/// A Boolean value indicating whether you can modify the document contents except for document attributes.
///
public bool AllowsDocumentChanges { get; set; }
///
/// A Boolean value indicating whether you can manage a document by inserting, deleting, and rotating pages.
///
public bool AllowsDocumentAssembly { get; set; }
///
/// A Boolean value indicating whether you can create or modify document annotations, including form field entries.
///
public bool AllowsCommenting { get; set; }
///
/// A Boolean value indicating whether you can modify form field entries even if you can't edit document annotations.
///
public bool AllowsFormFieldEntry { get; set; }
Encrypt
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.
CPDFDocumentNoEncryptAlgo
, it means that the document is not encrypted.
public enum CPDFDocumentEncryptAlgo
{
CPDFDocumentRC4,
CPDFDocumentAES128,
CPDFDocumentAES256,
CPDFDocumentNoEncryptAlgo
}
CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
CPDFPermissionsInfo permission = new CPDFPermissionsInfo();
permission.AllowsPrinting = true;
CPDFDocumentEncryptAlgo algorithm = CPDFDocumentEncryptAlgo.CPDFDocumentAES256;
document.Encrypt("UserPassword", "ownerPassword", permission,algorithm);
document.WriteToFilePath("savePath");
Decrypt
CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
if(document != null && document.ErrorType == CPDFDocumentError.CPDFDocumentPasswordError)
{
// Password required
}
CPDFDocument.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)
CPDFDocument document = CPDFDocument.InitWithFilePath("filePath");
document.Descrypt("savePath");