Crittografa il PDF tramite C++

Blocca i file PDF con la libreria Aspose.PDF usando C++.

Crittografa il documento PDF utilizzando la libreria C++

Per crittografare il file PDF, useremo Aspose.PDF for C++ API che è un’API di manipolazione dei documenti ricca di funzionalità, potente e facile da usare per la piattaforma cpp. Apri il gestore di pacchetti NuGet, cerca Aspose.pdf e installa. È inoltre possibile utilizzare il seguente comando dalla console di Package Manager.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

Come crittografare il PDF tramite C++


È necessario Aspose.PDF for C++ per provare il codice nel proprio ambiente.

  1. Aprire un documento PDF utilizzando l’oggetto Document.
  2. Crea una password per utente e proprietario.
  3. Chiama il metodo Document.Encrypt.
  4. Salva il file PDF.

<% encrypt.code-block.text %>

Crittografa documento PDF - C++

<% encrypt.code-block.subtitle %>

Input file:

File not added

Password:

Output format:

Output file:

    // String for path name.
    String _dataDir("C:\\Samples\\");

    // Load a source PDF file
    auto document = MakeObject<Document>(_dataDir + u"input.pdf");

    // Instantiate Document Privileges object
    // Apply restrictions on all privileges
    auto documentPrivilege = DocumentPrivilege::get_ForbidAll();
    // Only allow screen reading
    documentPrivilege->set_AllowScreenReaders(true);
    // Encrypt the file with User and Owner password.
    // Need to set the password, so that once the user views the file with user
    // password,
    // Only screen reading option is enabled
    document->Encrypt(u"user", u"owner", documentPrivilege, CryptoAlgorithm::AESx128, false);
    // Save updated document
    document->Save(_dataDir + u"SetPrivileges_out.pdf");