Change Password of PDF File in C++
Contents
[
Hide
]
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.
- Configure the permission flags with DocumentPrivilege.
- Create PdfFileSecurity.
- Bind the PDF input with Facade::BindPdf().
- Change passwords and security settings with PdfFileSecurity::ChangePassword().
- 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 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.
- Create PdfFileSecurity.
- Bind the PDF input with Facade::BindPdf().
- Change passwords and security settings with PdfFileSecurity::ChangePassword().
- Save the resulting PDF with PdfFileSecurity::
Save(). - 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.
- Create PdfFileSecurity.
- Bind the PDF input with Facade::BindPdf().
- Attempt the password change and check the result of PdfFileSecurity::TryChangePassword().
- Save the PDF with PdfFileSecurity::
Save()only after the operation succeeds. - Close the facade with PdfFileSecurity::
Close(). - 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