PDFの暗号化と復号化

このコード例では、IronPDFの強力なC# .NET PDFライブラリを使用して、メタデータの変更、PDFの読み取り専用化、権限の設定、ドキュメントの暗号化パスワードの変更方法を示します。

はじめに、openメソッドを使って既存のPDF文書をインポートします。 このメソッドは、2番目のパラメータとしてパスワードを指定することで、パスワードで保護されたドキュメントを開くために利用することができ、保護されたファイルの堅牢な処理を提供します。

新しいメタデータ情報を設定するには、まず辞書を作成し、作者やキーワードなどのメタデータのキーと値のペアを追加します。 新しいメタデータをPDFドキュメントに効果的に適用するために、IronPDFのoverrideMetadataメソッドを利用してください。

次に、IronPDFが提供するremovePasswordsAndEncryptionメソッドを使ってパスワードと暗号化を削除し、makePdfDocumentReadOnlyメソッドで新しいパスワードを設定してPDFを読み取り専用に設定し、ドキュメントの完全性とセキュリティを確保します。

PDF 文書の権限は、 「permissions」 と い う 名前のオブジ ェ ク ト を使っ て設定 さ れます。 こ のオブジ ェ ク ト は、 注釈、 内容抽出、 フ ォーム入力、 印刷な ど の特定のア ク シ ョ ンが許可 さ れ る か拒否 さ れ る か を指定 し ます。 ドキュメントのアクセシビリティ機能を正確に制御するために、setPermissionメソッドにパーミッションオブジェクトを渡します。

最後に、ドキュメントの暗号化パスワードを "my-password "に変更または設定し、変更したPDFを "secured.pdf "として保存し、アプリケーション開発における安全なドキュメント管理のためのIronPDFの機能を紹介します。

// Import the necessary namespace.
using IronPdf;

class PdfExample
{
    static void Main()
    {
        // Open an existing PDF document. If the document is password protected, provide the password.
        var pdf = IronPdf.PdfDocument.FromFile("existing.pdf", "document-password");

        // Initialize a dictionary to store metadata key-value pairs.
        var newMetadata = new Dictionary<string, string>
        {
            { "Author", "New Author" },
            { "Keywords", "PDF, Documentation, Example" }
        };

        // Apply the new metadata to the PDF document.
        pdf.OverrideMetadata(newMetadata);

        // Remove any existing passwords and encryption.
        pdf.RemovePasswordsAndEncryption();

        // Make the PDF document read-only by setting a read-only password.
        pdf.MakePdfDocumentReadOnly("read-only-password");

        // Define and set the permissions for the PDF document.
        var permissions = new PdfPermissions
        {
            AllowAnnotations = false,
            AllowContentExtraction = false,
            AllowFormFilling = true,
            AllowPrinting = false,
            AllowDocumentAssembly = false
        };
        pdf.SetPermissions(permissions);

        // Change the document encryption password.
        pdf.SaveAs("secured.pdf", "my-password"); // Save the modified PDF with a new security password.
    }
}
// Import the necessary namespace.
using IronPdf;

class PdfExample
{
    static void Main()
    {
        // Open an existing PDF document. If the document is password protected, provide the password.
        var pdf = IronPdf.PdfDocument.FromFile("existing.pdf", "document-password");

        // Initialize a dictionary to store metadata key-value pairs.
        var newMetadata = new Dictionary<string, string>
        {
            { "Author", "New Author" },
            { "Keywords", "PDF, Documentation, Example" }
        };

        // Apply the new metadata to the PDF document.
        pdf.OverrideMetadata(newMetadata);

        // Remove any existing passwords and encryption.
        pdf.RemovePasswordsAndEncryption();

        // Make the PDF document read-only by setting a read-only password.
        pdf.MakePdfDocumentReadOnly("read-only-password");

        // Define and set the permissions for the PDF document.
        var permissions = new PdfPermissions
        {
            AllowAnnotations = false,
            AllowContentExtraction = false,
            AllowFormFilling = true,
            AllowPrinting = false,
            AllowDocumentAssembly = false
        };
        pdf.SetPermissions(permissions);

        // Change the document encryption password.
        pdf.SaveAs("secured.pdf", "my-password"); // Save the modified PDF with a new security password.
    }
}
' Import the necessary namespace.
Imports IronPdf

Friend Class PdfExample
	Shared Sub Main()
		' Open an existing PDF document. If the document is password protected, provide the password.
		Dim pdf = IronPdf.PdfDocument.FromFile("existing.pdf", "document-password")

		' Initialize a dictionary to store metadata key-value pairs.
		Dim newMetadata = New Dictionary(Of String, String) From {
			{"Author", "New Author"},
			{"Keywords", "PDF, Documentation, Example"}
		}

		' Apply the new metadata to the PDF document.
		pdf.OverrideMetadata(newMetadata)

		' Remove any existing passwords and encryption.
		pdf.RemovePasswordsAndEncryption()

		' Make the PDF document read-only by setting a read-only password.
		pdf.MakePdfDocumentReadOnly("read-only-password")

		' Define and set the permissions for the PDF document.
		Dim permissions = New PdfPermissions With {
			.AllowAnnotations = False,
			.AllowContentExtraction = False,
			.AllowFormFilling = True,
			.AllowPrinting = False,
			.AllowDocumentAssembly = False
		}
		pdf.SetPermissions(permissions)

		' Change the document encryption password.
		pdf.SaveAs("secured.pdf", "my-password") ' Save the modified PDF with a new security password.
	End Sub
End Class
$vbLabelText   $csharpLabel

PDFの暗号化と復号化のコード例を探る

準備はいいですか?
バージョン: 2025.11 ただ今リリースされました