我正在尝试在 pdf 文档中创建签名,但没有原因和位置。
StampingProperties properties = new StampingProperties();
properties.UseAppendMode();
string fieldName = $"signature_{obj.Signer.Replace(" ", "_")}_{count}";
var IMG = "sig.jpg";
SignerProperties signerProperties = new SignerProperties();
signerProperties.SetPageRect(rectangle);
signerProperties.SetPageNumber(pageNr);
signerProperties.SetFieldName(fieldName);
signerProperties.SetSignatureCreator(signer);
signerProperties.SetSignDate(signDate);
signerProperties.SetReason("");
signerProperties.SetLocation("");
var signatureAppearance = new SignatureFieldAppearance(fieldName);
signatureAppearance.SetContent(new SignedAppearanceText().SetReasonLine("").SetLocationLine(""));
signatureAppearance.SetBackgroundImage(
new BackgroundImage.Builder()
.SetImage(new PdfImageXObject(ImageDataFactory.Create(IMG)))
.Build());
signerProperties.SetSignatureAppearance(signatureAppearance);
PdfSigner signer = new PdfSigner(pdfReader, outputStream, null, properties, signerProperties);
signer.SignDetached(eidSignature, chain, crlList, null, tsaClient, 0, PdfSigner.CryptoStandard.CADES);
我尝试将 SignatureFieldAppearance 和 SignerProperties 中的原因和位置设置为空字符串,但这不起作用。 我需要更改什么才能使其正常工作?
事实上,过去可以使用空字符串或
null
字符串来表示原因和位置,以使 iText 删除签名外观中的关联行。这不能以明显的方式工作,这是一个回归,现已在开发源中修复,请参阅此提交。
如果您需要官方 iText 版本并且无法从当前源编译自己的版本,则有一个解决方法(由 iText 开发建议):您可以创建一个自定义签名外观文本类,该类接受特定的非空、非空信号字符串以删除原因和/或位置,如下所示:
class CustomSignedAppearanceText : SignedAppearanceText
{
public override string GenerateDescriptionText()
{
StringBuilder stringBuilder = new StringBuilder();
string signedBy = GetSignedBy();
if (signedBy != null && !string.IsNullOrEmpty(signedBy))
{
stringBuilder.Append("Digitally signed by ").Append(signedBy);
}
if (isSignDateSet)
{
stringBuilder.Append('\n').Append("Date: ").Append(DateTimeUtil.DateToString(GetSignDate()));
}
string reason = GetReasonLine();
if (reason != null && !string.IsNullOrEmpty(reason) && !"DROP".Equals(reason))
{
stringBuilder.Append('\n').Append(reason);
}
string location = GetLocationLine();
if (location != null && !string.IsNullOrEmpty(location) && !"DROP".Equals(location))
{
stringBuilder.Append('\n').Append(location);
}
return stringBuilder.ToString();
}
public override SignedAppearanceText SetSignDate(DateTime signDate)
{
isSignDateSet = true;
return base.SetSignDate(signDate);
}
bool isSignDateSet = false;
}
(SignWithoutReasonOrLocation辅助类)
您可以使用“DROP”来表示您希望删除有问题的线路:
signatureAppearance.SetContent(new CustomSignedAppearanceText().SetReasonLine("DROP").SetLocationLine("DROP"));
(无原因或位置签名测试
SignLikeTaniaVanderstraetenWorkAround
)