Client Code Samples

This page contains a collection of working code samples for rendering documents using the Docmosis Cloud Service. You'll need to create a cloud account and then give it a try from your application...

Pick the snippet

Java Snippet using JSON - Download Full Sample

package com.docmosis.sample;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

/**
 * This sample code shows a render of the "samples/WelcomeTemplate.doc" template using JSON format
 * instruction and data.
 *
 * You need to set your access key before this example will run.  This key is your unique access to
 * the Docmosis services (keep it safe). You can see your access key in your Account Settings in the
 * Cloud area Docmosis web site.
 *
 * If you have troubles connecting, it may be because you have a proxy server you need to
 * configure. See the more complete example which provides proxy settings.
 *
 * Copyright Docmosis 2012
 *
 */

public class JavaBasicJSONExample
{

        private static final String DWS_RENDER_URL = "https://dws.docmosis.com/services/rs/render";

        /*
         * Run this example
         */

        public static void main(String[] args) throws MalformedURLException,
                        IOException
        {
                // Set your access Key
                String accessKey = "SET ME";
                if ("SET ME".equals(accessKey)) {
                        System.err.println("Please set your private access key from your Docmosis cloud account.");
                        System.exit(1);
                }

                HttpURLConnection conn = null;
                try {
                        conn = (HttpURLConnection) new URL(DWS_RENDER_URL).openConnection();
                        System.out.println("Connecting [directly] to " + DWS_RENDER_URL);

                        // set connection parameters
                        conn.setRequestMethod("POST");
                        conn.setUseCaches(false);
                        conn.setDoOutput(true);
                        conn.setDoInput(true);

                        // this example uses JSON format
                        conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");

                        conn.connect();
                        System.out.println("Connected");

                        final String outputFileName = "myWelcome.doc";

                        // build request
                        String templateName = "samples/WelcomeTemplate.doc";
                        StringBuffer sb = new StringBuffer();

                        // Start building the instruction
                        sb.append("{\n");
                        sb.append("\"accessKey\":\"").append(accessKey).append("\",\n");
                        sb.append("\"templateName\":\"").append(templateName).append("\",\n");
                        sb.append("\"outputName\":\"").append(outputFileName).append("\",\n");

                        // now add the data specifically for this template
                        sb.append("\"data\":{\n");
                        sb.append("\"date\":\"").append(new Date()).append("\",\n");
                        sb.append("\"title\":\"Welcome to Docmosis in the Cloud\",\n");
                        sb.append("\"messages\":[\n");
                        String[] messages = { "This cloud experience is better than I thought.",
                                        "The sun is shining", "Right, now back to work." };
                        for (int i = 0; i < messages.length; i++) {
                                sb.append("{\"msg\":\"").append(messages[i]).append("\"}");
                                if (i < messages.length - 1) {
                                        sb.append(',');
                                }
                                sb.append("\n");
                        }
                        sb.append("]}\n");
                        sb.append("}\n");

                        System.out.println("Sending request:" + sb.toString());

                        // send the instruction in UTF-8 encoding so that most character sets are available
                        OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
                        os.write(sb.toString());
                        os.flush();
                       
                        int status = conn.getResponseCode();
                        if (status == 200) {
                                // successful render,
                                // save our document to a file
                                byte[] buff = new byte[1000];
                                int bytesRead = 0;

                                File file = new File(outputFileName);
                                FileOutputStream fos = new FileOutputStream(file);
                                try {
                                        while ((bytesRead = conn.getInputStream().read(buff, 0, buff.length)) != -1) {
                                                fos.write(buff, 0, bytesRead);
                                        }
                                } finally {
                                        fos.close();
                                }

                                System.out.println("Created file:" + file.getAbsolutePath());
                        } else {
                                // something went wrong - tell the user
                                System.err.println("Our call failed: status = " + status);
                                System.err.println("message:" + conn.getResponseMessage());
                                BufferedReader errorReader = new BufferedReader(
                                                new InputStreamReader(conn.getErrorStream()));
                                String msg;
                                while ((msg = errorReader.readLine()) != null) {
                                        System.err.println(msg);
                                }
                                errorReader = null;
                        }
                       
                } catch (ConnectException e) {
                        // can't make the connection
                        System.err.println("Unable to connect to the docmosis cloud:" + e.getMessage());
                        System.err.println("If you have a proxy, you will need the Proxy aware example code.");
                        System.exit(2);
                } finally {
                        if (conn != null) {
                                conn.disconnect();
                        }
                }

        }
}

Java Snippet using XML - Download Full Sample

package com.docmosis.sample;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

/**
 * This sample code shows a render of the "samples/WelcomeTemplate.doc" template using XML format
 * instruction and data.
 *
 * You need to set your access key before this example will run.  This key is your unique access to
 * the Docmosis services (keep it safe). You can see your access key in your Account Settings in the
 * Cloud area Docmosis web site.
 *
 * If you have troubles connecting, it may be because you have a proxy server you need to
 * configure. See the more complete example which provides proxy settings.
 *
 * Copyright Docmosis 2012
 *
 */

public class JavaBasicXMLExample
{
        private static final String DWS_RENDER_URL = "https://dws.docmosis.com/services/rs/render";

        /*
         * Run this example
         */

        public static void main(String[] args) throws MalformedURLException,
                        IOException
        {
                // Set your access Key
                String accessKey = "SET ME";
                if ("SET ME".equals(accessKey)) {
                        System.err.println("Please set your private access key from your Docmosis cloud account.");
                        System.exit(1);
                }

                HttpURLConnection conn = null;
                try {
                        conn = (HttpURLConnection) new URL(DWS_RENDER_URL).openConnection();
                        System.out.println("Connecting [directly] to " + DWS_RENDER_URL);

                        // set connection parameters
                        conn.setRequestMethod("POST");
                        conn.setUseCaches(false);
                        conn.setDoOutput(true);
                        conn.setDoInput(true);

                        // this example uses JSON format
                        conn.setRequestProperty("Content-Type", "application/xml; charset=utf-8");

                        conn.connect();
                        System.out.println("Connected");

                        final String outputFileName = "myWelcome.doc";

                        // build request
                        String templateName = "samples/WelcomeTemplate.doc";
                        StringBuffer sb = new StringBuffer();

                        // Start building the instruction
                        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                        sb.append("<render \n");
                        sb.append("accessKey=\"").append(accessKey).append("\" ");
                        sb.append("templateName=\"").append(templateName).append("\" ");
                        sb.append("outputName=\"").append(outputFileName).append("\">\n");

                        // now add the data specifically for this template
                        sb.append("<data>\n");
                        sb.append("<date>").append(new Date()).append("</date>\n");
                        sb.append("<title>Welcome to Docmosis in the Cloud</title>\n");
                        String[] messages = { "This cloud experience is better than I thought.",
                                        "The sun is shining", "Right, now back to work." };
                        for (int i = 0; i < messages.length; i++) {
                                sb.append("<messages msg=\"").append(messages[i]).append("\"/>\n");
                        }
                        sb.append("</data>\n");
                        sb.append("</render>\n");

                        System.out.println("Sending request:" + sb.toString());

                        // send the instruction in UTF-8 encoding so that most character sets are available
                        OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
                        os.write(sb.toString());
                        os.flush();
                       
                        int status = conn.getResponseCode();
                        if (status == 200) {
                                // successful render,
                                // save our document to a file
                                byte[] buff = new byte[1000];
                                int bytesRead = 0;

                                File file = new File(outputFileName);
                                FileOutputStream fos = new FileOutputStream(file);
                                try {
                                        while ((bytesRead = conn.getInputStream().read(buff, 0, buff.length)) != -1) {
                                                fos.write(buff, 0, bytesRead);
                                        }
                                } finally {
                                        fos.close();
                                }

                                System.out.println("Created file:" + file.getAbsolutePath());
                        } else {
                                // something went wrong - tell the user
                                System.err.println("Our call failed: status = " + status);
                                System.err.println("message:" + conn.getResponseMessage());
                                BufferedReader errorReader = new BufferedReader(
                                                new InputStreamReader(conn.getErrorStream()));
                                String msg;
                                while ((msg = errorReader.readLine()) != null) {
                                        System.err.println(msg);
                                }
                                errorReader = null;
                        }
                       
                } catch (ConnectException e) {
                        // can't make the connection
                        System.err.println("Unable to connect to the docmosis cloud:" + e.getMessage());
                        System.err.println("If you have a proxy, you will need the Proxy aware example code.");
                        System.exit(2);
                } finally {
                        if (conn != null) {
                                conn.disconnect();
                        }
                }

        }
}