如何在自定义第 2 层文本中使用真实签名时间戳在 itextpdf 上创建签名外观

How to use real signature timestamp in custom layer 2 text on creating signature appearances at itextpdf

我想通过 appearance.setLayer2Text() 方法使用自定义文本和真实签名时间戳(如使用的渲染模式描述)创建签名外观。我读了书 Digital Signatures for PDF documents(pages 40-48 especially) and could not found a way how to do. rendering modes present a default description for the usage of metadata like name, signature stamp, reason and so on. I only need to signature timestamp from it and not else. I saw some examples using new Date() 作为签名时间设置第 2 层文本,但我不想要它。我想在 setLayer2Text() 中使用带有一些自定义文本的真正签名时间戳。

在此先感谢您的帮助。

我不确定我是否清楚地理解你的问题。

起初您似乎想要在该签名的可视化中从数字签名时间戳获得准确的日期时间。这是不可能的,请参阅下面的第一部分。

同时,您的评论指出您只想使用 iText 在创建第 2 层文本和签名中的签名时间条目时也使用的日期时间。这很简单,请参阅下面的第二部分。

来自数字签名时间戳的日期时间

这是不可能的:签名的可视化是 PDF 中的注释,因此它是签名内容的一部分。因此,必须在请求数字时间戳之前生成可视化。因此,您不能足够早地从时间戳中读取时间以放入签名外观。

当然你可以尽量靠近它,例如首先请求数字时间戳,然后立即使用该时间戳构建外观并使用新时间戳签名,但您可能有点偏离。

如果您的签名认证级别不禁止,您也可以在登录增量更新后更改签名外观。不过,在这种情况下,Adobe Reader 将在签名后警告更改...

iText 创建第 2 层文本和签名时使用的日期时间

另一方面,如果您只想使用 iText 在创建第 2 层文本时也使用的日期时间和签名中的签名时间条目,解决方案很简单: PdfSignatureAppearance class 有一个 SignDate 属性:

/** Holds value of property signDate. */
private Calendar signDate;

PdfSignatureAppearance构造函数中用当前时间初始化:

/**
 * Constructs a PdfSignatureAppearance object.
 * @param writer    the writer to which the signature will be written.
 */
PdfSignatureAppearance(PdfStamperImp writer) {
    [...]
    signDate = new GregorianCalendar();
    [...]
}

这是 iText 在 getAppearance 中创建文本时使用的日期时间:

if (layer2Text == null) {
    StringBuilder buf = new StringBuilder();
    buf.append("Digitally signed by ");
    [...]
    SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss z");
    buf.append("Date: ").append(sd.format(signDate.getTime()));
    [...]
}

这个属性有一个publicgetter和一个publicsetter

/**
 * Gets the signature date.
 * @return the signature date
 */
public java.util.Calendar getSignDate()

/**
 * Sets the signature date.
 * @param signDate the signature date
 */
public void setSignDate(java.util.Calendar signDate)

因此,您可以在这里检索使用时间,甚至可以自己设置!