How to Restrict PDF in .NET with C#

Learn how to apply PDF file restrictions protected with a permissions password using #C and the pdfRest Restrict PDF API Tool
Share this page

Why Use Restrict PDF with C#?

The pdfRest Restrict PDF API Tool is a powerful resource for developers looking to programmatically apply restrictions to PDF documents. By using this tool, you can control how the PDF can be used, such as preventing editing, printing, or copying of content. For instance, if you're distributing proprietary documents and want to ensure they are not altered or copied without permission, the Restrict PDF API can help you enforce these constraints.

This tutorial will guide you through the process of sending an API call to Restrict PDF using C#. Let's imagine you're developing a document management system and need to restrict access to sensitive financial reports before sharing them with certain stakeholders. The Restrict PDF API can be used to set a password and apply the necessary restrictions programmatically.

Restrict 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, "restricted-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_permissions_password");

        var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("copy_content"));
        multipartContent.Add(byteArrayOption2, "restrictions[]");
        var byteArrayOption3 = new ByteArrayContent(Encoding.UTF8.GetBytes("edit_content"));
        multipartContent.Add(byteArrayOption3, "restrictions[]");

        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 API C# Sample Code

Breaking Down the Code

The code snippet provided demonstrates how to make a multipart/form-data POST request to the pdfRest API's Restrict PDF endpoint using C#.

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

The HttpClient is initialized with the base address of the pdfRest API.

using (var request = new HttpRequestMessage(HttpMethod.Post, "restricted-pdf"))
{
    //...
}

A new HttpRequestMessage is created for the POST method to the "restricted-pdf" endpoint.

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

The API key is added to the request headers. 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");

The PDF file is read as a byte array and added to the multipart form data with the key "file". Replace "/path/to/file.pdf" with the path to your PDF file and "file_name.pdf" with the actual file name.

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

A new permissions password is set for the PDF. Replace "password" with the desired password.

var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("copy_content"));
multipartContent.Add(byteArrayOption2, "restrictions[]");

Restrictions are added to the PDF. In this example, "copy_content" is specified to prevent copying of content. Other restrictions can be added similarly.

Beyond the Tutorial

In this tutorial, you learned how to use C# to make an API call to the pdfRest Restrict PDF endpoint. By sending a properly formatted multipart/form-data request, you can add restrictions to a PDF document and set a permissions password. This is particularly useful when dealing with sensitive information that requires controlled distribution.

Feel free to demo all of the pdfRest API Tools in the API Lab at API Lab and refer to the API Reference documentation at Cloud API Reference Guide for more details on how to use the API.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at JSON Payload Example.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.