0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#でPDFをパスワードで保護する

Posted at

機密データを含むPDFファイルを扱うときは、許可された人だけがPDFを開く、表示、編集、または印刷できることを制限するために保護パスワードを設定することができます。

この記事では、無料の.NET PDFライブラリを使用してC#でパスワードでPDFファイルを暗号化する方法を共有します。

PDFを暗号化するための無料の.NETライブラリ

使用するPDFライブラリはFree Spire.PDF for .NETです。始める前に、手動でダウンロードしてインストールするか、Nuget経由で直接インストールする必要があります。
ダウンロードリンク:

C#でPDFファイルをパスワードで保護する方法

Free Spire.PDF for .NETでは、PDF ファイルに以下の 2 種類のパスワードを設定できます。

  • ドキュメントを開くパスワード:PDFファイルを開くためのパスワード。
  • 許可パスワード:印刷、コンテンツのコピー、コメントなどを制限するためのパスワード。

PDFファイルをパスワードで暗号化するには、以下の手順を参照してください:

1. 必要なネームスペースをインポートする;

using Spire.Pdf;
using Spire.Pdf.Security;

2. PdfDocumentクラスのインスタンスを作成し、PDFファイルを読み込みます;

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("input.pdf");

3. オープンパスワードとパーミッションパスワードでPDFを暗号化し、パーミッションと暗号化キーサイズを設定します;
(FreeSpire.PDF for .NETは、暗号化キーサイズ:40bit、128bit、256bitをサポートしています)

pdf.Security.Encrypt("123", "abc", PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit);

4. 結果のPDFファイルを保存する。

pdf.SaveToFile("Encrypt.pdf", FileFormat.PDF);

EncryptPDF.png

フルC#コード:

using Spire.Pdf;
using Spire.Pdf.Security;

namespace EncryptPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            // PDFサンプルファイルの読み込み
            PdfDocument pdf = new PdfDocument();
            pdf.LoadFromFile("sample.pdf");

            //パスワードでPDFファイルを暗号化する
            pdf.Security.Encrypt("123", "abc", PdfPermissionsFlags.Print | PdfPermissionsFlags.CopyContent, PdfEncryptionKeySize.Key128Bit);

            //結果ファイルの保存
            pdf.SaveToFile("ProtectPDF.pdf", FileFormat.PDF);
        }
    }
}

もっと見る

パスワードでPDF文書を暗号化した後、パスワードを削除するには、無料の.NET PDF APIを使用することもできます。さらに、PDFへのデジタル署名や他のPDF処理&変換機能もサポートしています。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?