How to Set Permissions on an Encrypted & Password-Protected PDF using C# .NET Framework
There are a few important things to consider when we code workflows to secure our PDF documents. Should we allow unsolicited viewers to print our content, create annotations, or fill in form-field elements?
Using the below code, we can take advantage of a free API that allows us to set restricted permissions on a PDF document after we encrypt and password-protect it. We can use this solution to quickly & easily customize basic security policies for our PDF documents without needing to use a special PDF-editing application.
Let’s start by installing the SDK. We can install via NuGet by running the below command in our Package Manager console:
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2
Next, let’s take care of authorization. We’ll need a free-tier API key to make up to 800 API calls per month — and this comes with zero commitments. Our total will simply reset the following month once we reach it.
Finally, let’s copy the below code and customize our request parameters. We’ll need to set owner & user passwords, and after that we can set restricted permissions via Booleans:
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class EditPdfSetPermissionsExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new EditPdfApi();
var ownerPassword = ownerPassword_example; // string | Password of a owner (creator/editor) of the PDF file (required)
var userPassword = userPassword_example; // string | Password of a user (reader) of the PDF file (optional)
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
var encryptionKeyLength = encryptionKeyLength_example; // string | Possible values are \"128\" (128-bit RC4 encryption) and \"256\" (256-bit AES encryption). Default is 256. (optional)
var allowPrinting = true; // bool? | Set to false to disable printing through DRM. Default is true. (optional)
var allowDocumentAssembly = true; // bool? | Set to false to disable document assembly through DRM. Default is true. (optional)
var allowContentExtraction = true; // bool? | Set to false to disable copying/extracting content out of the PDF through DRM. Default is true. (optional)
var allowFormFilling = true; // bool? | Set to false to disable filling out form fields in the PDF through DRM. Default is true. (optional)
var allowEditing = true; // bool? | Set to false to disable editing in the PDF through DRM (making the PDF read-only). Default is true. (optional)
var allowAnnotations = true; // bool? | Set to false to disable annotations and editing of annotations in the PDF through DRM. Default is true. (optional)
var allowDegradedPrinting = true; // bool? | Set to false to disable degraded printing of the PDF through DRM. Default is true. (optional)
try
{
// Encrypt, password-protect and set restricted permissions on a PDF
byte[] result = apiInstance.EditPdfSetPermissions(ownerPassword, userPassword, inputFile, encryptionKeyLength, allowPrinting, allowDocumentAssembly, allowContentExtraction, allowFormFilling, allowEditing, allowAnnotations, allowDegradedPrinting);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditPdfApi.EditPdfSetPermissions: " + e.Message );
}
}
}
}
Just like that, we can take full control over our PDF security with a single low-code solution.