// pdfファイルのパスワードを解除し、解除済みの一時ファイルのパスを返す。
// 一時ファイルは使用した後に消去すること。
public string DetachPassword(string path, string password)
{
// pathで渡されたファイルが存在するか?
if (!File.Exists(path))
{
throw new FileNotFoundException();
}
var reader = new iTextSharp.text.pdf.PdfReader(path, Encoding.UTF8.GetBytes(password));
string tmp = Path.GetTempFileName();
using (FileStream fs = new FileStream(tmp, FileMode.Create))
{
iTextSharp.text.pdf.PdfCopyFields copy = new iTextSharp.text.pdf.PdfCopyFields(fs);
copy.AddDocument(reader);
copy.Close();
}
reader.Close();
//一時ファイル名を元の名前にリネーム
string renamepath = Path.GetDirectoryName(tmp) + "\\" + Path.GetFileName(path);
File.Copy(tmp, renamepath, true);
File.Delete(tmp);
return renamepath;
}