PDFにパスワードと権限を設定する方法

This article was translated from English: Does it need improvement?
Translated
View the article in English

パスワード保護は、無許可のアクセスを制限するためにドキュメントを暗号化することを含みます。 それには通常、2種類のパスワードが含まれます: ユーザーパスワード (またはパスワードを開く)ドキュメントを開くために必要なユーザーパスワードおよびオーナーパスワード (または権限のパスワード)編集、印刷、およびその他のアクションの権限を制御します。

IronPDFは、既存および新規のPDFファイルに必要なパスワードと権限のすべてをサポートします。 詳細なメタデータおよびセキュリティ設定を適用できます。具体的には、PDF文書を印刷不可、読み取り専用、暗号化することができ、128ビット暗号化、復号、およびパスワード保護がすべてサポートされています。


PDF 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronPdf
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

PDF 用 C# NuGet ライブラリ

でインストール NuGet

Install-Package IronPdf
または
Java PDF JAR(ジャバPDF JAR)

ダウンロード DLL (ディーエルエル)

DLLをダウンロード

プロジェクトに手動でインストールする

今日からプロジェクトでIronPDFを使い始めましょう。無料のトライアルをお試しください。

最初のステップ:
green arrow pointer

チェックアウト IronPDF オン Nuget 迅速なインストールと展開のために。8百万以上のダウンロード数により、PDFをC#で変革しています。

PDF 用 C# NuGet ライブラリ nuget.org/packages/IronPdf/
Install-Package IronPdf

インストールを検討してください IronPDF DLL 直接。ダウンロードして、プロジェクトまたはGACの形式で手動でインストールしてください。 IronPdf.zip

プロジェクトに手動でインストールする

DLLをダウンロード

PDFにパスワードを設定する

ここでは an 例としてのPDF IronPDFを使用して保護したい内容。 以下のコードを実行して、PDFにパスワードを追加しましょう。 この例では、パスワード password123 を使用します。

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-add-password.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();

PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Secret Information:</h1> Hello World");

// Password to edit the pdf
pdf.SecuritySettings.OwnerPassword = "123password";

// Password to open the pdf
pdf.SecuritySettings.UserPassword = "password123";

pdf.SaveAs("protected.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()

Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>Secret Information:</h1> Hello World")

' Password to edit the pdf
pdf.SecuritySettings.OwnerPassword = "123password"

' Password to open the pdf
pdf.SecuritySettings.UserPassword = "password123"

pdf.SaveAs("protected.pdf")
VB   C#

結果として得られるPDFは次の通りです。password123 というパスワードを入力して閲覧できます。

パスワードで保護されたPDFを開く

これはパスワードが設定されたPDFを開く方法です。 PdfDocument.FromFileメソッドには、2つ目のオプション引数としてパスワードがあります。 このパラメータに正しいパスワードを入力してPDFを開いてください。

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-open-password.cs
using IronPdf;

var pdf = PdfDocument.FromFile("protected.pdf", "password123");

//... perform PDF-tasks

pdf.SaveAs("protected_2.pdf"); // Saved as another file
Imports IronPdf

Private pdf = PdfDocument.FromFile("protected.pdf", "password123")

'... perform PDF-tasks

pdf.SaveAs("protected_2.pdf") ' Saved as another file
VB   C#

高度なセキュリティと権限設定

PdfDocument オブジェクトには、AuthorModifiedDate など、設定可能な MetaData フィールドもあります。 以下のように、ユーザー注釈、ユーザー印刷、およびその他多くの機能を無効にすることもできます:

:path=/static-assets/pdf/content-code-examples/how-to/pdf-permissions-passwords-advanced.cs
using IronPdf;

// Open an Encrypted File, alternatively create a new PDF from HTML
var pdf = PdfDocument.FromFile("protected.pdf", "password123");

// Edit file security settings
// The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption();
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key");
pdf.SecuritySettings.AllowUserAnnotations = false;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
pdf.SecuritySettings.AllowUserFormData = false;
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;

// Save the secure PDF
pdf.SaveAs("secured.pdf");
Imports IronPdf

' Open an Encrypted File, alternatively create a new PDF from HTML
Private pdf = PdfDocument.FromFile("protected.pdf", "password123")

' Edit file security settings
' The following code makes a PDF read only and will disallow copy & paste and printing
pdf.SecuritySettings.RemovePasswordsAndEncryption()
pdf.SecuritySettings.MakePdfDocumentReadOnly("secret-key")
pdf.SecuritySettings.AllowUserAnnotations = False
pdf.SecuritySettings.AllowUserCopyPasteContent = False
pdf.SecuritySettings.AllowUserFormData = False
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights

' Save the secure PDF
pdf.SaveAs("secured.pdf")
VB   C#

権限設定はドキュメントパスワードに関連しており、以下のように動作します。 例えば、AllowUserCopyPasteContent プロパティを false に設定することは、コンテンツのコピー/貼り付けを防ぐことを意図しています:

  • パスワードが設定されていません: パスワードがない場合、コンテンツのコピー/ペーストは引き続きブロックされます。
  • ユーザーのパスワード設定: ユーザーのパスワードが設定されている場合、正しいパスワードを入力することでコンテンツのコピー/ペーストが可能になります。

  • オーナーパスワードの設定: オーナーパスワードが設定されている場合、ユーザーパスワードを入力するだけではコピー/ペースト機能を解除できません。 ただし、所有者パスワードを正しく入力すると、コンテンツのコピー/貼り付けが可能になります。
    権限ウィンドウ

    関連する記事では、事前定義されたメタデータとカスタムメタデータについて詳しく議論しています。 このリンクをフォローして詳細をご覧ください:PDFメタデータを設定および編集する方法."