com.aspose.pdf.facades

Class PdfFileSignature

  • All Implemented Interfaces:
    com.aspose.ms.System.IDisposable, IFacade, ISaveableFacade


    public final class PdfFileSignature
    extends SaveableFacade

    Represents a class to sign a pdf file with a certificate.

    • Constructor Detail

      • PdfFileSignature

        public PdfFileSignature()

        The constructor of PdfFileSignature class.

      • PdfFileSignature

        @Deprecated
        public PdfFileSignature(String inputFile)
        Deprecated. 

        The constructor of PdfFileSignature class.

        Parameters:
        inputFile - The input file for signature.
      • PdfFileSignature

        @Deprecated
        public PdfFileSignature(String inputFile,
                                   String outputFile)
        Deprecated. 

        The constructor of PdfFileSignature class.

        Parameters:
        inputFile - The input file for signature.
        outputFile - The output file.
      • PdfFileSignature

        public PdfFileSignature(IDocument document)

        Initializes new PdfFileSignature object on base of the document.

        Parameters:
        document - Pdf document.
      • PdfFileSignature

        @Deprecated
        public PdfFileSignature(IDocument document,
                                   String outputFile)
        Deprecated. 

        Initializes new PdfFileSignature object on base of the document.

        Parameters:
        document - Pdf document.
        outputFile - The output file.
    • Method Detail

      • getSignatureAppearance

        public String getSignatureAppearance()

        Gets a graphic appearance for the signature. Property value represents image file name.

        Returns:
        String value
      • setSignatureAppearance

        public void setSignatureAppearance(String value)

        Sets a graphic appearance for the signature. Property value represents image file name.

        Parameters:
        value - String value
      • bindPdf

        public void bindPdf(String inputFile)

        Binds a Pdf file for editing.

        Specified by:
        bindPdf in interface IFacade
        Overrides:
        bindPdf in class Facade
        Parameters:
        inputFile - The pdf file to be edited.
      • bindPdf

        public void bindPdf(InputStream inputStream)

        Binds a Pdf stream for editing.

        Specified by:
        bindPdf in interface IFacade
        Overrides:
        bindPdf in class Facade
        Parameters:
        inputStream - The pdf stream to be edited.
      • sign

        public void sign(int page,
                String SigReason,
                String SigContact,
                String SigLocation,
                boolean visible,
                Rectangle annotRect)

        Make a signature on the pdf document.


         
        
         String inFile = TestPath + "example1.pdf";
         String outFile = TestPath + "signature.pdf";
         PdfFileSignature pdfSign = new PdfFileSignature();
         pdfSign.bindPdf(inFile);
         Rectangle rect = new Rectangle(100, 100, 200, 200);
         pdfSign.setSignatureAppearance ( TestPath + "butterfly.jpg");
         pdfSign.setCertificate("certificate.pfx", "password");
         pdfSign.sign(2, "Allen", "success", "ChangSha", true, rect);
         pdfSign.save(outFile);
         
        Parameters:
        page - The page number on which signature is made.
        SigReason - The reason of signature.
        SigContact - The contact of signature.
        SigLocation - The location of signature.
        visible - The visiblity of signature.
        annotRect - The rect of signature.
      • sign

        public void sign(int page,
                String SigReason,
                String SigContact,
                String SigLocation,
                boolean visible,
                Rectangle annotRect,
                Signature sig)

        Sign the document with the given type signature.


        
         String inFile = TestPath + "example1.pdf";
         String outFile = TestPath + "signature.pdf";
         PdfFileSignature pdfSign = new PdfFileSignature(inFile, outFile);
         Rectangle rect = new Rectangle(100, 100, 200, 100);
         pdfSign.setSignatureAppearance ( TestPath + "butterfly.jpg");
         pdfSign.sign(2, "Allen", "success", "ChangSha", true, rect, new PKCS1("certificate.pfx", "password"));
         pdfSign.save();
         
        Parameters:
        page - The page number on which signature is made.
        SigReason - The reason of signature.
        SigContact - The contact of signature.
        SigLocation - The location of signature.
        visible - The visiblity of signature.
        annotRect - The rect of signature.
        sig - The type of the signature, could be PKCS1, PKCS7 and PKCS7Detached.
      • sign

        public void sign(int page,
                boolean visible,
                Rectangle annotRect,
                Signature sig)

        Sign the document with the given type signature.


        
         String inFile = TestPath + "example1.pdf";
         String outFile = TestPath + "signature.pdf";
         PKCS1 sig = new PKCS1("certificate.pfx", "password");
         sig.setReason ( "Some reason");
         sig.setContact ( "Smith");
         sig.setLocation ( "New York");
         PdfFileSignature pdfSign = new PdfFileSignature(inFile, outFile);
         Rectangle rect = new Rectangle(100, 100, 200, 100);
         pdfSign.setSignatureAppearance ( TestPath + "butterfly.jpg");
         pdfSign.sign(2, true, rect, sig);
         pdfSign.save();
         
        Parameters:
        page - The page number on which signature is made.
        visible - The visiblity of signature.
        annotRect - The rect of signature.
        sig - The type of the signature, could be PKCS1, PKCS7 and PKCS7Detached. Such data as signature reason, contact and location must be already present in this object (see corresponding properties).
      • sign

        public void sign(String SigName,
                String SigReason,
                String SigContact,
                String SigLocation,
                Signature sig)

        Sign the document with the given type signature which is placed in already presented signature field. Before signing signature field must be empty, i.e. field must not contain signature dictionary. Thus pdf document already has signature field, you should not supply the place to stamp the signature, corresponding page and rectangle are taken from signature field which is found by signature name (see SigName parameter).


        
         String inFile = TestPath + "example1.pdf";
         String outFile = TestPath + "signature.pdf";
         PKCS1 sig = new PKCS1("certificate.pfx", "password");
         PdfFileSignature pdfSign = new PdfFileSignature(inFile, outFile);
         pdfSign.setSignatureAppearance ( TestPath + "butterfly.jpg");
         pdfSign.sign("Signature1", "Allen", "success", "ChangSha", sig);
         pdfSign.save();
         
        Parameters:
        SigName - The name of the signature field.
        SigReason - The reason of signature.
        SigContact - The contact of signature.
        SigLocation - The location of signature.
        sig - The type of the signature, could be PKCS1, PKCS7 and PKCS7Detached.
      • sign

        public void sign(int page,
                String SigName,
                String SigReason,
                String SigContact,
                String SigLocation,
                boolean visible,
                Rectangle annotRect,
                Signature sig)

        Sign the document with the given type signature which is placed in already presented signature field. Before signing pdf document should already has signature field, corresponding page and rectangle are taken from signature field which is found by signature name (see SigName parameter).


         String inFile = TestPath + "blankWithSignature.pdf";
         String outFile = TestPath + "signature.pdf";
         PKCS7 sig = new PKCS7("certificate.pfx", "password");
         PdfFileSignature pdfSign = new PdfFileSignature(inFile);
         Rectangle rect = new Rectangle(100, 100, 100, 100);
         pdfSign.setSignatureAppearance( TestPath + "butterfly.jpg");
         pdfSign.sign(1, "Signature1", "ReasonToTest", "ContactMe", "SomeLocation", true, rect, sig);
         pdfSign.save(outFile);                
         
        Parameters:
        page - The page number on which signature is made.
        SigName - The name of the signature field.
        SigReason - The reason of signature.
        SigContact - The contact of signature.
        SigLocation - The location of signature.
        visible - The visiblity of signature.
        annotRect - The rect of signature.
        sig - The type of the signature, could be PKCS1, PKCS7 and PKCS7Detached.
      • sign

        public void sign(String SigName,
                Signature sig)

        Sign the document with the given type signature which is placed in already presented signature field. Before signing signature field must be empty, i.e. field must not contain signature dictionary. Thus pdf document already has signature field, you should not supply the place to stamp the signature, corresponding page and rectangle are taken from signature field which is found by signature name (see SigName parameter). Such data as signature reason, contact and location must be provided by corresponding properties of the Signature object sig.


         String inFile = TestPath + "example1.pdf";
         String outFile = TestPath + "signature.pdf";
         PKCS1 sig = new PKCS1("certificate.pfx", "password");
         sig.setReason ( "Some reason");
         sig.setContact ( "Smith");
         sig.setLocation ( "New York");
         PdfFileSignature pdfSign = new PdfFileSignature(inFile, outFile);
         pdfSign.setSignatureAppearance ( TestPath + "butterfly.jpg");
         pdfSign.sign("Signature1", sig);
         pdfSign.save();
         
        Parameters:
        SigName - The name of the signature field.
        sig - The type of the signature, could be PKCS1 (Pkcs1Signature object), PKCS7 and PKCS7 detached (Pkcs7Signature object)
      • isCertified

        public boolean isCertified()

        Gets the flag determining whether a document is certified or not.

        Returns:
        boolean value
      • getAccessPermissions

        public int getAccessPermissions()

        Returns the access permissions value of certified document by the MDP signature type.

        Returns:
        If the document is being certified, than returns access permissions value; otherwise, is thrown.
        See Also:
        DocMDPAccessPermissions
      • certify

        public void certify(int page,
                   String SigReason,
                   String SigContact,
                   String SigLocation,
                   boolean visible,
                   Rectangle annotRect,
                   DocMDPSignature docMdpSignature)

        Certify the document with the MDP signature. Such data as signature reason, contact and location must be provided by corresponding properties of the Signature object sig.

        Parameters:
        page - The page on which signature is made.
        SigReason - The reason of signature.
        SigContact - The contact of signature.
        SigLocation - The location of signature.
        visible - The visiblity of signature.
        annotRect - The rect of signature.
        docMdpSignature - The document MDP type of the signature.
      • getSignNames

        public List<String> getSignNames()

        Gets the names of all not empty signatures.


          String inFile=TestPath + "example1.pdf";
          PdfFileSignature pdfSign=new PdfFileSignature();
          pdfSign.bindPdf(inFile); 
          ArrayList names=pdfSign.getSignNames();
         for(int i=0;i<names.Count;i++)
         {
           System.out.println("signature name:"+(String)names[i]);
           System.out.println("coverswholedocument:"+pdfSign.IsCoversWholeDocument((String)names[i]));
           System.out.println("revision:"+pdfSign.GetRevision((String)names[i]));       
           System.out.println("verifysigned:"+pdfSign.VerifySigned((String)names[i]));
           System.out.println("reason:"+pdfSign.GetReason((String)names[i]));
           System.out.println("location:"+pdfSign.GetLocation((String)names[i]));
           System.out.println("datatime:"+pdfSign.GetDateTime((String)names[i]));               
          }
          System.out.println("totalvision:"+pdfSign.GetTotalRevision());
          
        Returns:
        Return a arrayList.
      • getBlankSignNames

        public List<String> getBlankSignNames()

        Gets the names of all empty signature fields.

        Returns:
        Return a arrayList.
      • isContainSignature

        @Deprecated
        public boolean isContainSignature()
        Deprecated. 

        Checks if the pdf has a digital signature or not.

        Returns:
        Return a result of bool type.
      • containsSignature

        public boolean containsSignature()

        Checks if the pdf has a digital signature or not.

        Returns:
        Return a result of bool type.
      • containsUsageRights

        public boolean containsUsageRights()

        Checks if the pdf has a usage rights or not.

        Returns:
        Returns a result of bool type.
      • isCoversWholeDocument

        @Deprecated
        public boolean isCoversWholeDocument(String signName)
        Deprecated. 

        Checks if the signature covers the whole document.

        Parameters:
        signName - The name of signature.
        Returns:
        Return a result of bool type.
      • coversWholeDocument

        public boolean coversWholeDocument(String signName)

        Checks if the signature covers the whole document.

        Parameters:
        signName - The name of signature.
        Returns:
        Return a result of bool type.
      • getRevision

        public int getRevision(String signName)

        Gets the revision of a signature.

        Parameters:
        signName - The name of signature.
        Returns:
        Return the number of signature revision.
      • getTotalRevision

        public int getTotalRevision()

        Gets the toltal revision.

        Returns:
        Return the total number of signature revision.
      • removeUsageRights

        public void removeUsageRights()

        Removes the usage rights entry.

      • removeSignature

        public void removeSignature(String signName)

        Remove the signature according to the name of the signature.


        
         String inFile = TestPath + "example1.pdf";
         PdfFileSignature pdfSign = new PdfFileSignature();
         pdfSign.bindPdf(inFile); 
         List names = pdfSign.getSignNames();
         for(int i = 0; i < names.size(); i++)
         {
            pdfSign.removeSignature((String)names.get(i));
         }
         pdfSign.save(TestPath + "signed_removed.pdf");
         
        Parameters:
        signName - The name of signature.
      • removeSignature

        public void removeSignature(String signName,
                           boolean removeField)

        Removes the signature according to the name of the signature.


         
         
         
         String inFile = TestPath + "example1.pdf";
         PdfFileSignature pdfSign = new PdfFileSignature();
         pdfSign.BindPdf(inFile); 
         List names = pdfSign.getSignNames();
         for(int i = 0; i < names.size(); i++)
         {
            pdfSign.removeSignature((String)names.get(i), false);
         }
         pdfSign.save(TestPath + "signed_removed.pdf");
         
        Parameters:
        signName - The name of signature.
        removeField - If set to true, than removes both of signature and field from document; otherwise, signature only.
      • verifySigned

        public boolean verifySigned(String signName)

        Checks the validity of a signature.

        Parameters:
        signName - The name of signature.
        Returns:
        Return a result of bool type.
      • getSignerName

        public String getSignerName(String signName)

        Gets the name of person or organization who signing the pdf document.

        Parameters:
        signName - The name of signature.
        Returns:
        Returns the result of the signer's name.
      • getDateTime

        public Date getDateTime(String signName)

        Gets the signature's datetime.

        Parameters:
        signName - The name of signature.
        Returns:
        Return the result of DateTime type.
      • getReason

        public String getReason(String signName)

        Gets the reason of a signature.

        Parameters:
        signName - The name of signature.
        Returns:
        Returns a result of String type.
      • getLocation

        public String getLocation(String signName)

        Gets the location of a signature.

        Parameters:
        signName - The name of signature.
        Returns:
        Returns a result of String type.
      • getContactInfo

        public String getContactInfo(String signName)

        Gets the contact information of a signature.

        Parameters:
        signName - The name of signature.
        Returns:
        Returns a result of String type.
      • verifySignature

        public boolean verifySignature(String signName)

        Checks the validity of a signature.

        Parameters:
        signName - The name of signature.
        Returns:
        Return a result of bool type.
      • extractImage

        public InputStream extractImage(String signName)

        Extracts signature's image.

        Parameters:
        signName - The name of signature.
        Returns:
        If image was successfully found than returns stream object; otherwise, null.
      • extractCertificate

        public InputStream extractCertificate(String signName)

        Extracts signature's single X.509 certificate as a stream.

        Parameters:
        signName - The name of signature.
        Returns:
        If certificate was found returns X.509 single certificate; otherwise, null.
      • save

        @Deprecated
        public void save()
        Deprecated. 

        Save signed pdf file. Output filename must be provided before with the help of coresponding PdfFileSignature constructor.

      • setCertificate

        public void setCertificate(String pfx,
                          String pass)

        Set certificate file and password for signing routine.

        Parameters:
        pfx - PKCS #12 certificate file.
        pass - Password to get access for the certificate private key.
      • getSignatureAppearanceStream

        public InputStream getSignatureAppearanceStream()

        Gets a graphic appearance for the signature. Property value represents image stream.

        Returns:
        InputStream element
      • setSignatureAppearanceStream

        public void setSignatureAppearanceStream(InputStream value)

        Sets a graphic appearance for the signature. Property value represents image stream.

        Parameters:
        value - InputStream element
      • close

        public void close()
        Closes the facade.
        Specified by:
        close in interface IFacade
        Overrides:
        close in class Facade
      • dispose

        public void dispose()
        Closes the facade.
        Specified by:
        dispose in interface com.aspose.ms.System.IDisposable
        Overrides:
        dispose in class Facade