if you think this library has been useful, feel free to give any help. Don't be shy.

Click here to lend your support to: Java2word, coding for fun! and make a donation at !


project moved to:


  1. hi Leonardo,

    I am new to Java, and I am trying to generate word document using an exsiting template. Your blog has been very useful, thanks. I tried your code, however, I can only get the xml print out, but have no idea how to gerate the word document based on the xml file. If you could give a hint on this, I would be really appreciate this. Thanks in advance for your time.

  2. Hi Leonardo,

    Is there anyway using java2word to process one or more existing tables in a template?
    What I would like to do is to give my end users complete control over the styling of the template (as long as they don't modify place holders) and would like to create just the table structure (header row and a single data row) and then have java2word add the rows that will contain the data.

    1. Hi Ian,

      What you want is possible. However one of the pre-requirements for templates is:

      Save it as "Microsoft Word 2003 - XML" (on the top of this page)

      If the user saves as different format such as .doc (real doc which is a zipped file with a few required directories) the java2word template engine won't understand it.

  3. Hi ,
    Thanks for all the good work Leonardo,
    I am haning the n no. of templates and I want to select few of them and create a document , Can you suggest some way

  4. I have done this by some way , Just curious to know is there any specific handling for special characters e.g. > or "


  5. Hi Leonardo ,
    I am having a template of word 2003 and some of sections contains the table ,

    I am having a requirement to remove those tables conditionally ,
    But I am not able to replace the text from the table i.e. the result output contains the same table and the text. Is the table text getting stored seperately in the xml ?
    . e.g.
    Sometimes I do not want the section1 text and need to remove completely

    I am doing something like...

    String xmlSubString = xmlTemplate.substring(xmlTemplate.indexOf("secion1"),xmlTemplate.indexOf("section2"));
    xmlString = xmlString.replaceAll(xmlSubString,"");

    Do you have any idea...

  6. The thing is I do not want to create a new table , instead I want to use it from the template itself , Also can you include a constructior for your document class which can take up the existing template as argument, and then by providing the getter methods to render the tables and modify them ..


  7. How save the new document?
    "TestUtils.createLocalDoc(xmlTemplate);" gime ve a lot of exceptions, what I need to include in my project to save the new document?


  8. Sorry, Solved...:
    String tmpDocs = "C://";

    File fileObj = new File((new StringBuilder()).append(tmpDocs)
    PrintWriter writer = null;
    try {
    writer = new PrintWriter(fileObj);
    } catch (FileNotFoundException e) {


    But my new .xml/doc is always corrupt :-(

    1. Hi Maneta, i'm having the same problem that you had at that moment .... how do you resolve it? i think the code above is missing something .... how do you generate the value for "myDoc" variable ...? thanx in advance!

  9. Hi Leonardo, when I use a template, and, for example, I put a:


    to replace a key, the word "Section" appear without heading1 format. I've noticed that this only occur using templates, not when I create a new Document2004(). Do you know any workaround for this case?

    1. For any that find this problem:

      The "heading2" style (or any other heading style) is generated by the API in:


      On the other way, my Office isn't in english language (actually it is in portuguese)! So, I need to replace the string "Heading2" for "Ttulo2", for example:


      This workaround solved my problem!

  10. Very nice example !
    But it seems that in the first column of the table header, there is always some whitespace in it. how to correct this problem ?
    Another question is it possible to remove the table border ?

  11. Greetings!
    I'd like to use the "generate doc from template" approach ... but ... can i add images before/after the template text? My images are dynamically generated, so they can't be put together with the template text.

    Thanks in advance, and congratulations for your GR8 library!

  12. Hi Leonardo,
    How can I contact you directly ?

  13. Hi Leonardo,

    Is there anyway using java2word to customize table, to style the table
    for ex- table width, coloumn width row height e.t.c.