通过 C++ 加密 PDF

使用 C++ 使用 Aspose.PDF 库锁定 PDF 文件。

使用 C++ 库加密 PDF 文档

为了加密 PDF 文件,我们将使用 Aspose.PDF for C++ API,这是一款功能丰富、功能强大且易于使用的适用于 cpp 平台的文档处理 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以从软件包管理器控制台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

如何通过 C++ 加密 PDF


你需要 Aspose.PDF for C++ 才能在你的环境中试用代码。

1.使用文档对象打开 PDF 文档。 1.为用户和所有者创建密码。 1.拨打文档.加密方法。 1.保存 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");