Microsoft .NET Tutorial

This article describes how to use the LiveDocx web service within an easy Windows Forms application. Create professional documents without installing any software on your developer machine or deploying any additional assemblies to the target machine.

The following easy steps are required to access the LiveDocx API:

  1. In case you do not have one, create your free LiveDocx account:

    Create free LiveDocx account
  2. By creating an account, you have access to your template folder. You can browse your templates by clicking on My Templates when you are logged in with your credentials. Now, you can upload your templates that should be merged using the LiveDocx API.

    For the purpose of this sample application, we have created a template for you to use. Please place it in your My Templates folder:

    invoice_template.docx
  3. Create a new Windows Forms Application in your preferred language:

    Create new .NET Windows Forms Application
  4. Select the project LiveDocxSample in the Solution Explorer. Then from the Project menu, choose Add Web Reference… to open this dialog:

    Add Web Reference
  5. In the opened dialog box, type http://api.livedocx.com/ into the URL text box and click Go.

    Then choose the Current version, change the Web reference name to LiveDocx and click Add Reference.

  6. Add the using statement for the System.Net and System.IO assembly to the Form1.cs file.

  7. Now insert a simple button to your form and add the following code to the button's Click event:

    try
    {
     LiveDocx.MailMerge mailMerge1 = new LiveDocx.MailMerge();
     CookieContainer cookieJar = new CookieContainer();
     mailMerge1.CookieContainer = cookieJar;
    
     mailMerge1.LogIn("username", "password");
     mailMerge1.SetRemoteTemplate("invoice_template.docx");
    
     string[][] mergeData = new string[][] {
       new string[] { "phone", "date", "customer_number", "invoice_number",
                      "account_number", "month", "total_net", "tax",
                      "tax_value", "total"},
       new string[] { "0421 3359 129", DateTime.Now.ToString(), "11", "3",
                      "#55", "November", "€ 100.0", "19",
                      "€ 19.0", "€ 119.0" } };
    
     string[][] mergeBlockData = new string[][] {
       new string[] { "connection_number", "connection_duration", "fee" },
       new string[] { "7777", "3:15", "€ 10.0" },
       new string[] { "6666", "23:15", "€ 20.0" },
       new string[] { "5555", "13:15", "€ 30.0" },
       new string[] { "8888", "83:15", "€ 40.0" } };
    
     mailMerge1.SetFieldValues(mergeData);
     mailMerge1.SetBlockFieldValues("connection", mergeBlockData);
    
     mailMerge1.CreateDocument();
     
     string ext = "pdf";
     byte[] doc =
       Convert.FromBase64String(mailMerge1.RetrieveDocument(ext));
    
     string filename = Path.Combine(Path.GetTempPath(),
       string.Format("LiveDocxSample_{0}.{1}",
       Guid.NewGuid().ToString(), ext));
     
     File.WriteAllBytes(filename, doc);
    }
    catch (Exception exc)
    {
     Console.WriteLine(exc.Message);
    }

    Please use the credentials you've created in step 1 as the arguments for the LogIn method.

  8. Press F5 to compile and start the application and click on the button to create your first document.

Instead of building the project yourself following the above steps, you can just download a slightly more advanced version here. Please note that this project differs from the above in two places:

  1. You enter your credentials into the app.config file instead of inserting them as the arguments of the LogIn method.
  2. The template is included in the project as a resource and set by the SetLocalTemplate method. So there is no need for you to manually upload the template to the LiveDocx server.

Download Visual Studio 2008 project: livedocx_sample_dotnet.zip

LiveDocx Documentation