Encrypt PDF File in C++

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.

  1. Configure the permission flags with DocumentPrivilege.
  2. Create PdfFileSecurity.
  3. Bind the PDF input with Facade::BindPdf().
  4. Encrypt the bound PDF with PdfFileSecurity::MfEncryptFile().
  5. Save the resulting PDF with PdfFileSecurity::Save().
  6. 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.

  1. Configure the permission flags with DocumentPrivilege.
  2. Create PdfFileSecurity.
  3. Bind the PDF input with Facade::BindPdf().
  4. Encrypt the bound PDF with PdfFileSecurity::MfEncryptFile().
  5. Save the resulting PDF with PdfFileSecurity::Save().
  6. 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.

  1. Configure the permission flags with DocumentPrivilege.
  2. Create PdfFileSecurity.
  3. Bind the PDF input with Facade::BindPdf().
  4. Encrypt the bound PDF with PdfFileSecurity::MfEncryptFile().
  5. Save the resulting PDF with PdfFileSecurity::Save().
  6. 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