通過C++加密 PDF

使用 C++鎖定帶有.PDF庫的“檔”。

使用 C++ 庫加密 PDF 文件

為了加密PDF檔,我們將使用[Aspose.PDF for C++](https://products.aspose.com/pdf/cpp)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 cpp 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

如何通過C++加密 PDF


您需要為C++.PDF [Aspose https://releases.aspose.com/pdf/cpp) 才能在您的環境中嘗試代碼。

  1. 使用文件對象打開 PDF 文件。
  2. 為使用者和擁有者創建密碼。
  3. 呼叫「文件加密」 方法。
  4. 保存 PDF 檔。

<% encrypt.code-block.text %>

加密 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");