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. The form is rendered as HTML.
• 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 an org.w3c.dom.Document object.
• convertDataSource: Converts an org.w3c.dom.Document object to a BLOB object. This method accepts an org.w3c.dom.Document object as an input parameter and returns a BLOB object.
• renderPOForm: Uses the Forms ES service web service API to render a dynamic purchase order form. The BLOB 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.)/** Ensure that you create the Java proxy classes to use* base64 encoding. This is required to populate a BLOB* object with data or retrieve data from a BLOB object.** For information, see "Creating Java proxy classes using Apache Axis"* 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 java.io.ByteArrayOutputStream;//Import DOM librariesimport org.w3c.dom.Element;import javax.xml.parsers.*;//Import Java Tranformation classesimport javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import javax.xml.rpc.holders.LongHolder;import javax.xml.rpc.holders.StringHolder;import com.adobe.idp.services.BLOB;import com.adobe.idp.services.FormsService;import com.adobe.idp.services.FormsServiceServiceLocator;import com.adobe.idp.services.HTMLRenderSpec;import com.adobe.idp.services.HTMLToolbar;import com.adobe.idp.services.TransformTo;import com.adobe.idp.services.URLSpec;import com.adobe.idp.services.holders.BLOBHolder;import com.adobe.idp.services.holders.FormsResultHolder;public class RenderPrepopulateFormWS 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 {//Create an org.w3c.dom.Document objectorg.w3c.dom.Document myDom = createDataSource();//Convert the org.w3c.dom.Document object//to a BLOB objectBLOB formData = convertDataSource(myDom);//Render the dynamic form using data located within the//BLOB 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 BLOB objectprivate BLOB 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 occured: "+e.getMessage());}//Create a BLOB object and copy the contents of the byte arrayBLOB formData = new BLOB();formData.setBinaryData(mybytes);return formData;}//Render the purchase order form using//the specified BLOB objectprivate void renderPOForm(HttpServletResponse resp, BLOB formData){try{com.adobe.idp.services.FormsServiceServiceLocator sl = new FormsServiceServiceLocator();FormsService formsOb = sl.getFormsService();((javax.xml.rpc.Stub)formsOb)._setProperty(javax.xml.rpc.Stub.USERNAME_PROPERTY, "administrator");((javax.xml.rpc.Stub)formsOb)._setProperty(javax.xml.rpc.Stub.PASSWORD_PROPERTY, "password");//Create an HTMLRenderSpec object to store//HTML run-time optionsHTMLRenderSpec htmlRS = new HTMLRenderSpec();htmlRS.setHTMLToolbar(HTMLToolbar.Vertical);//Specify URI values used by the Forms serviceURLSpec uriValues = new URLSpec();uriValues.setApplicationWebRoot("http://localhost:8080/FormsServiceClientApp");uriValues.setContentRootURI("C:\\Adobe");uriValues.setTargetURL("http://localhost:8080/FormsServiceClientApp/HandleData");//Create class holder objectsBLOBHolder outRenderPDFFormResultDoc = new BLOBHolder();FormsResultHolder formsResult = new FormsResultHolder();BLOBHolder blobHolder = new BLOBHolder();LongHolder longHolder = new LongHolder();StringHolder stringHolder = new StringHolder();StringHolder stringHolder2 = new StringHolder();//Invoke the renderHTMLForm method to render//an HTML formformsOb.renderHTMLForm("PO.xdp",TransformTo.MSDHTML,null,htmlRS ,"",uriValues,null,outRenderPDFFormResultDoc,blobHolder,longHolder,stringHolder,stringHolder2,formsResult);//Create a BLOB object that contains form dataBLOB formData2 = formsResult.value.getOutputContent();//Get the content type of the respose and//set the HttpServletResponse object’s content typeString contentType = formData2.getContentType();resp.setContentType(contentType);//Create a ServletOutputStream objectServletOutputStream oOutput = resp.getOutputStream();//Create a byte array that stores form data in the BLOB objectbyte[] cContent = formData.getBinaryData();//Write a byte stream back to the web browser.//Pass the byte arrayoOutput.write(cContent);}catch (Exception e) {System.out.println("The following error 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/000083.html