Set Privileges on an Existing PDF File in C++
Contents
[
Hide
]
Use PdfFileSecurity for this task. The examples below retain the original C++ code and API Reference steps.
Set PDF privileges with passwords
Set printing allowed and copying disabled using the supplied sample passwords.
- Configure the permission flags with DocumentPrivilege.
- Create PdfFileSecurity.
- Bind the PDF input with Facade::BindPdf().
- Apply document privileges with PdfFileSecurity::SetPrivilege().
- 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/PdfFileSecurity.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileSecurity
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void SetPdfPrivilegesWithPasswords(String inputFile, String outputFile)
{
auto fileSecurity = MakeObject<PdfFileSecurity>();
fileSecurity->BindPdf(inputFile);
auto privilege = DocumentPrivilege::get_ForbidAll();
privilege->set_AllowPrint(true);
privilege->set_AllowCopy(false);
fileSecurity->SetPrivilege(u"user_password", u"owner_password", privilege);
fileSecurity->Save(outputFile);
fileSecurity->Close();
}
} // namespace Facades::Examples::PDF_FileSecurity
Set PDF privileges without passwords
Set a printing-only privilege set using the overload without password arguments.
- Configure the permission flags with DocumentPrivilege.
- Create PdfFileSecurity.
- Bind the PDF input with Facade::BindPdf().
- Apply document privileges with PdfFileSecurity::SetPrivilege().
- 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/PdfFileSecurity.h>
#include <system/string.h>
namespace Facades::Examples::PDF_FileSecurity
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Facades;
void SetPdfPrivilegesWithoutPasswords(String inputFile, String outputFile)
{
auto fileSecurity = MakeObject<PdfFileSecurity>();
fileSecurity->BindPdf(inputFile);
auto privilege = DocumentPrivilege::get_ForbidAll();
privilege->set_AllowPrint(true);
fileSecurity->SetPrivilege(privilege);
fileSecurity->Save(outputFile);
fileSecurity->Close();
}
} // namespace Facades::Examples::PDF_FileSecurity
Try set PDF privileges without exception
Check TrySetPrivilege and save only when it returns true; otherwise print a diagnostic.
- Configure the permission flags with DocumentPrivilege.
- Create PdfFileSecurity.
- Bind the PDF input with Facade::BindPdf().
- Attempt the privilege update and check the result of PdfFileSecurity::TrySetPrivilege().
- 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/DocumentPrivilege.h>
#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 TrySetPdfPrivilegesWithoutException(String inputFile, String outputFile)
{
auto fileSecurity = MakeObject<PdfFileSecurity>();
fileSecurity->BindPdf(inputFile);
auto privilege = DocumentPrivilege::get_ForbidAll();
privilege->set_AllowPrint(true);
if (fileSecurity->TrySetPrivilege(u"user_password", u"owner_password", privilege))
{
fileSecurity->Save(outputFile);
}
else
{
std::cout << "Setting privileges failed. Check passwords or document state." << std::endl;
}
fileSecurity->Close();
}
} // namespace Facades::Examples::PDF_FileSecurity