encrypt decrypt pdf C++

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++.

The following code sample shows how to encrypt a PDF file using C++.

For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-C
// Load an existing PDF document
auto doc = MakeObject<Document>(L"..\\Data\\SecurityAndSignatures\\input.pdf");
// Way1: Using predefined privilege directly.
System::SharedPtr<Aspose::Pdf::Facades::DocumentPrivilege> privilege = Aspose::Pdf::Facades::DocumentPrivilege::get_Print();
doc->Encrypt(L"user", L"owner", privilege, CryptoAlgorithm::AESx128, false);
doc->Save(L"..\\Data\\SecurityAndSignatures\\SetPrivelegesWay1_out.pdf");
// Way2: Based on a predefined privilege and change some specifical permissions.
System::SharedPtr<Aspose::Pdf::Facades::DocumentPrivilege> privilege2 = Aspose::Pdf::Facades::DocumentPrivilege::get_AllowAll();
privilege->set_AllowPrint(false);
privilege->set_AllowModifyContents(false);
doc->Encrypt(L"user", L"owner", privilege2, CryptoAlgorithm::AESx128, false);
doc->Save(L"..\\Data\\SecurityAndSignatures\\SetPrivelegesWay2_out.pdf");
// Way3: Based on a predefined privilege and change some specifical Adobe Professional permissions combination.
System::SharedPtr<Aspose::Pdf::Facades::DocumentPrivilege> privilege3 = Aspose::Pdf::Facades::DocumentPrivilege::get_ForbidAll();
privilege->set_ChangeAllowLevel(1);
privilege->set_PrintAllowLevel(2);
doc->Encrypt(L"user", L"owner", privilege3, CryptoAlgorithm::AESx128, false);
doc->Save(L"..\\Data\\SecurityAndSignatures\\SetPrivelegesWay3_out.pdf");
// Way4: Mixes the way2 and way3
System::SharedPtr<Aspose::Pdf::Facades::DocumentPrivilege> privilege4 = Aspose::Pdf::Facades::DocumentPrivilege::get_ForbidAll();
privilege->set_ChangeAllowLevel(1);
privilege->set_AllowPrint(true);
doc->Encrypt(L"user", L"owner", privilege4, CryptoAlgorithm::AESx128, false);
doc->Save(L"..\\Data\\SecurityAndSignatures\\SetPrivelegesWay4_out.pdf");

Decrypt PDF Files using C++

The following are the steps to decrypt a PDF file using Aspose.PDF for C++.

The following code sample shows how to decrypt a PDF using C++.

For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-C
// Load an existing PDF document
auto doc = MakeObject<Document>(L"..\\Data\\SecurityAndSignatures\\Decrypt.pdf", L"password");
// Decrypt PDF
doc->Decrypt();
// Save the updated document
doc->Save(L"..\\Data\\SecurityAndSignatures\\Decrypt_out.pdf");

Conclusion

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.

See Also