Encrypt PDF File in C++
Contents
[
Hide
]
Use PdfFileSecurity for this task. The examples below retain the original C++ code and API Reference steps.
Encrypt PDF with encryption algorithm
Encrypt with AES, x256 key size, and printing allowed.
- Configure the permission flags with DocumentPrivilege.
- Create PdfFileSecurity.
- Bind the PDF input with Facade::BindPdf().
- Encrypt the bound PDF with PdfFileSecurity::MfEncryptFile().
- Save the resulting PDF with PdfFileSecurity::
Save(). - Close the facade with PdfFileSecurity::
Close().
#include <Aspose.PDF.Cpp/Facades/Algorithm.h>
#include <Aspose.PDF.Cpp/Facades/DocumentPrivilege.h>
#include <Aspose.PDF.Cpp/Facades/KeySize.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileSecurity.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileSecurity
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void EncryptPdfWithEncryptionAlgorithm(String inputFile, String outputFile)
{
auto fileSecurity = MakeObject<PdfFileSecurity>();
fileSecurity->BindPdf(inputFile);
auto privilege = DocumentPrivilege::get_ForbidAll();
privilege->set_AllowPrint(true);
fileSecurity->MfEncryptFile(u"user_password", u"owner_password", privilege, KeySize::x256, Algorithm::AES);
fileSecurity->Save(outputFile);
fileSecurity->Close();
}
} // namespace Facades::Examples::PDF_FileSecurity
Encrypt PDF with permissions
Encrypt with x128 key size while disabling printing and copying from an initially AllowAll privilege set.
- Configure the permission flags with DocumentPrivilege.
- Create PdfFileSecurity.
- Bind the PDF input with Facade::BindPdf().
- Encrypt the bound PDF with PdfFileSecurity::MfEncryptFile().
- Save the resulting PDF with PdfFileSecurity::
Save(). - Close the facade with PdfFileSecurity::
Close().
#include <Aspose.PDF.Cpp/Facades/DocumentPrivilege.h>
#include <Aspose.PDF.Cpp/Facades/KeySize.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileSecurity.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileSecurity
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void EncryptPdfWithPermissions(String inputFile, String outputFile)
{
auto fileSecurity = MakeObject<PdfFileSecurity>();
fileSecurity->BindPdf(inputFile);
auto privilege = DocumentPrivilege::get_AllowAll();
privilege->set_AllowPrint(false);
privilege->set_AllowCopy(false);
fileSecurity->MfEncryptFile(u"user_password", u"owner_password", privilege, KeySize::x128);
fileSecurity->Save(outputFile);
fileSecurity->Close();
}
} // namespace Facades::Examples::PDF_FileSecurity
Encrypt PDF with user owner password
Encrypt with sample user and owner passwords, x128 key size, and printing allowed.
- Configure the permission flags with DocumentPrivilege.
- Create PdfFileSecurity.
- Bind the PDF input with Facade::BindPdf().
- Encrypt the bound PDF with PdfFileSecurity::MfEncryptFile().
- Save the resulting PDF with PdfFileSecurity::
Save(). - Close the facade with PdfFileSecurity::
Close().
#include <Aspose.PDF.Cpp/Facades/DocumentPrivilege.h>
#include <Aspose.PDF.Cpp/Facades/KeySize.h>
#include <Aspose.PDF.Cpp/Facades/PdfFileSecurity.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileSecurity
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void EncryptPdfWithUserOwnerPassword(String inputFile, String outputFile)
{
auto fileSecurity = MakeObject<PdfFileSecurity>();
fileSecurity->BindPdf(inputFile);
auto privilege = DocumentPrivilege::get_ForbidAll();
privilege->set_AllowPrint(true);
fileSecurity->MfEncryptFile(u"user_password", u"owner_password", privilege, KeySize::x128);
fileSecurity->Save(outputFile);
fileSecurity->Close();
}
} // namespace Facades::Examples::PDF_FileSecurity