C# でPDFファイルを暗号化する

C# を使用して、AES-128 暗号化による PDF 形式のドキュメント権限を設定します

.NET ライブラリを使用してPDFファイルを保護する方法

PDFファイルを保護するために、net プラットフォーム用の機能豊富で強力で使いやすいドキュメント操作 API である Aspose.PDF for .NET API を使用します。NuGet パッケージマネージャーを開き、aspose.pdf を検索してインストールします。パッケージマネージャーコンソールから次のコマンドを使用することもできます。

Package Manager Console

PM > Install-Package Aspose.PDF

C# でPDFを保護する


お使いの環境でコードを試すには Aspose.PDF for .NET が必要です。

1.PDF を Document のインスタンスとともに読み込みます。 1.documentPrivilegeのオブジェクトを作成し、オプションを設定します。 1.AES-128 で PDF を保護するには、Document.Encrypt メソッドを呼び出します 1.結果をPDF形式で保存

PDFを保護-C#。

<% protect.code-block.subtitle %>


    using (var document = new Aspose.Pdf.Document("template.pdf"))
{
    // instantiate DocumentPrivileges object
    // apply restrictions on all privileges
    var documentPrivilege = Aspose.Pdf.Facades.DocumentPrivilege.ForbidAll;
    // allow screen reading
    documentPrivilege.AllowScreenReaders = true;
    // encrypt the file with User and Owner password.
    document.Encrypt("userpassword", "ownerpassword", documentPrivilege, Aspose.Pdf.CryptoAlgorithm.AESx128, false);
    // save updated document
    document.Save("protected.pdf");
}