Class PdfSignatureAppearance


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

      • NOT_CERTIFIED

        public static final int NOT_CERTIFIED
        Approval signature
        See Also:
        Constant Field Values
      • CERTIFIED_NO_CHANGES_ALLOWED

        public static final int CERTIFIED_NO_CHANGES_ALLOWED
        Author signature, no changes allowed
        See Also:
        Constant Field Values
      • CERTIFIED_FORM_FILLING

        public static final int CERTIFIED_FORM_FILLING
        Author signature, form filling allowed
        See Also:
        Constant Field Values
      • CERTIFIED_FORM_FILLING_AND_ANNOTATIONS

        public static final int CERTIFIED_FORM_FILLING_AND_ANNOTATIONS
        Author signature, form filling and annotations allowed
        See Also:
        Constant Field Values
      • questionMark

        public static final String questionMark
        An appearance that can be used for layer 1 (if acro6Layers is false).
        See Also:
        Constant Field Values
    • Method Detail

      • setCertificationLevel

        public void setCertificationLevel​(int certificationLevel)
        Sets the document type to certified instead of simply signed.
        Parameters:
        certificationLevel - the values can be: NOT_CERTIFIED, CERTIFIED_NO_CHANGES_ALLOWED, CERTIFIED_FORM_FILLING and CERTIFIED_FORM_FILLING_AND_ANNOTATIONS
      • getCertificationLevel

        public int getCertificationLevel()
        Gets the certified status of this document.
        Returns:
        the certified status
      • getReason

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

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

        public void setReasonCaption​(String reasonCaption)
        Sets the caption for signing reason.
        Parameters:
        reasonCaption - the signing reason caption
      • getLocation

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

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

        public void setLocationCaption​(String locationCaption)
        Sets the caption for the signing location.
        Parameters:
        locationCaption - the signing location caption
      • getSignatureCreator

        public String getSignatureCreator()
        Gets the signature creator.
        Returns:
        the signature creator
      • setSignatureCreator

        public void setSignatureCreator​(String signatureCreator)
        Sets the name of the application used to create the signature.
        Parameters:
        signatureCreator - the name of the signature creating application
      • getContact

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

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

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

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

        public InputStream getRangeStream()
                                   throws IOException
        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
        Throws:
        IOException
      • addDeveloperExtension

        public void addDeveloperExtension​(PdfDeveloperExtension de)
        Adds the appropriate developer extension.
      • 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
      • setCertificate

        public void setCertificate​(Certificate signCertificate)
        Sets the certificate used to provide the text in the appearance. This certificate doesn't take part in the actual signing process.
        Parameters:
        signCertificate - the certificate
      • setSignatureEvent

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

        public String getFieldName()
        Gets the field name.
        Returns:
        the field name
      • getNewSigName

        public String getNewSigName()
        Gets a new signature field name that doesn't clash with any existing name.
        Returns:
        a new signature field name
      • getPage

        public int getPage()
        Gets the page number of the field.
        Returns:
        the page number of the field
      • 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
      • 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
      • isInvisible

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

        public void setVisibleSignature​(Rectangle pageRect,
                                        int page,
                                        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​(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
      • getRenderingMode

        public PdfSignatureAppearance.RenderingMode getRenderingMode()
        Gets the rendering mode for this signature.
        Returns:
        the rendering mode for this signature
        Since:
        5.0.1
      • setRenderingMode

        public void setRenderingMode​(PdfSignatureAppearance.RenderingMode renderingMode)
        Sets the rendering mode for this signature.
        Parameters:
        renderingMode - the rendering mode
        Since:
        5.0.1
      • getSignatureGraphic

        public Image getSignatureGraphic()
        Gets the Image object to render.
        Returns:
        the image
      • setSignatureGraphic

        public void setSignatureGraphic​(Image signatureGraphic)
        Sets the Image object to render when Render is set to RenderingMode.GRAPHIC or RenderingMode.GRAPHIC_AND_DESCRIPTION.
        Parameters:
        signatureGraphic - image rendered. If null the mode is defaulted to RenderingMode.DESCRIPTION
      • isAcro6Layers

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

        public void setAcro6Layers​(boolean acro6Layers)
        Deprecated.
        Adobe no longer supports Adobe Acrobat / Reader versions older than 9
        Acrobat 6.0 and higher recommends that only layer n0 and n2 be present. Use this method with value false if you want to ignore this recommendation.
        Parameters:
        acro6Layers - if true only the layers n0 and n2 will be present
      • getLayer

        public PdfTemplate getLayer​(int layer)
        Gets a template layer to create a signature appearance. The layers can go from 0 to 4, but only layer 0 and 2 will be used if acro6Layers is true.

        Consult PPKAppearances.pdf for further details.

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

        public void setReuseAppearance​(boolean reuseAppearance)
        Indicates that the existing appearances needs to be reused as layer 0.
      • getImage

        public Image getImage()
        Gets the background image for the layer 2.
        Returns:
        the background image for the layer 2
      • setImage

        public void setImage​(Image image)
        Sets the background image for the layer 2.
        Parameters:
        image - the background image for the layer 2
      • 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
      • setLayer2Text

        public void setLayer2Text​(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 String getLayer2Text()
        Gets the signature text identifying the signer if set by setLayer2Text().
        Returns:
        the signature text identifying the signer
      • 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
      • 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
      • setLayer4Text

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

        public String getLayer4Text()
        Gets the text identifying the signature status if set by setLayer4Text().
        Returns:
        the text identifying the signature status
      • 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
      • getStamper

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

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

        public PdfSigLockDictionary getFieldLockDict()
        Getter for the field lock dictionary.
        Returns:
        Field lock dictionary.
      • setFieldLockDict

        public void setFieldLockDict​(PdfSigLockDictionary fieldLock)
        Setter for the field lock dictionary.

        Be aware: if a signature is created on an existing signature field, then its /Lock dictionary takes the precedence (if it exists).

        Parameters:
        fieldLock - Field lock dictionary.
      • 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
      • preClose

        public void preClose​(HashMap<PdfName,​Integer> exclusionSizes)
                      throws 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().

        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:
        IOException - on error
        DocumentException - on error