はじめに
PDFファイルを暗号化したいよ!!ドラえもん、なんかいい道具出してよ!!タダで!!Windowsで動くやつがいいな!!
VisualStudioCommunityEditionでC#でWindowsアプリつくれるよね。でもPDFファイルいじるにはどうするんだろう!??
そうだ!!最近はやりのChatGPTに聞いてみよう!!
ってなかんじで、ChatGPTに聞いてみてなんとかなるかな、と思い挑戦してみました。
※できあがったものをgithubにおいておきます
https://github.com/hase-k0x01/PdfEncrypter
ChatGPTに聞いてみた
ChatGPTとかに入力する質問文を考える専門の仕事があるらしいですね。プロンプトエンジニアっていうらしいです。私はプロンプトエンジニアではないので、至極てきとうに入力してみます。
Show source code to encrypt pdf in C# .
出てきた回答が以下の通り
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
class Program
{
static void Main(string[] args)
{
string inputFilePath = @"C:\input.pdf";
string outputFilePath = @"C:\output.pdf";
string password = "mypassword";
using (var input = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
using (var output = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
{
var reader = new PdfReader(input);
var encryption = new PdfEncryption();
encryption.UserPassword = encryption.OwnerPassword = password;
encryption.Permissions = PdfWriter.AllowPrinting | PdfWriter.AllowCopy | PdfWriter.AllowScreenReaders;
PdfStamper stamper = new PdfStamper(reader, output);
stamper.SetEncryption(encryption);
stamper.Close();
}
}
}
つづいて、説明文が
This code reads an existing PDF document from the inputFilePath, encrypts it with the specified password, and saves the encrypted document to the outputFilePath. The PdfEncryption object is used to set the password and permissions for the encrypted PDF. In this example, the permissions allow printing, copying, and screen reader access. You can adjust these permissions to your needs by setting the appropriate PdfWriter constants.
あー、なんかそれっぽいね!!いいね。
さっそく、VisualStudioでプロジェクト作って、コピペしてみよう!!
久しぶりにVisualStudioを使ってみる
そういえば、VisualStudioを使うのは、すごく久しぶりだったんですよね。
で、コマンドボタンのところにChatGPTのコードをコピペしたら、赤線がでて怒られたので、そのままだと動かないんだなぁって思って原因を探ります。
さすがに、コピペしただけじゃ動かなかった
調べてみると、"iTextSharp"っていうライブラリが必要のようでした。
インストール方法はGoogleに頼りました笑
こちらのサイト↓に、iTextSharpのライブラリのインストール方法が書いてありました。
https://va2577.github.io/post/111/
恥ずかしながら、VisualStudioの追加ライブラリのインストール方法を知らなかったんですけど、今はpythonでいうpipみたいにしてインストールできるんですねー。驚きです。
NuGet パッケージマネージャで以下のようにします。
PM> Install-Package iTextSharp
さて、パッケージをインストールできて、usingのところの赤線が消えました。めでたしめでたし。
あー、でも、赤線はきえないんですねー、何ででしょうか。
メソッドがないんですね。
さすがに、ChatGPT様そのままのコードじゃだめなんですね。
なんででしょう???
マニュアルが必要ですね。
ChatGPTに、「マニュアルだせよ!!」って頼んでみました。
Show reference manual of iTextSharp.
で、その回答...
回答内容は「iTextSharpライブラリは、2018年からサポートされなくなったよ!!PDFSharpライブラリを使ってね!!」ってな感じの内容です。
サポートされれないんかーーーい!!
だったらはじめからPDFSharpのコードを出しなさ~~~い!!
気を取り直してPDFSharpのコードを要求する
というわけで、気を取り直して、PDFSharpのコードを出すように要求してみました。
Show source code to encrypt pdf in C# with PDFSharp library.
ちゃんとPDFSharpライブラリを使っているっぽいですね。
コードはこんな感じでした。
using System;
using System.IO;
using PdfSharp.Pdf;
using PdfSharp.Pdf.Security;
class Program
{
static void Main(string[] args)
{
string inputFilePath = @"C:\input.pdf";
string outputFilePath = @"C:\output.pdf";
string password = "mypassword";
using (var input = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
using (var output = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
{
var document = PdfReader.Open(input);
var securitySettings = document.SecuritySettings;
securitySettings.UserPassword = password;
securitySettings.OwnerPassword = password;
securitySettings.PermitAccessibilityExtractContent = true;
securitySettings.PermitAnnotations = true;
securitySettings.PermitAssembleDocument = true;
securitySettings.PermitExtractContent = true;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = true;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = true;
document.Save(output);
}
}
}
あー、まあなんかそれっぽいですね。
PDFSharpライブラリを利用してみる
とりあえず、PDFSharpのライブラリをインストールします。
で、ChatGPT様が教えてくださったコードをコピペしました。
とくに赤線もでませんでした。
ChatGPTのコードに埋め込んであったファイル名の部分は、テキストボックスに入力したファイル名にしておきます。その他配置したコントロールと連携するようにしておきます。
toolStripStatusLabel.Text = "Starting Encrypting";
string inputFilePath = textBoxSourceFileName.Text;
string outputFilePath = textBoxDestinationFileName.Text;
string password = textBoxPassword.Text;
toolStripStatusLabel.Text = "Encrypting...";
using (var input = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read))
using (var output = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
{
var document = PdfReader.Open(input);
var securitySettings = document.SecuritySettings;
securitySettings.UserPassword = password;
securitySettings.OwnerPassword = password;
securitySettings.PermitAccessibilityExtractContent = true;
securitySettings.PermitAnnotations = true;
securitySettings.PermitAssembleDocument = true;
securitySettings.PermitExtractContent = true;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = true;
securitySettings.PermitModifyDocument = true;
securitySettings.PermitPrint = true;
document.Save(output);
}
toolStripStatusLabel.Text = "Done...";
動かしてみた
ビルドエラーなし、OK!!
とりあえず、ファイル名を設定して、後はボタンを押すだけ。
ボタンを押して...
例外も発生しないので、いけたのでは???
ファイルはできていました。
ちゃんと暗号化されてるっぽいですねー
ほら、ちゃんとPDF開けました。やったね!!!
ってことで、企みはまあ成功です
まとめ
ChatGPTで提示されたコードでアプリが作れるかという試みでしたが、まあなんとかなりました。
それっぽいコードを提案してくれるんですねー、すごいです。でも、役に立たないコードも出てきます。
なので、プログラミングの知識全くないとかだとエラーに対処できないので厳しいですね。
こういうアプリを作りたい、どうやるんだろう!!???のとっかかり位にしかならないですね。
そのほか
今回、ひさしぶりにVisualStudioを使ったんですが、コードの提案が素晴らしいんですよね。例えば、以下のコードなんですが、ボタンを押してファイル選択ダイアログを呼び出してその結果をテキストボックスにセットするって言う処理をしてるんですが、コードを書こうとおもって押したキー文字数は、ほんの数文字くらいです。あとはVisualStudioが勝手に提案してくれました。TABキー押すだけで入力できました。素晴らしいですね。
private void buttonSelectSourceFile_Click(object sender, EventArgs e)
{
if (openSourceFileDialog.ShowDialog(this) == DialogResult.OK)
{
string FileName;
FileName = openSourceFileDialog.FileName;
textBoxSourceFileName.Text = FileName;
}
}
おわり