作成日: 2024/07/24 最終更新日: 2024/08/08
文書種別
使用方法
詳細
DioDocs for PDF は PDF長期署名 (PAdES, PDF Advanced Electronic Signatures) に対応しています。
PAdESの仕様や設定方法は製品ヘルプの「デジタル署名」をご確認ください。
未署名のPDFに対してLTAレベルで署名する場合、以下のような手順になります。
// 未署名のPDFを読み込む
var fs = File.OpenRead("sample.pdf");
var doc = new GcPdfDocument();
doc.Load(fs);
var page = doc.Pages.First();
var tf = new TextFormat()
{
Font = FontCollection.SystemFonts.FindFamilyName("MS Gothic"),
FontSize = 10,
};
// 署名フィールド(署名の視覚情報)を追加
var sf = new SignatureField();
sf.Widget.Rect = new RectangleF(72, 72, 288, 36);
sf.Widget.Page = page;
doc.AcroForm.Fields.Add(sf);
// 電子署名証明書、中間CA証明書、ルート証明書(※1)
var cert_sign = new X509Certificate2(File.ReadAllBytes("sample.pfx"), "password");
var cert_ica = new X509Certificate2(File.ReadAllBytes("InterCA.crt"));
var cert_rca = new X509Certificate2(File.ReadAllBytes("RootCA.crt"));
// タイムスタンプ(※2)
var timeProp = new TimeStampProperties()
{
TimeStamp = new TimeStamp(@"http://ts.ssl.com")
};
// 署名プロパティ
var signProp = new SignatureProperties()
{
SignatureField = sf,
SignatureBuilder = new Pkcs7SignatureBuilder(cert_sign)
{
Format = Pkcs7SignatureBuilder.SignatureFormat.ETSI_CAdES_detached
},
Location = "コンピューター名または物理的な位置",
SignerName = "氏名",
TimeStamp = timeProp.TimeStamp
};
signProp.SignatureAppearance.TextFormat = tf;
using(var fs_sign = new FileStream("signature.pdf", FileMode.Create, FileAccess.ReadWrite))
{
// タイムスタンプを含むデジタル署名 (B-T level)
doc.Sign(signProp, fs_sign);
// デジタル署名に検証情報を追加 (B-LT level)
doc.AcroForm.Fields.OfType<SignatureField>()
.Where(sf => sf.Value.Type == "Sig")
.ToList()
.ForEach(sf => {
var vp = new DocumentSecurityStore.VerificationParams();
vp.Certificates = [cert_ica, cert_rca];
doc.SecurityStore.AddVerification(sf.Value, vp);
});
// ドキュメントに対してタイムスタンプを付与
doc.TimeStamp(timeProp, fs_sign);
// タイムスタンプに検証情報を追加 (B-LTA level)
doc.AcroForm.Fields.OfType<SignatureField>()
.Where(sf => sf.Value.Type == "DocTimeStamp")
.ToList()
.ForEach(sf => {
doc.SecurityStore.AddVerification(sf.Value);
});
// 増分更新で保存
doc.Save(fs_sign, SaveMode.IncrementalUpdate);
}
※1
署名や検証に必要となるファイルの種類は証明書の仕様に依存します。
認証局にご確認ください。
※2
実際の運用では認定時刻認証業務をご利用ください。
総務省|タイムスタンプ・eシール|タイムスタンプについて