PDFの暗号化と復号化
このコード例では、IronPDFの強力なC# .NET PDFライブラリを使用して、メタデータの変更、PDFの読み取り専用化、権限の設定、ドキュメントの暗号化パスワードの変更方法を示します。
はじめに、既存のPDF文書をopenメソッドを使ってインポートします。 このメソッドは、パスワードを 2 番目のパラメータとして指定することでパスワードで保護されたドキュメントを開くために使用でき、保護されたファイルを強力に処理できます。
新しいメタデータ情報を設定するには、まず辞書を作成し、作成者やキーワードなどのメタデータのキーと値のペアを追加します。 IronPDF のoverrideMetadataメソッドを利用して、新しいメタデータを PDF ドキュメントに効果的に適用します。
次に、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




