Change Password of PDF File in C++

Use PdfFileSecurity for this task. The examples below retain the original C++ code and API Reference steps.

Change password and reset security

Change both passwords using the existing owner password and reset privileges to printing only with x128 key size.

  1. Configure the permission flags with DocumentPrivilege.
  2. Create PdfFileSecurity.
  3. Bind the PDF input with Facade::BindPdf().
  4. Change passwords and security settings with PdfFileSecurity::ChangePassword().
  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 ChangePasswordAndResetSecurity(String inputFile, String outputFile)
{
    auto fileSecurity = MakeObject<PdfFileSecurity>();
    fileSecurity->BindPdf(inputFile);
    auto privilege = DocumentPrivilege::get_ForbidAll();
    privilege->set_AllowPrint(true);
    fileSecurity->ChangePassword(u"owner_password", u"new_user_password", u"new_owner_password", privilege,
                                 KeySize::x128);
    fileSecurity->Save(outputFile);
    fileSecurity->Close();
}
} // namespace Facades::Examples::PDF_FileSecurity

Change user and owner password

Change the user and owner passwords using the existing owner password.

  1. Create PdfFileSecurity.
  2. Bind the PDF input with Facade::BindPdf().
  3. Change passwords and security settings with PdfFileSecurity::ChangePassword().
  4. Save the resulting PDF with PdfFileSecurity::Save().
  5. Close the facade with PdfFileSecurity::Close().
#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 ChangeUserAndOwnerPassword(String inputFile, String outputFile)
{
    auto fileSecurity = MakeObject<PdfFileSecurity>();
    fileSecurity->BindPdf(inputFile);
    fileSecurity->ChangePassword(u"owner_password", u"new_user_password", u"new_owner_password");
    fileSecurity->Save(outputFile);
    fileSecurity->Close();
}
} // namespace Facades::Examples::PDF_FileSecurity

Try change password without exception

Check TryChangePassword and save only when it returns true; otherwise print a diagnostic.

  1. Create PdfFileSecurity.
  2. Bind the PDF input with Facade::BindPdf().
  3. Attempt the password change and check the result of PdfFileSecurity::TryChangePassword().
  4. Save the PDF with PdfFileSecurity::Save() only after the operation succeeds.
  5. Close the facade with PdfFileSecurity::Close().
  6. Print the results or diagnostic messages shown in the example.
#include <Aspose.PDF.Cpp/Facades/PdfFileSecurity.h>
#include <iostream>
#include <system/string.h>

namespace Facades::Examples::PDF_FileSecurity
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;

void TryChangePasswordWithoutException(String inputFile, String outputFile)
{
    auto fileSecurity = MakeObject<PdfFileSecurity>();
    fileSecurity->BindPdf(inputFile);
    if (fileSecurity->TryChangePassword(u"owner_password", u"new_user_password", u"new_owner_password"))
    {
        fileSecurity->Save(outputFile);
    }
    else
    {
        std::cout << "Password change failed. Check owner password or document security." << std::endl;
    }
    fileSecurity->Close();
}
} // namespace Facades::Examples::PDF_FileSecurity