Class CreateVisibleSignature

  • All Implemented Interfaces:
    org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface

    public class CreateVisibleSignature
    extends CreateSignatureBase
    This is an example for visual signing a pdf.
    Author:
    Vakhtang Koroghlishvili
    See Also:
    CreateSignature
    • Constructor Summary

      Constructors 
      Constructor Description
      CreateVisibleSignature​(java.security.KeyStore keystore, char[] pin)
      Initialize the signature creator with a keystore (pkcs12) and pin that should be used for the signature.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction getStreamCacheCreateFunction()
      Get the function to be used to create an instance of the choosen stream cache.
      boolean isLateExternalSigning()  
      static void main​(java.lang.String[] args)
      Arguments are [0] key store [1] pin [2] document that will be signed [3] image of visible signature
      void setLateExternalSigning​(boolean lateExternalSigning)
      Set late external signing.
      void setStreamCacheCreateFunction​(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction streamCache)
      Set the function to be used to create an instance of the choosen stream cache.
      void setVisibleSignatureProperties​(java.lang.String name, java.lang.String location, java.lang.String reason, boolean visualSignEnabled)
      Set visible signature properties for existing signature fields.
      void setVisibleSignatureProperties​(java.lang.String name, java.lang.String location, java.lang.String reason, int preferredSize, int page, boolean visualSignEnabled)
      Set visible signature properties for new signature fields.
      void setVisibleSignDesigner​(int zoomPercent, java.io.InputStream imageStream)
      Set visible signature designer for an existing signature field.
      void setVisibleSignDesigner​(java.lang.String filename, int x, int y, int zoomPercent, java.io.InputStream imageStream, int page)
      Open the PDF, create and set the visible signature designer for a new signature field.
      void signPDF​(java.io.File inputFile, java.io.File signedFile, java.lang.String tsaUrl)
      Sign pdf file and create new file that ends with "_signed.pdf".
      void signPDF​(java.io.File inputFile, java.io.File signedFile, java.lang.String tsaUrl, java.lang.String signatureFieldName)
      Sign pdf file and create new file that ends with "_signed.pdf".
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CreateVisibleSignature

        public CreateVisibleSignature​(java.security.KeyStore keystore,
                                      char[] pin)
                               throws java.security.KeyStoreException,
                                      java.security.UnrecoverableKeyException,
                                      java.security.NoSuchAlgorithmException,
                                      java.io.IOException,
                                      java.security.cert.CertificateException
        Initialize the signature creator with a keystore (pkcs12) and pin that should be used for the signature.
        Parameters:
        keystore - is a pkcs12 keystore.
        pin - is the pin for the keystore / private key
        Throws:
        java.security.KeyStoreException - if the keystore has not been initialized (loaded)
        java.security.NoSuchAlgorithmException - if the algorithm for recovering the key cannot be found
        java.security.UnrecoverableKeyException - if the given password is wrong
        java.security.cert.CertificateException - if the certificate is not valid as signing time
        java.io.IOException - if no certificate could be found
    • Method Detail

      • isLateExternalSigning

        public boolean isLateExternalSigning()
      • setLateExternalSigning

        public void setLateExternalSigning​(boolean lateExternalSigning)
        Set late external signing. Enable this if you want to activate the demo code where the signature is kept and added in an extra step without using PDFBox methods. This is disabled by default.
        Parameters:
        lateExternalSigning -
      • getStreamCacheCreateFunction

        public org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction getStreamCacheCreateFunction()
        Get the function to be used to create an instance of the choosen stream cache.
        Returns:
        the function to be used to create an instance of the choosen stream cache
      • setStreamCacheCreateFunction

        public void setStreamCacheCreateFunction​(org.apache.pdfbox.io.RandomAccessStreamCache.StreamCacheCreateFunction streamCache)
        Set the function to be used to create an instance of the choosen stream cache.
        Parameters:
        streamCache - the function to be used to create an instance of the choosen stream cache
      • setVisibleSignDesigner

        public void setVisibleSignDesigner​(java.lang.String filename,
                                           int x,
                                           int y,
                                           int zoomPercent,
                                           java.io.InputStream imageStream,
                                           int page)
                                    throws java.io.IOException
        Open the PDF, create and set the visible signature designer for a new signature field.
        Parameters:
        filename - path of the PDF file
        x - position of the signature field
        y - position of the signature field
        zoomPercent - increase (positive value) or decrease (negative value) image with x percent.
        imageStream - input stream of an image.
        page - the signature should be placed on (1-based)
        Throws:
        java.io.IOException
      • setVisibleSignDesigner

        public void setVisibleSignDesigner​(int zoomPercent,
                                           java.io.InputStream imageStream)
                                    throws java.io.IOException
        Set visible signature designer for an existing signature field.
        Parameters:
        zoomPercent - increase (positive value) or decrease (negative value) image with x percent.
        imageStream - input stream of an image.
        Throws:
        java.io.IOException
      • setVisibleSignatureProperties

        public void setVisibleSignatureProperties​(java.lang.String name,
                                                  java.lang.String location,
                                                  java.lang.String reason,
                                                  int preferredSize,
                                                  int page,
                                                  boolean visualSignEnabled)
        Set visible signature properties for new signature fields.
        Parameters:
        name -
        location -
        reason -
        preferredSize -
        page - the signature should be placed on (1-based)
        visualSignEnabled -
      • setVisibleSignatureProperties

        public void setVisibleSignatureProperties​(java.lang.String name,
                                                  java.lang.String location,
                                                  java.lang.String reason,
                                                  boolean visualSignEnabled)
        Set visible signature properties for existing signature fields.
        Parameters:
        name -
        location -
        reason -
        visualSignEnabled -
      • signPDF

        public void signPDF​(java.io.File inputFile,
                            java.io.File signedFile,
                            java.lang.String tsaUrl)
                     throws java.io.IOException
        Sign pdf file and create new file that ends with "_signed.pdf".
        Parameters:
        inputFile - The source pdf document file.
        signedFile - The file to be signed.
        tsaUrl - optional TSA url
        Throws:
        java.io.IOException
      • signPDF

        public void signPDF​(java.io.File inputFile,
                            java.io.File signedFile,
                            java.lang.String tsaUrl,
                            java.lang.String signatureFieldName)
                     throws java.io.IOException
        Sign pdf file and create new file that ends with "_signed.pdf".
        Parameters:
        inputFile - The source pdf document file. It will be opened if it hasn't been opened before in setVisibleSignDesigner(java.lang.String, int, int, int, java.io.InputStream, int).
        signedFile - The file to be signed.
        tsaUrl - optional TSA url
        signatureFieldName - optional name of an existing (unsigned) signature field
        Throws:
        java.io.IOException
      • main

        public static void main​(java.lang.String[] args)
                         throws java.security.KeyStoreException,
                                java.security.cert.CertificateException,
                                java.io.IOException,
                                java.security.NoSuchAlgorithmException,
                                java.security.UnrecoverableKeyException
        Arguments are [0] key store [1] pin [2] document that will be signed [3] image of visible signature
        Parameters:
        args -
        Throws:
        java.security.KeyStoreException
        java.security.cert.CertificateException
        java.io.IOException
        java.security.NoSuchAlgorithmException
        java.security.UnrecoverableKeyException