PDF encryption is the process of protecting the document using a password as well as some cryptographic algorithms including AES or RC4. You can also set different privileges to limit user’s access to different operations. For example, you can only allow printing, adding annotation, filling forms, and etc. In this article, you will learn how to encrypt PDF files using C++. Furthermore, this article also covers how to set different privileges and decrypt password-protected PDF files using C++.
C++ API to Encrypt or Decrypt PDF Files
Aspose.PDF for C++ is a native C++ API that lets you create, read, and manipulate PDF documents. In addition, it also allows you to encrypt PDFs with passwords and decrypt the encrypted PDFs within a few lines of code. You can download the API from the downloads section or get it installed using NuGet.
Encrypt PDF Files using C++
The following are the steps to encrypt a PDF file using Aspose.PDF for C++.
- Load the PDF document using Document class.
- Use DocumentPrivilege class to set different privileges such as allow printing, allow modifying content and etc.
- Call Document.Encrypt(String, String, SharedPtr<DocumentPrivilege>, CryptoAlgorithm, bool) method to encrypt the PDF.
- Save the PDF using the Document->Save(String) method.
The following code sample shows how to encrypt a PDF file using C++.
Decrypt PDF Files using C++
The following are the steps to decrypt a PDF file using Aspose.PDF for C++.
- Load the PDF file using the Document class and provide the document’s path and password.
- Decrypt the file using Document->Decrypt() method.
- Save the decrypted PDF using the Document->Save(String) method.
The following code sample shows how to decrypt a PDF using C++.
In this article, you have learned how to encrypt or decrypt PDF files using C++. The code sample has also shown how to set different privileges when encrypting a PDF file. You can explore more about the C++ PDF API using documentation.