How to Encrypt/Add Password & Permissions to PDF Files

Securing PDF files with passwords and specific permissions is vital for protecting sensitive documents from unauthorized access and manipulation. The [POST] Encrypt/Add Password & Permissions API from IronSecure Doc provides an efficient way to encrypt PDF files with user and owner passwords while configuring permissions for printing, form filling, annotations, and more. This API ensures that the PDF is fully protected and access is controlled, making it ideal for legal, financial, or personal documents.

Pull and Start IronSecureDoc

If you don't have IronSecureDoc running yet, please follow the links below to get it set up:

Host LocallyDeploy to Cloud

The [POST] Encrypt/Add Password & Permissions API

The [POST] Encrypt/Add Password & Permissions API allows you to add security layers to PDF documents by encrypting them with passwords and setting specific permissions for various operations. Whether it's granting read access, allowing form filling, or disabling printing, this API gives you control over how your PDF files are accessed and modified.

Swagger

Swagger offers a user-friendly interface to test this API interactively. You can use it to send test requests and receive responses without writing code, making it a great tool for developers.

Steps to Redact Text with Swagger

  1. Access the Swagger UI:

If your API server is running locally, you can access Swagger by navigating to http://localhost:8080/swagger/index.html in your web browser.

Swagger docs

  1. Locate the API:

Find the [POST] /v1/document-services/pdfs/encrypt API, which corresponds to Encrypt/Add Password & Permissions.

Redact text

  1. Input Parameters:

Provide the required parameters for the API. You can upload a sample PDF file and specify the desired permissions.

  1. Upload PDF File:

In the request body, upload a PDF file as pdf_file and define the necessary permissions, such as allowing or restricting printing, form filling, and content extraction.

  1. Execute the Request:

Once all parameters are set, click "Execute" to send the request. The response will return the encrypted PDF with the specified security settings.

  1. Check the Output PDF:

Type in 'password' to view the PDF content.


Understanding Input Parameters

The API requires specific parameters to encrypt the PDF and assign permissions. Below is a breakdown of both required and optional parameters:

Required Parameters

NameData TypeDescription
pdf_fileapplication/pdfThe PDF file you want to encrypt.
allow_extracting_contentbooleanIndicate whether content extraction is allowed.
allow_form_fillingbooleanIndicate whether form filling is allowed.
allow_annotationsbooleanIndicate whether annotations are allowed.
allow_printingbooleanIndicate whether printing is allowed.
allow_modificationsbooleanIndicate whether modifications are allowed.
new_owner_passwordstringThe new owner password for the PDF, which grants full access and the ability to change permissions.

Optional Parameters

NameData TypeDescription
user_passwordstringThe current user password, required if the PDF has a user password to grant read access.
owner_passwordstringThe current owner password, required if the PDF has an owner password to grant full access.
new_user_passwordstringThe new user password for read access.
save_as_pdfabooleanSave the PDF as PDF/A-3 compliant.
save_as_pdfuabooleanSave the PDF as PDF/UA compliant.
authorstringSet the PDF metadata Author property.
titlestringSet the PDF metadata Title property.
subjectstringSet the PDF metadata Subject property.

API Integration: Python Example

Once you're familiar with the input parameters, you can call the API using Python or any other preferred language. Below is an example of how to integrate this API using Python.

import requests

# Endpoint URL
url = 'http://localhost:8080/v1/document-services/pdfs/encrypt'

# Headers with metadata information
headers = {
    'accept': '*/*',
    'author': 'IronSoftware',
    'title': 'ENCRYPTION DEMO 2024',
    'subject': 'DEMO EXAMPLE'
}

# Files to be sent in the request
files = {
    'pdf_file': ('sample.pdf', open('sample.pdf', 'rb'), 'application/pdf')
}

# Parameters for encryption and permissions
data = {
    'allow_extracting_content': 'true',
    'allow_form_filling': 'true',
    'allow_annotations': 'false',
    'allow_printing': 'false',
    'allow_modifications': 'true',
    'new_owner_password': 'password',
    'new_user_password': 'password',
    'save_as_pdfa': 'false',
    'save_as_pdfua': 'false'
}

# Send POST request with parameters
response = requests.post(url, headers=headers, files=files, data=data)

# Save the encrypted PDF to file system
with open('sample_encrypted.pdf', 'wb') as f:
    f.write(response.content)

print('PDF encrypted successfully.')
import requests

# Endpoint URL
url = 'http://localhost:8080/v1/document-services/pdfs/encrypt'

# Headers with metadata information
headers = {
    'accept': '*/*',
    'author': 'IronSoftware',
    'title': 'ENCRYPTION DEMO 2024',
    'subject': 'DEMO EXAMPLE'
}

# Files to be sent in the request
files = {
    'pdf_file': ('sample.pdf', open('sample.pdf', 'rb'), 'application/pdf')
}

# Parameters for encryption and permissions
data = {
    'allow_extracting_content': 'true',
    'allow_form_filling': 'true',
    'allow_annotations': 'false',
    'allow_printing': 'false',
    'allow_modifications': 'true',
    'new_owner_password': 'password',
    'new_user_password': 'password',
    'save_as_pdfa': 'false',
    'save_as_pdfua': 'false'
}

# Send POST request with parameters
response = requests.post(url, headers=headers, files=files, data=data)

# Save the encrypted PDF to file system
with open('sample_encrypted.pdf', 'wb') as f:
    f.write(response.content)

print('PDF encrypted successfully.')
PYTHON

Steps Explained:

  • Load the PDF: The PDF file to be encrypted is loaded from the local file system.
  • Set Encryption Parameters: Define permissions like allowing printing, form filling, and content extraction, as well as setting new passwords.
  • Call the API: The [POST] Encrypt/Add Password & Permissions API is called, passing the necessary parameters.
  • Save the Result: The encrypted PDF is saved as a new file.

The PDF file is encrypted as shown below. Type in 'password' to view the PDF content.

Frequently Asked Questions

How do I encrypt a PDF file to protect sensitive information?

To encrypt a PDF file, you can use the IronSecureDoc API, which allows you to set both user and owner passwords. These passwords help control access and permissions, such as printing and form filling, ensuring the document's security.

What steps are involved in using the IronSecureDoc API for PDF encryption?

First, pull the IronSecureDoc Docker Image and set it up according to your hosting environment. Next, use the API to specify parameters for encryption, such as setting passwords and permissions, and execute the API call to produce a secured PDF.

How can I test the IronSecureDoc API without writing code?

You can test the IronSecureDoc API using Swagger. Access the Swagger UI, find the relevant API endpoint, input the necessary parameters, and execute the request to see the response and ensure the API is working correctly.

What are user and owner passwords in PDF security?

In PDF security, the user password allows reading the document, while the owner password grants full access, including changing permissions. With IronSecureDoc, you can set both types of passwords to enhance document security.

Can I configure a PDF to be PDF/A compliant using IronSecureDoc?

Yes, the IronSecureDoc API includes parameters that allow you to save the encrypted PDF as PDF/A-3 compliant, which is a standard for long-term preservation of electronic documents.

What permissions can be controlled with the IronSecureDoc API?

Using the IronSecureDoc API, you can control permissions such as printing, form filling, content extraction, annotations, and modifications, ensuring that PDFs are used in a manner consistent with your security policies.

How can I integrate the IronSecureDoc API with a Python application?

To integrate the IronSecureDoc API with Python, utilize the requests library to send a POST request to the API endpoint. Include necessary headers, parameters, and file data in your request to encrypt and manage your PDF files.

What parameters are essential for encrypting a PDF using IronSecureDoc?

Essential parameters include the PDF file to encrypt, user and owner passwords, and permissions for actions like printing and editing. These parameters ensure the document's security and control over its usage.

Is it possible to modify metadata in a PDF during encryption with IronSecureDoc?

Yes, you can modify PDF metadata such as author, title, and subject using IronSecureDoc API's optional parameters, allowing you to customize the document's information during the encryption process.

What are the hosting requirements for running IronSecureDoc locally?

IronSecureDoc can be hosted locally on Windows, Mac, or Linux systems. Follow the specific setup tutorials for your operating system to ensure correct installation and operation of the service.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?