Search code examples
javaitextsignature

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


I want to create signature appearance using custom text and real signature timestamp (as used rending mode description) with the method appearance.setLayer2Text(). I read the book 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() as signature time setting the layer 2 text but I don't want it. I want to use really signature timestamp at setLayer2Text() with some custom text.

thanks in advance for any help.


Solution

  • I'm not sure I clearly understand your question.

    At first you appeared to want to have the exact datetime from a digital signature time stamp in the visualization of that signature. This is not possible, see the first section below.

    Meanwhile your comments point in the direction that you simply want to use the datetime also used by iText when creating the layer 2 text and the signing time entry in the signature. This is trivial, see the second section below.

    Datetime from a digital signature time stamp

    This is not possible: The visualization of the signature is an annotation in the PDF and therefore it is part of the signed content. Thus, the visualization must be generated before the digital timestamp is requested. Consequentially you cannot read the time from the timestamp early enough to put into the signature appearance.

    Of course you can try to be as near to it as possible, e.g. by first requesting a digital timestamp and immediately thereafter constructing the appearance using that timestamp and signing with a new timestamp, but you may be a bit off.

    If your signature certification level does not forbid it, you could also change the signature appearance after signing in an incremental update. In this case, though, Adobe Reader will warn about changes after signing...

    Datetime used by iText when creating layer 2 text and signature

    On the other hand, if you simply want to use the datetime also used by iText when creating the layer 2 text and the signing time entry in the signature, the solution is trivial: The PdfSignatureAppearance class has a SignDate property:

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

    It is initialized with the current time in the PdfSignatureAppearance constructor:

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

    This is the datetime used by iText when creating the text in 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()));
        [...]
    }
    

    This property has a public getter and a public setter

    /**
     * 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)
    

    Thus, you can retrieve the time to use here and you can even set it yourself!