Add Password Protection to PDF using C# REST API

This tutorial guides on how to add password protection to PDF using C# REST API. You will learn to password protect PDF using C# Low Code API with the help of .NET-based Cloud SDK. All the necessary details will be shared to perform the task, including setting the password and encryption type.

Prerequisite

Steps to Password Protect PDF Doc using C# REST Interface

  1. Import the required namespaces for Aspose PDF Cloud, file handling, and text encoding
  2. Create a new PdfApi instance using your API key and App ID to authorize access to Aspose’s cloud services
  3. Open your target PDF file from disk so you can read and process it
  4. Convert your user and owner passwords into Base64 strings for secure transmission
  5. Create a list of permissions—like printing, extracting, and modifying content—that the encrypted PDF will allow
  6. Call PutEncryptDocument to apply encryption to your file, specifying the output file name, encryption type, and the permissions list
  7. After encryption, download the newly encrypted file from Aspose Cloud
  8. Write the downloaded, encrypted content into a new file on your computer

These steps have summarized the process to protect PDF document using C# .NET-based API. Import the necessary classes, create an instance of the PdfApi, and open the PDF file to prepare it for encryption. Set up user and owner passwords, define the allowed permissions, and call PutEncryptDocument to apply protection. Finally, download the encrypted PDF, save it locally with a new name, and print the response status to confirm success.

Code to Password Protect PDF Document using C# RESTful Service

This code demonstrates the process to add password to PDF using C# Low Code API. Note that the password must be converted to a base 64 string; otherwise, you will get an exception. You can add more permissions, such as ModifyTextAnnotations, FillForm, ExtractContentWithDisabilities, AssembleDocument, and PrintingQuality.

This article has guided us in making a PDF password protected using C# .NET-based API. To verify a signature in PDF, refer to the article Verify signature in PDF using C# REST API.