Class PdfSignatureAppearance


  • public class PdfSignatureAppearance
    extends java.lang.Object
    This class takes care of the cryptographic options and appearances that form a signature.
    • Field Detail

      • SELF_SIGNED

        public static final PdfName SELF_SIGNED
        The self signed filter.
      • VERISIGN_SIGNED

        public static final PdfName VERISIGN_SIGNED
        The VeriSign filter.
      • WINCER_SIGNED

        public static final PdfName WINCER_SIGNED
        The Windows Certificate Security.
      • layer2Text

        private java.lang.String layer2Text
      • reason

        private java.lang.String reason
      • location

        private java.lang.String location
      • signDate

        private java.util.Calendar signDate
      • provider

        private java.lang.String provider
      • page

        private int page
      • fieldName

        private java.lang.String fieldName
      • privKey

        private java.security.PrivateKey privKey
      • certChain

        private java.security.cert.Certificate[] certChain
      • crlList

        private java.security.cert.CRL[] crlList
      • newField

        private boolean newField
      • originalout

        private java.io.OutputStream originalout
      • tempFile

        private java.io.File tempFile
      • preClosed

        private boolean preClosed
      • range

        private int[] range
      • raf

        private java.io.RandomAccessFile raf
      • bout

        private byte[] bout
      • boutLen

        private int boutLen
      • externalDigest

        private byte[] externalDigest
      • externalRSAdata

        private byte[] externalRSAdata
      • digestEncryptionAlgorithm

        private java.lang.String digestEncryptionAlgorithm
      • exclusionLocations

        private java.util.HashMap exclusionLocations
      • questionMark

        public static final java.lang.String questionMark
        Commands to draw a yellow question mark in a stream content
        See Also:
        Constant Field Values
      • contact

        private java.lang.String contact
        Holds value of property contact.
      • layer2Font

        private Font layer2Font
        Holds value of property layer2Font.
      • layer4Text

        private java.lang.String layer4Text
        Holds value of property layer4Text.
      • acro6Layers

        private boolean acro6Layers
        Holds value of property acro6Layers.
      • runDirection

        private int runDirection
        Holds value of property runDirection.
      • imageScale

        private float imageScale
        Holds value of property imageScale.
    • Constructor Detail

      • PdfSignatureAppearance

        PdfSignatureAppearance​(PdfStamperImp writer)
    • Method Detail

      • setLayer2Text

        public void setLayer2Text​(java.lang.String text)
        Sets the signature text identifying the signer.
        Parameters:
        text - the signature text identifying the signer. If null or not set a standard description will be used
      • getLayer2Text

        public java.lang.String getLayer2Text()
        Gets the signature text identifying the signer if set by setLayer2Text().
        Returns:
        the signature text identifying the signer
      • setLayer4Text

        public void setLayer4Text​(java.lang.String text)
        Sets the text identifying the signature status.
        Parameters:
        text - the text identifying the signature status. If null or not set the description "Signature Not Verified" will be used
      • getLayer4Text

        public java.lang.String getLayer4Text()
        Gets the text identifying the signature status if set by setLayer4Text().
        Returns:
        the text identifying the signature status
      • getRect

        public Rectangle getRect()
        Gets the rectangle representing the signature dimensions.
        Returns:
        the rectangle representing the signature dimensions. It may be null or have zero width or height for invisible signatures
      • isInvisible

        public boolean isInvisible()
        Gets the visibility status of the signature.
        Returns:
        the visibility status of the signature
      • setCrypto

        public void setCrypto​(java.security.PrivateKey privKey,
                              java.security.cert.Certificate[] certChain,
                              java.security.cert.CRL[] crlList,
                              PdfName filter)
        Sets the cryptographic parameters.
        Parameters:
        privKey - the private key
        certChain - the certificate chain
        crlList - the certificate revocation list. It may be null
        filter - the crytographic filter type. It can be SELF_SIGNED, VERISIGN_SIGNED or WINCER_SIGNED
      • setVisibleSignature

        public void setVisibleSignature​(Rectangle pageRect,
                                        int page,
                                        java.lang.String fieldName)
        Sets the signature to be visible. It creates a new visible signature field.
        Parameters:
        pageRect - the position and dimension of the field in the page
        page - the page to place the field. The fist page is 1
        fieldName - the field name or null to generate automatically a new field name
      • setVisibleSignature

        public void setVisibleSignature​(java.lang.String fieldName)
        Sets the signature to be visible. An empty signature field with the same name must already exist.
        Parameters:
        fieldName - the existing empty signature field name
      • getLayer

        public PdfTemplate getLayer​(int layer)
        Gets a template layer to create a signature appearance. The layers can go from 0 to 4.

        Consult PPKAppearances.pdf for further details.

        Parameters:
        layer - the layer
        Returns:
        a template
      • getTopLayer

        public PdfTemplate getTopLayer()
        Gets the template that aggregates all appearance layers. This corresponds to the /FRM resource.

        Consult PPKAppearances.pdf for further details.

        Returns:
        the template that aggregates all appearance layers
      • fitText

        public static float fitText​(Font font,
                                    java.lang.String text,
                                    Rectangle rect,
                                    float maxFontSize,
                                    int runDirection)
        Fits the text to some rectangle adjusting the font size as needed.
        Parameters:
        font - the font to use
        text - the text
        rect - the rectangle where the text must fit
        maxFontSize - the maximum font size
        runDirection - the run direction
        Returns:
        the calculated font size that makes the text fit
      • setExternalDigest

        public void setExternalDigest​(byte[] digest,
                                      byte[] RSAdata,
                                      java.lang.String digestEncryptionAlgorithm)
        Sets the digest/signature to an external calculated value.
        Parameters:
        digest - the digest. This is the actual signature
        RSAdata - the extra data that goes into the data tag in PKCS#7
        digestEncryptionAlgorithm - the encryption algorithm. It may must be null if the digest is also null. If the digest is not null then it may be "RSA" or "DSA"
      • getReason

        public java.lang.String getReason()
        Gets the signing reason.
        Returns:
        the signing reason
      • setReason

        public void setReason​(java.lang.String reason)
        Sets the signing reason.
        Parameters:
        reason - the signing reason
      • getLocation

        public java.lang.String getLocation()
        Gets the signing location.
        Returns:
        the signing location
      • setLocation

        public void setLocation​(java.lang.String location)
        Sets the signing location.
        Parameters:
        location - the signing location
      • getProvider

        public java.lang.String getProvider()
        Returns the Cryptographic Service Provider that will sign the document.
        Returns:
        provider the name of the provider, for example "SUN", or null to use the default provider.
      • setProvider

        public void setProvider​(java.lang.String provider)
        Sets the Cryptographic Service Provider that will sign the document.
        Parameters:
        provider - the name of the provider, for example "SUN", or null to use the default provider.
      • getPrivKey

        public java.security.PrivateKey getPrivKey()
        Gets the private key.
        Returns:
        the private key
      • getCertChain

        public java.security.cert.Certificate[] getCertChain()
        Gets the certificate chain.
        Returns:
        the certificate chain
      • getCrlList

        public java.security.cert.CRL[] getCrlList()
        Gets the certificate revocation list.
        Returns:
        the certificate revocation list
      • getFilter

        public PdfName getFilter()
        Gets the filter used to sign the document.
        Returns:
        the filter used to sign the document
      • isNewField

        public boolean isNewField()
        Checks if a new field was created.
        Returns:
        true if a new field was created, false if signing an existing field or if the signature is invisible
      • getPage

        public int getPage()
        Gets the page number of the field.
        Returns:
        the page number of the field
      • getFieldName

        public java.lang.String getFieldName()
        Gets the field name.
        Returns:
        the field name
      • getPageRect

        public Rectangle getPageRect()
        Gets the rectangle that represent the position and dimension of the signature in the page.
        Returns:
        the rectangle that represent the position and dimension of the signature in the page
      • getSignDate

        public java.util.Calendar getSignDate()
        Gets the signature date.
        Returns:
        the signature date
      • setSignDate

        public void setSignDate​(java.util.Calendar signDate)
        Sets the signature date.
        Parameters:
        signDate - the signature date
      • setSigout

        void setSigout​(ByteBuffer sigout)
      • getOriginalout

        java.io.OutputStream getOriginalout()
      • setOriginalout

        void setOriginalout​(java.io.OutputStream originalout)
      • getTempFile

        public java.io.File getTempFile()
        Gets the temporary file.
        Returns:
        the temporary file or null is the document is created in memory
      • setTempFile

        void setTempFile​(java.io.File tempFile)
      • getNewSigName

        public java.lang.String getNewSigName()
        Gets a new signature fied name that doesn't clash with any existing name.
        Returns:
        a new signature fied name
      • preClose

        public void preClose()
                      throws java.io.IOException,
                             DocumentException
        This is the first method to be called when using external signatures. The general sequence is: preClose(), getDocumentBytes() and close().

        If calling preClose() dont't call PdfStamper.close().

        No external signatures are allowed if this methos is called.

        Throws:
        java.io.IOException - on error
        DocumentException - on error
      • preClose

        public void preClose​(java.util.HashMap exclusionSizes)
                      throws java.io.IOException,
                             DocumentException
        This is the first method to be called when using external signatures. The general sequence is: preClose(), getDocumentBytes() and close().

        If calling preClose() dont't call PdfStamper.close().

        If using an external signature exclusionSizes must contain at least the PdfName.CONTENTS key with the size that it will take in the document. Note that due to the hex string coding this size should be byte_size*2+2.

        Parameters:
        exclusionSizes - a HashMap with names and sizes to be excluded in the signature calculation. The key is a PdfName and the value an Integer. At least the PdfName.CONTENTS must be present
        Throws:
        java.io.IOException - on error
        DocumentException - on error
      • close

        public void close​(PdfDictionary update)
                   throws java.io.IOException,
                          DocumentException
        This is the last method to be called when using external signatures. The general sequence is: preClose(), getDocumentBytes() and close().

        update is a PdfDictionary that must have exactly the same keys as the ones provided in preClose(HashMap).

        Parameters:
        update - a PdfDictionary with the key/value that will fill the holes defined in preClose(HashMap)
        Throws:
        DocumentException - on error
        java.io.IOException - on error
      • getRangeStream

        public java.io.InputStream getRangeStream()
        Gets the document bytes that are hashable when using external signatures. The general sequence is: preClose(), getRangeStream() and close().

        Returns:
        the document bytes that are hashable
      • getCryptoDictionary

        public PdfDictionary getCryptoDictionary()
        Gets the user made signature dictionary. This is the dictionary at the /V key.
        Returns:
        the user made signature dictionary
      • setCryptoDictionary

        public void setCryptoDictionary​(PdfDictionary cryptoDictionary)
        Sets a user made signature dictionary. This is the dictionary at the /V key.
        Parameters:
        cryptoDictionary - a user made signature dictionary
      • getStamper

        public PdfStamper getStamper()
        Gets the PdfStamper associated with this instance.
        Returns:
        the PdfStamper associated with this instance
      • setStamper

        void setStamper​(PdfStamper stamper)
      • isPreClosed

        public boolean isPreClosed()
        Checks if the document is in the process of closing.
        Returns:
        true if the document is in the process of closing, false otherwise
      • getSigStandard

        public PdfSigGenericPKCS getSigStandard()
        Gets the instance of the standard signature dictionary. This instance is only available after pre close.

        The main use is to insert external signatures.

        Returns:
        the instance of the standard signature dictionary
      • getContact

        public java.lang.String getContact()
        Gets the signing contact.
        Returns:
        the signing contact
      • setContact

        public void setContact​(java.lang.String contact)
        Sets the signing contact.
        Parameters:
        contact - the signing contact
      • getLayer2Font

        public Font getLayer2Font()
        Gets the n2 and n4 layer font.
        Returns:
        the n2 and n4 layer font
      • setLayer2Font

        public void setLayer2Font​(Font layer2Font)
        Sets the n2 and n4 layer font. If the font size is zero, auto-fit will be used.
        Parameters:
        layer2Font - the n2 and n4 font
      • isAcro6Layers

        public boolean isAcro6Layers()
        Gets the Acrobat 6.0 layer mode.
        Returns:
        the Acrobat 6.0 layer mode
      • setAcro6Layers

        public void setAcro6Layers​(boolean acro6Layers)
        Acrobat 6.0 and higher recomends that only layer n2 and n4 be present. This method sets that mode.
        Parameters:
        acro6Layers - if true only the layers n2 and n4 will be present
      • setRunDirection

        public void setRunDirection​(int runDirection)
        Sets the run direction in the n2 and n4 layer.
        Parameters:
        runDirection - the run direction
      • getRunDirection

        public int getRunDirection()
        Gets the run direction.
        Returns:
        the run direction
      • setSignatureEvent

        public void setSignatureEvent​(PdfSignatureAppearance.SignatureEvent signatureEvent)
        Sets the signature event to allow modification of the signature dictionary.
        Parameters:
        signatureEvent - the signature event
      • getImageScale

        public float getImageScale()
        Gets the scaling to be applied to the background image.
        Returns:
        the scaling to be applied to the background image
      • setImageScale

        public void setImageScale​(float imageScale)
        Sets the scaling to be applied to the background image. If it's zero the image will fully fill the rectangle. If it's less than zero the image will fill the rectangle but will keep the proportions. If it's greater than zero that scaling will be applied. In any of the cases the image will always be centered. It's zero by default.
        Parameters:
        imageScale - the scaling to be applied to the background image