Class PdfSignature

java.lang.Object
com.codename1.pdf.PdfSignature

public final class PdfSignature extends Object

Describes the digital signature PdfDocument.sign(PdfSignature) embeds when the document is saved.

The result is a PAdES baseline (B-B) signature: a detached CMS SignedData over the whole file, SHA-256, with the signer's certificate. Acrobat, Preview and pdfsig report it as signed; whether they call it trusted depends on the certificate, which is the application's choice, not this class's.

PrivateKey key = PrivateKey.fromPem(new String(Util.readInputStream(
        CN.getResourceAsStream("/signer-key.pem")), "UTF-8"));
byte[] certificateDer = Util.readInputStream(CN.getResourceAsStream("/signer.der"));
PdfDocument doc = new PdfDocument();
doc.addPage(PdfPageSize.A4);
// ... draw the certificate of completion ...
doc.sign(new PdfSignature(PdfSigner.provider(key, certificateDer))
        .setReason("Certificate of completion")
        .setLocation("Lingo Academy")
        .setVisible(doc.getPage(0), 380, 40, 180, 50));
byte[] signed = doc.toByteArray();
  • Constructor Details

    • PdfSignature

      public PdfSignature(PdfSignatureProvider provider)
      A signature whose key material comes from provider.
  • Method Details

    • getProvider

      public PdfSignatureProvider getProvider()
      The provider given to the constructor.
    • setName

      public PdfSignature setName(String name)
      The signer's name as shown by viewers; defaults to the certificate's common name.
    • getName

      public String getName()
      The signer's name, or null for the certificate's common name.
    • setReason

      public PdfSignature setReason(String reason)
      Why the document was signed.
    • getReason

      public String getReason()
      Why the document was signed, or null.
    • setLocation

      public PdfSignature setLocation(String location)
      Where the document was signed.
    • getLocation

      public String getLocation()
      Where the document was signed, or null.
    • setContactInfo

      public PdfSignature setContactInfo(String contactInfo)
      How to reach the signer.
    • getContactInfo

      public String getContactInfo()
      How to reach the signer, or null.
    • setSigningTime

      public PdfSignature setSigningTime(long millis)
      The claimed signing time, in milliseconds since the epoch; the current time by default. It is the signer's claim, not a timestamp authority's.
    • getSigningTime

      public long getSigningTime()
      The claimed signing time.
    • setVisible

      public PdfSignature setVisible(PdfPage page, float x, float y, float width, float height)
      Shows the signature as a box at (x, y, width, height) on page, in the page's top-left based coordinates, with the signer's name, the date and the reason. Without this the signature is invisible and attached to the first page.
    • isVisible

      public boolean isVisible()
      True when setVisible(PdfPage, float, float, float, float) placed the signature on a page.
    • setReservedBytes

      public PdfSignature setReservedBytes(int bytes)
      The space reserved in the file for the CMS signature, in bytes; 12 KB by default, enough for a signer certificate and two intermediates. Saving fails if the signature does not fit.
    • getReservedBytes

      public int getReservedBytes()
      The space reserved for the CMS signature, in bytes.