The pdfRest Encrypt PDF API Tool is a powerful resource for developers looking to secure their PDF documents programmatically. By using this tool, you can encrypt PDF files to ensure that sensitive information is protected and only accessible to individuals with the correct password. For documents that already have a password set, you can also use this tool to change its password. This tutorial will guide you through the process of sending an API call to Encrypt PDF using C# programming language, which is a common language for backend development and scripting in Windows environments.
Imagine you're working on a project with a client who sends you a confidential document, like a new product design or a marketing strategy. You need to access the document to collaborate, but also ensure its security. You can utilize the Encrypt PDF API to set a new password on the document before saving or sharing it further within your team. This two-step process ensures you can collaborate on the document while keeping the information secure, even if someone intercepts it during transmission.
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 byteArrayOption1 = new ByteArrayContent(Encoding.UTF8.GetBytes("CURRENTPASSWORD")); multipartContent.Add(byteArrayOption1, "current_open_password"); var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("NEWPASSWORD")); multipartContent.Add(byteArrayOption2, "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); } }
Reference: pdfRest C# Sample Code
The code begins by creating an instance of HttpClient
with the base address set to the pdfRest API endpoint. A new HttpRequestMessage
is then constructed for the POST method to the 'encrypted-pdf' endpoint.
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) { using (var request = new HttpRequestMessage(HttpMethod.Post, "encrypted-pdf")) { ... } }
The API key is added to the request headers, which is required for authentication. The content type that the client accepts is also specified as 'application/json'.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); request.Headers.Accept.Add(new("application/json"));
A MultipartFormDataContent
object is created to hold the file content, the current encryption password, and the new encryption password. The PDF file is read into a byte array and added to the multipart content with the appropriate content type. The password is also encoded into a byte array and added to the multipart content.
var multipartContent = new MultipartFormDataContent(); ... multipartContent.Add(byteArrayOption, "new_open_password");
The multipart content is then assigned to the request, which is sent asynchronously. The response is read as a string and printed to the console.
request.Content = multipartContent; var response = await httpClient.SendAsync(request); ... Console.WriteLine(apiResult);
In this tutorial, we've learned how to use C# to send a multipart API call to pdfRest's Encrypt PDF API. This allows us to modify a PDF document's password, ensuring that only authorized individuals with the new password can open the document. By following this example, you can integrate PDF password modification into your own applications, adding an essential security feature for handling sensitive documents.
For further exploration and to demo all of the pdfRest API Tools, visit the API Lab. For more detailed information on the API, check out the API Reference Guide.
Note: This is an example of a multipart API call. If you are interested in code samples using JSON payloads, you can find them at pdfRest C# JSON Payload Sample Code.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.