How to Encrypt/Add Password & Permissions to PDF Files in C#
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.
How to Encrypt/Add Password & Permissions to PDF Files
- Pull and start the IronSecureDoc Docker Image
- Test the API using Swagger
- Specify the parameters for password and permissions
- Execute the API call to apply the changes
- Export the encrypted PDF document
Pull and Start IronSecureDoc
If you don't have IronSecureDoc running yet, please follow the links below to get it set up:
| Host Locally | Deploy 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
- 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.

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

- Input Parameters:
Provide the required parameters for the API. You can upload a sample PDF file and specify the desired permissions.
- 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.
- 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.
- 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
| Name | Data Type | Description |
|---|---|---|
pdf_file | application/pdf | The PDF file you want to encrypt. |
allow_extracting_content | boolean | Indicate whether content extraction is allowed. |
allow_form_filling | boolean | Indicate whether form filling is allowed. |
allow_annotations | boolean | Indicate whether annotations are allowed. |
allow_printing | boolean | Indicate whether printing is allowed. |
allow_modifications | boolean | Indicate whether modifications are allowed. |
new_owner_password | string | The new owner password for the PDF, which grants full access and the ability to change permissions. |
Optional Parameters
| Name | Data Type | Description |
|---|---|---|
user_password | string | The current user password, required if the PDF has a user password to grant read access. |
owner_password | string | The current owner password, required if the PDF has an owner password to grant full access. |
new_user_password | string | The new user password for read access. |
save_as_pdfa | boolean | Save the PDF as PDF/A-3 compliant. |
save_as_pdfua | boolean | Save the PDF as PDF/UA compliant. |
author | string | Set the PDF metadata Author property. |
title | string | Set the PDF metadata Title property. |
subject | string | Set 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': 'Iron Software',
'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.')
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 can I encrypt a PDF file using IronSecureDoc?
You can encrypt a PDF file using IronSecureDoc by utilizing the [POST] Encrypt/Add Password & Permissions API. This allows you to set user and owner passwords and configure specific permissions for the PDF document.
What permissions can be configured with the IronSecureDoc API?
The IronSecureDoc API allows you to configure permissions for printing, form filling, annotations, content extraction, and modifications to ensure the desired level of access and security for your PDF documents.
Is it possible to test the IronSecureDoc API without writing code?
Yes, you can test the IronSecureDoc API interactively using Swagger, which provides a user-friendly interface to send and receive requests and responses, offering a code-free testing experience.
How do I deploy IronSecureDoc to a cloud platform?
IronSecureDoc can be deployed to cloud platforms such as Azure and AWS. Instructions on deploying IronSecureDoc to these environments are available on the Iron Software website.
What is the role of the Docker Image in using IronSecureDoc?
The IronSecureDoc Docker Image is required for running the API service. You can pull and start the Docker Image to host IronSecureDoc locally or in cloud environments.
Can I use IronSecureDoc to create PDF/A or PDF/UA compliant files?
Yes, IronSecureDoc offers optional parameters for saving PDF files as PDF/A-3 or PDF/UA compliant, ensuring adherence to specific standards and accessibility.
Why is it important to use owner and user passwords in PDF encryption?
Owner and user passwords in PDF encryption are crucial for controlling access levels. Owner passwords grant full access to the document, while user passwords provide more limited access, protecting sensitive information.
What coding languages can integrate with the IronSecureDoc API?
The IronSecureDoc API can be integrated with multiple programming languages such as Python, C#, and Java, among others, allowing flexibility for developers working in diverse environments.
What are the required parameters for encrypting a PDF with IronSecureDoc?
The required parameters include the PDF file itself and several permissions settings such as allowing content extraction, form filling, annotations, printing, modifications, and setting the new owner password.
What advantage does using IronSecureDoc offer for PDF security?
IronSecureDoc provides a robust solution for PDF security by allowing comprehensive encryption and permissions management, making it suitable for protecting legal, financial, or personal documents from unauthorized access.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.