encHow to encrypt, password-protect and set restricted permissions on a PDF in C# .NET Framework

Today we will be laying out all your PDF security needs. With one API function, we can set password permissions for viewing and editing, encrypt our file, as well as control permissions for printing, copying, form filling, annotations, DRM, and more.

To get the ball rolling, we must install our API client. Using NuGet, we simply run this command in the console of your Package Manager to get things chugging along.

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 2.2.0

Next step, call EditPdfSetPermissions:

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: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");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.Stream(); // 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 PDFbyte[] 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 );}}}}

As you can see, there are plenty of security and permission options available. After setting these to your needs, simply drop in your PDF file and you are good to go!

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store