Quick Start (EJB mode): Rendering an interactive PDF form using the Java API

The following code example renders an interactive PDF form named Loan.xdp to a client web browser. A file is attached to the form. Notice that the form design is part of a LiveCycle application and is referenced by using the content root URI value repository:///. (See Rendering Interactive PDF Forms.)

/* 
    * This Java Quick Start uses the following JAR files 
    * 1. adobe-forms-client.jar 
    * 2. adobe-livecycle-client.jar 
    * 3. adobe-usermanager-client.jar 
    *  
    *  (Because Forms quick starts are implemented as Java servlets, it is  
    *  not necessary to include J2EE specific JAR files - the Java project 
    *  that contains this quick start is exported as a WAR file which 
    *  is deployed to the J2EE application server) 
    *   
    *  These JAR files are located in the following path: 
    * <install directory>/sdk/client-libs/common 
    *  
    * For complete details about the location of these JAR files,  
    * see "Including LiveCycle library files" in Programming with LiveCycle. 
    */ 
import java.io.FileInputStream; 
import java.io.IOException; 
import javax.servlet.Servlet; 
import javax.servlet.ServletException; 
import javax.servlet.ServletOutputStream; 
 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import com.adobe.livecycle.formsservice.client.*; 
import java.util.*; 
import java.io.InputStream; 
 
import com.adobe.idp.Document; 
import com.adobe.idp.dsc.clientsdk.ServiceClientFactory; 
import com.adobe.idp.dsc.clientsdk.ServiceClientFactoryProperties; 
 
public class RenderPDFForm extends HttpServlet implements Servlet { 
 
    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException { 
            doPost(req,resp); 
    } 
     
    public void doPost(HttpServletRequest req, HttpServletResponse resp) 
        throws ServletException, IOException { 
        try{ 
            //Set connection properties required to invoke LiveCycle                                 
            Properties connectionProps = new Properties(); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_DEFAULT_EJB_ENDPOINT, "jnp://localhost:1099"); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_TRANSPORT_PROTOCOL,ServiceClientFactoryProperties.DSC_EJB_PROTOCOL);           
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_SERVER_TYPE, "JBoss"); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_USERNAME, "administrator"); 
            connectionProps.setProperty(ServiceClientFactoryProperties.DSC_CREDENTIAL_PASSWORD, "password"); 
             
            //Create a ServiceClientFactory object 
            ServiceClientFactory myFactory = ServiceClientFactory.createInstance(connectionProps); 
                 
            //Create a FormsServiceClient object 
            FormsServiceClient formsClient = new FormsServiceClient(myFactory);  
         
            //Set the parameter values for the renderPDFForm method 
            String formName = "Applications/FormsApplication/1.0/FormsFolder/Loan.xdp";   
                     
            byte[]    cData = "".getBytes(); 
            Document oInputData = new Document(cData); 
                                     
            //Set run-time options using a PDFFormRenderSpec instance 
            PDFFormRenderSpec pdfFormRenderSpec = new PDFFormRenderSpec(); 
            pdfFormRenderSpec.setCacheEnabled(new Boolean(true)); 
            pdfFormRenderSpec.setAcrobatVersion(AcrobatVersion.Acrobat_9); 
                     
            //Specify URI values that are required to render a form 
            URLSpec uriValues = new URLSpec();  
            uriValues.setApplicationWebRoot("http://localhost:8080/FormsQS"); 
            uriValues.setContentRootURI("repository:///"); 
            uriValues.setTargetURL("http://localhost:8080/FormsQS/HandleData"); 
                     
            //Specify file attachments to attach to the form 
            FileInputStream fileAttachment = new FileInputStream("C:\\rideau1.jpg");  
            Document attachment1 = new Document(fileAttachment); 
            String fileName = "rideau1.jpg"; 
            Map fileAttachments = new HashMap(); 
            fileAttachments.put(fileName, attachment1);  
                         
            //Invoke the renderPDFForm method and write the  
            //results to a client web browser 
            FormsResult formOut = formsClient.renderPDFForm( 
                        formName,               //formQuery 
                        oInputData,             //inDataDoc 
                        pdfFormRenderSpec,      //PDFFormRenderSpec 
                        uriValues,                //urlSpec 
                        fileAttachments            //attachments 
                        ); 
         
            //Create a Document object that stores form data 
            Document myData = formOut.getOutputContent(); 
         
            //Get the content type of the response and 
            //set the HttpServletResponse objects content type 
            String contentType = myData.getContentType();  
            resp.setContentType(contentType); 
         
            //Create a ServletOutputStream object 
            ServletOutputStream oOutput = resp.getOutputStream(); 
         
            //Create an InputStream object 
            InputStream inputStream = myData.getInputStream(); 
         
            //Write the data stream to the web browser 
            byte[] data = new byte[4096]; 
            int bytesRead = 0; 
            while ((bytesRead = inputStream.read(data)) > 0) 
            { 
                oOutput.write(data, 0, bytesRead); 
            } 
         
            }catch (Exception e) { 
                 e.printStackTrace(); 
              } 
        } 
}