Set Privileges on an Existing PDF File in C++

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.

  1. Configure the permission flags with DocumentPrivilege.
  2. Create PdfFileSecurity.
  3. Bind the PDF input with Facade::BindPdf().
  4. Apply document privileges with PdfFileSecurity::SetPrivilege().
  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/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.

  1. Configure the permission flags with DocumentPrivilege.
  2. Create PdfFileSecurity.
  3. Bind the PDF input with Facade::BindPdf().
  4. Apply document privileges with PdfFileSecurity::SetPrivilege().
  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/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.

  1. Configure the permission flags with DocumentPrivilege.
  2. Create PdfFileSecurity.
  3. Bind the PDF input with Facade::BindPdf().
  4. Attempt the privilege update and check the result of PdfFileSecurity::TrySetPrivilege().
  5. Save the PDF with PdfFileSecurity::Save() only after the operation succeeds.
  6. Close the facade with PdfFileSecurity::Close().
  7. 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