How to Remove PDF Password in .NET with C#

Learn how to remove a PDF password using the pdfRest Decrypt PDF API Tool with C#
Share this page

Why Remove PDF Password with C#?

The pdfRest Encrypt PDF API Tool allows you to securely access and process password-protected PDF documents. In this tutorial, we'll show you how to send an API call using C# to Remove a PDF Password. This is valuable for organizations that receive password-protected documents from external sources or need to access archived PDFs with passwords.

Here's a common scenario where Removing a PDF Password comes into play:

Imagine a legal firm receives password-protected witness testimonies from another law office. These documents are crucial for ongoing litigation but require decryption before lawyers can review and analyze the content.

Using the Encrypt PDF Tool, the firm can securely remove the password, allowing authorized personnel to access the information within the document and proceed with their casework efficiently. This eliminates the need to remember and share the password as files are internally reviewed.

Remove PDF Password with C# Code Example

using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
    using (var request = new HttpRequestMessage(HttpMethod.Post, "decrypted-pdf"))
    {
        request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
        request.Headers.Accept.Add(new("application/json"));
        var multipartContent = new MultipartFormDataContent();

        var byteArray = File.ReadAllBytes("/path/to/file.pdf");
        var byteAryContent = new ByteArrayContent(byteArray);
        multipartContent.Add(byteAryContent, "file", "file_name.pdf");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

        var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("password"));
        multipartContent.Add(byteArrayOption, "current_open_password");


        request.Content = multipartContent;
        var response = await httpClient.SendAsync(request);

        var apiResult = await response.Content.ReadAsStringAsync();

        Console.WriteLine("API response received.");
        Console.WriteLine(apiResult);
    }
}

This code is sourced from the pdfRest API samples repository on GitHub.

Breaking Down the Code

The provided C# code snippet demonstrates how to make a multipart/form-data POST request to the pdfRest API to decrypt a PDF file. Let's break down the code into its key components:

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })

This creates an instance of HttpClient with the base address set to the pdfRest API endpoint.

using (var request = new HttpRequestMessage(HttpMethod.Post, "decrypted-pdf"))

An HttpRequestMessage is constructed with the POST method targeting the "decrypted-pdf" endpoint.

request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

The API key is added to the request headers to authenticate the request. Replace "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" with your actual API key.

var byteArray = File.ReadAllBytes("/path/to/file.pdf");
var byteAryContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteAryContent, "file", "file_name.pdf");

A byte array of the PDF file is read from the specified path, then added to the multipart content with the form key "file" and the file name "file_name.pdf". Replace "/path/to/file.pdf" with the actual file path and "file_name.pdf" with the desired name for the file.

var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("password"));
multipartContent.Add(byteArrayOption, "current_open_password");

The password for decrypting the PDF is encoded in UTF-8 and added to the multipart content with the form key "current_open_password". Replace "password" with the actual password used to encrypt the PDF.

var response = await httpClient.SendAsync(request);
var apiResult = await response.Content.ReadAsStringAsync();

The request is sent asynchronously, and the response is read as a string. The result is then printed to the console.

Beyond the Tutorial

In this tutorial, we've learned how to use C# to make an API call to the pdfRest Decrypt PDF API to remove a password from a PDF document. This process is essential for anyone needing to distribute encrypted PDF files to a wider audience. By following the steps outlined, you can integrate PDF decryption into your own applications.

To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information on the API endpoints and their parameters, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdfRest API samples repository on GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us