The pdfRest Restrict PDF API Tool is a robust resource for developers aiming to programmatically remove restrictions from PDF documents. This API facilitates a variety of security adjustments, including the removal of password protection and permission limitations, which are essential for maximizing how a PDF is utilized, edited, or shared. This tutorial will guide you through the process of lifting restrictions by sending an API call to the /unrestricted-pdf endpoint of the pdfRest API using C#.
In practical scenarios, the need to unlock a PDF document arises frequently. For example, a company might require to lift access limitations from their financial reports to share with a broader audience, or an author may decide to remove copy or print protection from their work. With the pdfRest API, users can programmatically eliminate these restrictions, thus enhancing flexibility and accessibility of their digital assets.
using System.Text; using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") }) { using (var request = new HttpRequestMessage(HttpMethod.Post, "unrestricted-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"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name"); byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf"); var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("password")); multipartContent.Add(byteArrayOption, "current_permissions_password"); request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine("API response received."); Console.WriteLine(apiResult); } }
Code source: pdf-rest-api-samples on GitHub
This code snippet illustrates how to execute a multipart API call to the pdfRest API's /unrestricted-pdf endpoint to remove restrictions from a PDF using C#. Below are the breakdowns of the key components in this code:
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
This line initializes a new instance of the HttpClient class and sets the base address to the pdfRest API.
using (var request = new HttpRequestMessage(HttpMethod.Post, "unrestricted-pdf"))
Here, we create a new HttpRequestMessage with the HTTP method set to POST, targeting the "unrestricted-pdf" endpoint.
request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
This line adds your API key to the request headers, which is required for authentication with the pdfRest API.
var byteArray = File.ReadAllBytes("/path/to/file"); var byteAryContent = new ByteArrayContent(byteArray); multipartContent.Add(byteAryContent, "file", "file_name");
These lines read the PDF file into a byte array, create a ByteArrayContent from it, and add it to the multipart form data content with the key "file".
var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("password")); multipartContent.Add(byteArrayOption, "current_permissions_password");
This snippet creates a ByteArrayContent containing the password for the current permissions (if the PDF is already password protected) and adds it to the multipart content.
var response = await httpClient.SendAsync(request);
This line sends the assembled request to the pdfRest API and awaits the response.
var apiResult = await response.Content.ReadAsStringAsync();
After receiving the response, this line reads the content of the response asynchronously and stores it in the variable "apiResult".
Following the tutorial above, you've learned how to make a multipart API call to the pdfRest API to lift restrictions from PDF documents using C#. This capability enables you to enhance the accessibility and usability of your digital content. With this foundation, you're encouraged to explore other features provided by the pdfRest API Tools in the API Lab at pdfRest API Lab. For comprehensive details on the API, consider reviewing the API Reference Guide.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples on GitHub.
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.