How to Encrypt PDF Files in .NET with C#
Why Encrypt PDF with C#?
The pdfRest Encrypt PDF API Tool allows developers to secure their PDF files by encrypting them with a password. This tutorial will demonstrate how to send an API call to Encrypt PDF with C#.
Encrypting a PDF can be crucial in scenarios where sensitive information, such as financial reports, legal documents, or personal data, needs to be transmitted or stored securely. By encrypting the PDF, you can ensure that only authorized individuals with the password can access the content of the document.
Encrypt PDF 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, "encrypted-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, "new_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); } }
Source code reference: pdfRest Encrypt PDF API C# Sample
Breaking Down the C# Code
The above code snippet demonstrates how to make a multipart/form-data POST request to the pdfRest Encrypt PDF API endpoint using C#. Let's break down the key parts of this code:
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
This initializes a new instance of the HttpClient class and sets the base address for the API.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
This line adds your API key to the request headers, which is necessary for authentication with the pdfRest API.
var byteArray = File.ReadAllBytes("/path/to/file.pdf"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name.pdf");
Here, the PDF file is read into a byte array, wrapped in a ByteArrayContent object, and added to the multipart form data content with the name "file". The "file_name" is the name of the file as it will appear in the form data.
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("password")); multipartContent.Add(byteArrayOption, "new_open_password");
This part creates another ByteArrayContent object containing the password for encrypting the PDF. It is then added to the multipart content with the name "new_open_password".
var response = await httpClient.SendAsync(request);
The HttpClient sends the POST request asynchronously and awaits the response from the server.
var apiResult = await response.Content.ReadAsStringAsync();
Finally, the response content is read as a string, which contains the result of the API call.
Beyond the Tutorial
In this tutorial, we've learned how to encrypt a PDF file using the pdfRest Encrypt PDF API and C#. This process involves sending a multipart/form-data request with the file and encryption password. After the API call, the server responds with the encrypted PDF or an error message if something went wrong.
To further explore the capabilities of pdfRest and test the API tools, you can visit the API Lab at API Lab. For comprehensive documentation on all available endpoints and parameters, refer to the API Reference at Cloud API Reference Guide.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at JSON Payload Example.