How to Decrypt and Unlock Password-Protected PDFs using C# .NET Framework

Cloudmersive
2 min readJan 22, 2024

--

Developing workflows around unlocking protected PDF documents can be a bit of a challenge.

Thankfully, using the below code, we can take advantage of a free & low-code API solution to handle that workflow for us. We can easily unlock PDFs by supplying our file data and our password in the request.

Let’s start by installing the SDK. We can install via NuGet by running the following command in our Package Manager console:

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

Next, let’s focus on authorization. We can easily authorize our API calls with a free-tier API key (this allows up to 800 API calls per month with no additional commitments — our total will just reset the following month after we reach it).

Let’s now copy the below code and supply our API key in the appropriate parameter. We can then unlock our PDFs with the appropriate password:

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 EditPdfDecryptExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new EditPdfApi();
var password = password_example; // string | Valid password for the PDF file
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.

try
{
// Decrypt and password-protect a PDF
byte[] result = apiInstance.EditPdfDecrypt(password, inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditPdfApi.EditPdfDecrypt: " + e.Message );
}
}
}
}

Just like that, we’re all done — no more code required.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet