The following code example prepopulates a dynamic form with a dynamic data source. That is, the data source is created at run-time and is not contained within an XML file or created during design time. This code example contains three user-defined methods:
• createDataSource: Creates an org.w3c.dom.Document object that represents the data source that is used to prepopulate the form. This user-defined method returns the org.w3c.dom.Document object.
• convertDataSource: Converts an org.w3c.dom.Document object to a com.adobe.idp.Document object. This method accepts an org.w3c.dom.Document object as an input parameter and returns a com.adobe.idp.Document object.
• renderPOForm: Uses the Forms ES service Java API to render a dynamic purchase order form. The com.adobe.idp.Document object that was returned by the convertDataSource method is used to prepopulate the form.All of these methods are invoked from within the Java servlet’s doPost method. (See Prepopulating Dynamic 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>/Adobe/LiveCycle8/LiveCycle_ES_SDK/client-libs** For complete details about the location of these JAR files,* see "Including LiveCycle ES library files" in Programming with LiveCycle ES*/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.ByteArrayOutputStream;import java.io.InputStream;import com.adobe.idp.Document;import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;import com.adobe.idp.dsc.clientsdk.ServiceClientFactoryProperties;import org.w3c.dom.Element;import javax.xml.parsers.*;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;public class RenderDynamicForm 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 {//Render a dynamic purchase order form//Create an org.w3c.dom.Document objectorg.w3c.dom.Document myDom = createDataSource();//Convert the org.w3c.dom.Document object//to a com.adobe.idp.Document objectcom.adobe.idp.Document formData = convertDataSource(myDom);//Render the dynamic form using data located within the//com.adobe.idp.Document objectrenderPOForm(resp,formData);}//Creates an org.w3c.dom.Document objectprivate org.w3c.dom.Document createDataSource(){org.w3c.dom.Document document = null;try{//Create DocumentBuilderFactory and DocumentBuilder objectsDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();DocumentBuilder builder = factory.newDocumentBuilder();//Create a new Document objectdocument = builder.newDocument();//Create the root element and append it to the XML DOMElement root = (Element)document.createElement("transaction");document.appendChild(root);//Create the header elementElement header = (Element)document.createElement("header");root.appendChild(header);//Create the txtPONum element and append it to the//header elementElement txtPONum = (Element)document.createElement("txtPONum");txtPONum.appendChild(document.createTextNode("8745236985"));header.appendChild(txtPONum);//Create the dtmDate element and append it to the//header elementElement dtmDate = (Element)document.createElement("dtmDate");dtmDate.appendChild(document.createTextNode("2007-02-08"));header.appendChild(dtmDate);//Create the orderedByAddress element and append//it to the header elementElement orderedByAddress =(Element)document.createElement("orderedByAddress");orderedByAddress.appendChild(document.createTextNode("222, Any Blvd"));header.appendChild(orderedByAddress);//Create the txtOrderedByPhone element and append//it to the header elementElement txtOrderedByPhone = (Element)document.createElement("txtOrderedByPhone");txtOrderedByPhone.appendChild(document.createTextNode("(555) 555-2334"));header.appendChild(txtOrderedByPhone);//Create the txtOrderedByFax element and append//it to the header elementElement txtOrderedByFax = (Element)document.createElement("txtOrderedByFax");txtOrderedByFax.appendChild(document.createTextNode("(555) 555-9334"));header.appendChild(txtOrderedByFax);//Create the txtOrderedByContactName element and append//it to the header elementElement txtOrderedByContactName = (Element)document.createElement("txtOrderedByContactName");txtOrderedByContactName.appendChild(document.createTextNode("Frank Jones"));header.appendChild(txtOrderedByContactName);//Create the deliverToAddress element and append//it to the header elementElement deliverToAddress = (Element)document.createElement("deliverToAddress");deliverToAddress.appendChild(document.createTextNode("555, Any Blvd"));header.appendChild(deliverToAddress);//Create the txtDeliverToPhone element and append//it to the header elementElement txtDeliverToPhone = (Element)document.createElement("txtDeliverToPhone");txtDeliverToPhone.appendChild(document.createTextNode("(555) 555-9098"));header.appendChild(txtDeliverToPhone);//Create the txtDeliverToFax element and append//it to the header elementElement txtDeliverToFax = (Element)document.createElement("txtDeliverToFax");txtDeliverToFax.appendChild(document.createTextNode("(555) 555-9000"));header.appendChild(txtDeliverToFax);//Create the txtDeliverToContactName element and//append it to the header elementElement txtDeliverToContactName = (Element)document.createElement("txtDeliverToContactName");txtDeliverToContactName.appendChild(document.createTextNode("Jerry Johnson"));header.appendChild(txtDeliverToContactName);//Create the detail element and append it to the rootElement detail = (Element)document.createElement("detail");root.appendChild(detail);//Create the txtPartNum element and append it to the//detail elementElement txtPartNum = (Element)document.createElement("txtPartNum");txtPartNum.appendChild(document.createTextNode("00010-100"));detail.appendChild(txtPartNum);//Create the txtDescription element and append it//to the detail elementElement txtDescription = (Element)document.createElement("txtDescription");txtDescription.appendChild(document.createTextNode("Monitor"));detail.appendChild(txtDescription);//Create the numQty element and append it to//the detail elementElement numQty = (Element)document.createElement("numQty");numQty.appendChild(document.createTextNode("1"));detail.appendChild(numQty);//Create the numUnitPrice element and append it//to the detail elementElement numUnitPrice = (Element)document.createElement("numUnitPrice");numUnitPrice.appendChild(document.createTextNode("350.00"));detail.appendChild(numUnitPrice);//Create another detail element named detail2 and//append it to rootElement detail2 = (Element)document.createElement("detail");root.appendChild(detail2);//Create the txtPartNum element and append it to the//detail2 elementElement txtPartNum2 = (Element)document.createElement("txtPartNum");txtPartNum2.appendChild(document.createTextNode("00010-200"));detail2.appendChild(txtPartNum2);//Create the txtDescription element and append it//to the detail2 elementElement txtDescription2 = (Element)document.createElement("txtDescription");txtDescription2.appendChild(document.createTextNode("Desk lamps"));detail2.appendChild(txtDescription2);//Create the numQty element and append it to the//detail2 elementElement numQty2 = (Element)document.createElement("numQty");numQty2.appendChild(document.createTextNode("3"));detail2.appendChild(numQty2);//Create the NUMUNITPRICE elementElement numUnitPrice2 = (Element)document.createElement("numUnitPrice");numUnitPrice2.appendChild(document.createTextNode("55.00"));detail2.appendChild(numUnitPrice2);}catch (Exception e) {System.out.println("The following exception occurred: "+e.getMessage());}return document;}//Converts an org.w3c.dom.Document object to a//com.adobe.idp.Document objectprivate Document convertDataSource(org.w3c.dom.Document myDOM){byte[] mybytes = null;try{//Create a Java Transformer objectTransformerFactory transFact = TransformerFactory.newInstance();Transformer transForm = transFact.newTransformer();//Create a Java ByteArrayOutputStream objectByteArrayOutputStream myOutStream = new ByteArrayOutputStream();//Create a Java Source objectjavax.xml.transform.dom.DOMSource myInput = new DOMSource(myDOM);//Create a Java Result objectjavax.xml.transform.stream.StreamResult myOutput = new StreamResult(myOutStream);//Populate the Java ByteArrayOutputStream objecttransForm.transform(myInput,myOutput);// Get the size of the ByteArrayOutputStream bufferint myByteSize = myOutStream.size();//Allocate myByteSize to the byte arraymybytes = new byte[myByteSize];//Copy the content to the byte arraymybytes = myOutStream.toByteArray();}catch (Exception e) {System.out.println("The following exception occurred: "+e.getMessage());}//Create a com.adobe.idp.Document object and copy the//contents of the byte arrayDocument myDocument = new Document(mybytes);return myDocument;}//Render the purchase order form using the specified//com.adobe.idp.Document objectprivate void renderPOForm(HttpServletResponse resp, Document formData){try{//Set connection properties required to invoke LiveCycle ESProperties 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 objectServiceClientFactory myFactory = ServiceClientFactory.createInstance(connectionProps);//Create a FormsServiceClient objectFormsServiceClient formsClient = new FormsServiceClient(myFactory);//Set the parameter values for the renderPDFForm methodString formName = "PO.xdp";//Cache the formPDFFormRenderSpec pdfFormRenderSpec = new PDFFormRenderSpec();pdfFormRenderSpec.setCacheEnabled(new Boolean(true));//Specify URI valuesURLSpec uriValues = new URLSpec();uriValues.setApplicationWebRoot("http://localhost:8080/FormsServiceClientApp");uriValues.setContentRootURI("C:\\Adobe");uriValues.setTargetURL("http://localhost:8080/FormsServiceClientApp/HandleData");//Invoke the renderForm methodFormsResult formOut = formsClient.renderPDFForm(formName, //formQueryformData, //inDataDocpdfFormRenderSpec, //PDFFormRenderSpecuriValues, //urlSpecnull //attachments);//Create a ServletOutputStream objectServletOutputStream oOutput = resp.getOutputStream();//Create a Document object that stores form dataDocument myData = formOut.getOutputContent();//Create an InputStream objectInputStream inputStream = myData.getInputStream();//Get the size of the InputStream objectint size = inputStream.available();//Create and populate a byte arraybyte[] data = new byte[size];inputStream.read(data);//Write the data stream to the web browseroOutput.write(data);}catch (Exception e) {System.out.println("The following exception occurred: "+e.getMessage());}}}
| Programming with LiveCycle ES (LiveDocs) |
| Adobe LiveCycle ES Update 1 |
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/livecycle/8.2/programLC/programmer/help/000082.html