Quick Start (SwaRef): Digitally signing a PDF document using the web service API

The following Java code example digitally signs a PDF document that is based on a PDF file named LoanSig.pdf. The alias that is specified for the security credential is secure, and revocation checking is performed. Because no CRL or OCSP server information is specified, the server information is obtained from the certificate used to digitally sign the PDF document. The signed document is saved as a PDF file named LoanSigned.pdf. (See Digitally Signing PDF Documents.)

/** 
    * Ensure that you create Java proxy files that consume 
    * the Signature services  WSDL. You can use JAX-WS to create 
    * the proxy Java files.   
    *    
    * For information, see "Invoking LiveCycle using SwaRef" in Programming with LiveCycle   
    */ 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.xml.ws.BindingProvider; 
 
import com.adobe.idp.services.*; 
 
public class SignDocumentSWAref { 
 
public static void main(String[] args) { 
         
    try 
    { 
        // Create a SignatureServiceService object 
        SignatureServiceService signatureService = new SignatureServiceService(); 
        SignatureService signatureClient =  signatureService.getSignatureservice(); 
         
        //Set connection settings 
        String url = "http://hiro-xp:8080/soap/services/signatureservice?blob=swaRef"; 
        String username = "administrator"; 
        String password = "password"; 
         
        ((BindingProvider) signatureClient).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); 
        ((BindingProvider) signatureClient).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username); 
        ((BindingProvider) signatureClient).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password); 
             
        //Specify a PDF document to sign 
        String path1 = "C:\\Adobe\LoanSig.pdf"; 
        FileDataSource ds1 = new FileDataSource(path1);  
        DataHandler handler1 = new DataHandler(ds1); 
        BLOB inDoc = new BLOB(); 
        inDoc.setSwaRef(handler1); 
     
        //Specify the name of the signature field 
        String fieldName = "SignatureField1"; 
 
        //Create a Credential object 
        Credential myCred = new Credential(); 
        myCred.setAlias("secure"); 
 
        //Specify the reason to sign the document 
        String reason = "The document was reviewed"; 
 
        //Specify the location of the signer 
        String location  = "New York HQ";  
 
        //Specify contact information 
        String contactInfo = "Tony Blue"; 
 
        //Create a PDFSignatureAppearanceOptions object  
        //and show date information 
        PDFSignatureAppearanceOptionSpec appear = new  PDFSignatureAppearanceOptionSpec();  
        appear.setShowDate(true); 
        appear.setShowReason(true); 
     
        //Set revocation checking to false 
        java.lang.Boolean revCheck = new Boolean(true); 
     
        //Create an OCSPOptionSpec object to pass to the sign method 
              OCSPOptionSpec ocspSpec = new OCSPOptionSpec(); 
          
              //Create a CRLOptionSpec object to pass to the sign method 
              CRLOptionSpec crlSpec = new CRLOptionSpec(); 
          
              //Create a TSPOptionSpec object to pass to the sign method 
              TSPOptionSpec tspSpec = new TSPOptionSpec(); 
 
              //Sign the PDF document 
              BLOB signedDoc = signatureClient.sign( 
                      inDoc, 
                      fieldName, 
                      myCred, 
                      HashAlgorithm.SHA_1, 
                      reason, 
                      location, 
                      contactInfo, 
                      appear, 
                      revCheck, 
                      ocspSpec, 
                      crlSpec, 
                      tspSpec); 
     
         //Create a new file containing the returned PDF document 
        File f = new File("C:\\Adobe\LoanSigned.pdf"); 
        BLOB outBlob = signedDoc; 
        DataHandler handler2 = outBlob.getSwaRef(); 
         
        //Get an InputStream from DataHandler and 
        //write to a file 
        InputStream inputStream = handler2.getInputStream(); 
        OutputStream out = new FileOutputStream(f); 
         
        //Iterate through the buffer 
        byte buf[] = new byte[1024]; 
        int len; 
        while ((len = inputStream.read(buf)) > 0) 
            out.write(buf, 0, len); 
        out.close(); 
        inputStream.close(); 
 
      } 
     
    catch (Exception ee) 
      { 
        ee.printStackTrace(); 
      } 
      } 
} 

// Ethnio survey code removed