Add restrictions to a PDF

Add restrictions to prevent copying, printing, and modifying a PDF document.

Endpoints

POST https://api.pdfblocks.com/v1/add_restrictions

Global endpoint (default)

POST https://eu.api.pdfblocks.com/v1/add_restrictions

European-only endpoint

POST https://{base_url}/v1/add_restrictions

Self-hosted server endpoint

Parameters

The endpoint accepts multipart/form-data request bodies with the folowing parameters:

file Required

The content of the input PDF document.

owner_password Required

The password required to open and change permissions of the PDF document.

  • The length is between 4 and 32 characters.
  • The characters in the password must be in the ASCII printable range (between positions 32 and 126). This range includes lowercase and uppercase letters, numbers, space, and some commonly used symbols like . , : ; # $ % & ( ) * + = ? !
  • Both conditions as a regex pattern: ^[\x20-\x7e]{4,32}$

user_password optional

The password required to open the PDF document.

  • If the user_password is set, the user will be able to open the document with either the user_password or the owner_password.
  • If the user_password is not set, the user will be able to open the document without a password.
  • The length is between 4 and 32 characters.
  • The characters in the password must be in the ASCII printable range (between positions 32 and 126). This range includes lowercase and uppercase letters, numbers, space, and some commonly used symbols like . , : ; # $ % & ( ) * + = ? !
  • Both conditions as a regex pattern: ^[\x20-\x7e]{4,32}$

encryption_algorithm optional

The algorithm used to encrypt the file.

  • Valid values are AES-128 and AES-256
  • The default value is AES-128

allow_copy_content optional

A boolean value indicating if the user can copy the text and images to the clipboard

  • The default value is true

allow_change_content optional

A boolean value indicating if the user can change the content of the document

  • The default value is true

allow_print optional

A boolean value indicating if the user can print the document

  • The default value is true

allow_print_high_resolution optional

A boolean value indicating if the user can print the document in high resolution

  • The default value is true

allow_comment_and_fill_form optional

A boolean value indicating if the user can add, edit and modify annotations and fill form fields

  • The default value is true

allow_fill_form optional

A boolean value indicating if the user can fill forms fields

  • The default value is true

allow_assemble_document optional

A boolean value indicating if the user can assemble and manipulate the document

  • The default value is true

allow_accessibility optional

A boolean value indicating if accessibility programs can read the text and images of the document

  • The default value is true

Returns

If successful, the call returns a response with Content-Type: application/pdf with the content of the restricted PDF document. Otherwise, this call returns an error.

Code examples

curl

curl \
-F file=@input.pdf \
-F owner_password='battery staple' \
-F encryption_algorithm=AES-128 \
-F allow_copy_content=false \
-F allow_change_content=false \
-F allow_print=false \
-F allow_print_high_resolution=false \
-F allow_comment_and_fill_form=true \
-F allow_fill_form=true \
-F allow_assemble_document=true \
-F allow_accessibility=true \
-o restricted.pdf \
-H 'X-Api-Key: your_api_key' \
https://api.pdfblocks.com/v1/add_restrictions

Python

# pip install requests

import requests

url = 'https://api.pdfblocks.com/v1/add_restrictions'

body = {
  'file' : open('input.pdf', 'rb'),
  'owner_password' : 'battery staple',
  'allow_copy_content' : false,
  'allow_change_content' : false,
  'allow_print' : false,
  'allow_print_high_resolution' : false,
  # 'allow_comment_and_fill_form' : true,
  # 'allow_fill_form' : true,
  # 'allow_assemble_document' : true,
  # 'allow_accessibility' : true,
  # 'encryption_algorithm' : 'AES-128',
}

headers = { 'X-Api-Key' : 'your_api_key' }

response = requests.post(url, files=body, headers=headers)

if response.status_code == 200:
  with open('restricted.pdf', 'wb') as output:
    for chunk in response.iter_content():
        output.write(chunk)

Ruby

# gem install http

require 'http'

url = 'https://api.pdfblocks.com/v1/add_restrictions'

body = {
  file: HTTP::FormData::File.new('input.pdf'),
  owner_password: 'battery staple',
  allow_copy_content: false,
  allow_change_content: false,
  allow_print: false,
  allow_print_high_resolution: false,
  # allow_comment_and_fill_form: true,
  # allow_fill_form: true,
  # allow_assemble_document: true,
  # allow_accessibility: true,
  # encryption_algorithm: 'AES-128',
}

headers = { 'X-Api-Key' => 'your_api_key' }

response = HTTP.post(url, form: body, headers: headers)

if response.status.ok?
  File.open('restricted.pdf', 'wb') do |output|
    response.body.each { |chunk| output.write(chunk) }
  end
end

C# (.NET Core)

// Using .NET Core 3.1

using System.IO;
using System.Net.Http;

class Program
{
    static void Main()
    {
        string url = "https://api.pdfblocks.com/v1/add_restrictions";

        var inputFile = new FileStream("input.pdf", FileMode.Open, FileAccess.Read);

        var body = new MultipartFormDataContent();
        body.Add(new StreamContent(inputFile), "file");
        body.Add(new StringContent("correct horse"), "owner_password");
        body.Add(new StringContent("false"), "allow_copy_content");
        body.Add(new StringContent("false"), "allow_change_content");
        body.Add(new StringContent("false"), "allow_print");
        body.Add(new StringContent("false"), "allow_print_high_resolution");
        // body.Add(new StringContent("true"), "allow_comment_and_fill_form");
        // body.Add(new StringContent("true"), "allow_fill_form");
        // body.Add(new StringContent("true"), "allow_assemble_document");
        // body.Add(new StringContent("true"), "allow_accessibility");
        // body.Add(new StringContent("AES-128"), "encryption_algorithm");

        var message = new HttpRequestMessage(HttpMethod.Post, url);
        message.Content = body;
        message.Headers.Add("X-Api-Key", "your_api_key");

        var client = new HttpClient();
        var response = client.SendAsync(message).Result;
        if(response.IsSuccessStatusCode)
        {
            using var output = new FileStream("restricted.pdf", FileMode.Create, FileAccess.Write);
            response.Content.CopyToAsync(output).Wait();
        }
    }
}