Tutorials

Secure PDF Files with a Password in C#

By ComPDFKit | Thu. 15 Jun. 2023
SecurityC#

With the rise of digital documents, most companies need to share some files with others while they also want to protect PDFs or other types of files from being reshared, copied, printed, or edited. Anytime you want to secure something, a lock can figure it out. So do the documents. We came up with ways to lock our documents nowadays. We will show you how to secure your PDF files programmatically. Let’s start with the key. 

 

 

Password Types

 

There are two ways to protect a document with passwords: A user password or an owner 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 some 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 this document, but not modify it.

 

 

Security Permission

 

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 the 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

 

ComPDFKit PDF SDK’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 the function 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) method:

  

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

 

 

Watermark

 

Adding a non-removable watermark to documents can discourage viewers from sharing your content or taking screenshots.

 

- To add a watermark, use the function CPDFDocument.CreateWatermark(C_Watermark_Type type).

- To remove all the watermarks in a PDF document, use the function CPDFDocument.DeleteWatermarks().

 

How to generate a PDF with a watermark on all its pages using the CPDFDocument API:

 

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

CPDFWatermark watermark = document.InitWatermark(C_Watermark_Type.WATERMARK_TYPE_TEXT);
watermark.SetText("test");
watermark.SetScale(2);
watermark.SetRotation(0);
watermark.SetOpacity(120);
watermark.SetVertalign(C_Watermark_Vertalign.WATERMARK_VERTALIGN_CENTER);
watermark.SetHorizalign(C_Watermark_Horizalign.WATERMARK_HORIZALIGN_CENTER);
watermark.SetVertOffset(0);
watermark.SetHorizOffset(0);
watermark.CreateWatermark();

document.WriteToFilePath("savePath");

 

 

Conclusion

 

Now you have learned different ways to protect sensitive PDF documents from unauthorized access. You can apply a password to open a document or permit some operations like printing, copying, editing, etc. Please consult our website for more about our ComPDFKit PDF SDK.