pdf签名上的自定义词典项目

问题描述 投票:0回答:1

我正在使用iTextSharp 5签署一份pdf文档。这是按预期工作的。我现在正在尝试向签名添加一些自定义属性(字典项)。

PdfStamper st = PdfStamper.CreateSignature(reader, signedPdf, '\0', null, true);
PdfSignatureAppearance sap = st.SignatureAppearance;

// Adding the custom properties
PdfDictionary dict = new PdfDictionary();
dict.Put(new PdfName("SomeKey"), new PdfString("SomeValue"));
// ... more keys and values
sap.CryptoDictionary = dict;

// to make some of the parameters clear, these are the types. 
// implementation was skipped for clarity, but is already working in our product though
ITSAClient tsaClient = ...
ICollection<ICrlClient> crlClients = ...
IOcspClient ocspClient = ...
IExternalSignature externalSignature = ...

// adding the signature to the document
MakeSignature.SignDetached(sap, externalSignature, chain, crlClients, ocspClient, tsaClient,
                        certSize, CryptoStandard.CMS);

我试图使用RUPS在文档中找到我的额外属性,但我无法找到它们。

此外,再次打开pdf时,使用以下代码无法找到数据(但我显然无法确认这是该作业的正确代码):

PdfDictionary sigDict = reader.AcroFields.GetSignatureDictionary(signature name);

当然,我可以将我的属性序列化为json并将它们放在隐藏的文本字段中,这就是我的后备场景。但我必须使用签名等保护该字段。我认为这种方法更多是pdf设计的实际方式。

对于此特定产品,由于不祥原因,无法升级到iText 7。但是我们有一个更新的产品,我需要在以后使用iText 7添加相同的功能。

是否有人知道这是否是正确的方法和我缺少的?我无法在pdf词典中结合签名找到好的编码示例。

c# itext sign
1个回答
0
投票

您可以在字典中设置自定义键,然后将其分配给签名外观的CryptoDictionary

PdfDictionary dict = new PdfDictionary();
dict.Put(new PdfName("SomeKey"), new PdfString("SomeValue"));
// ... more keys and values
sap.CryptoDictionary = dict;

然后你调用SignDetached实用方法:

MakeSignature.SignDetached(sap, externalSignature, chain, crlClients, ocspClient, tsaClient,
                    certSize, CryptoStandard.CMS);

不幸的是(对你来说),MakeSignature.SignDetached本身创建了一个新词典,它最终分配给签名外观的CryptoDictionary

PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, sigtype == CryptoStandard.CADES ? PdfName.ETSI_CADES_DETACHED : PdfName.ADBE_PKCS7_DETACHED);
dic.Reason = sap.Reason;
dic.Location = sap.Location;
dic.SignatureCreator = sap.SignatureCreator;
dic.Contact = sap.Contact;
dic.Date = new PdfDate(sap.SignDate); // time-stamp will over-rule this
sap.CryptoDictionary = dic; 

因此,您的字典及其自定义值将被删除!

要将自定义值添加到签名值字典,您必须更改方法。你可以

  • MakeSignature.SignDetached中的整个代码复制到您的代码中并添加您的自定义键和值;要么
  • 使用MakeSignature.SignExternalContainer而不是MakeSignature.SignDetached。这个实用方法给出了IExternalSignatureContainer实现,而不是IExternalSignature实现和其他参数,并且该接口有一个回调方法 /** * Modifies the signature dictionary to suit the container. At least the keys PdfName.FILTER and * PdfName.SUBFILTER will have to be set. * @param signDic the signature dictionary */ void ModifySigningDictionary(PdfDictionary signDic); 您可以在此处添加自定义键。但显然,你必须将MakeSignature.SignDetached中的一些代码复制到你的IExternalSignatureContainer实现中。

如果您不了解MakeSignature.SignDetached中的代码及其含义,那么前一个选项将更容易。另一方面,后一种选择会更加清晰,因为它允许您在实用程序方法中留下一些低级细节。

© www.soinside.com 2019 - 2024. All rights reserved.