Search code examples
javaxmldocxdocx4j

Whats the format for a new content container in the .docx for docx4j when we want to add xml data into the .docx (Template) and get a new .docx file?


For the doc file used in this class

public class ContentControlBindingExtensions {

public static JAXBContext context = org.docx4j.jaxb.Context.jc; 

static String filepathprefix;

/**
 * @param args
 */
public static void main(String[] args) throws Exception {

//      String input_DOCX = System.getProperty("user.dir") + "/sample-docs/word/databinding/CountryRegions.xml";

    String input_DOCX = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice.docx";
    String input_XML = System.getProperty("user.dir") + "/sample-docs/word/databinding/invoice-data.xml";


    // resulting docx
    String OUTPUT_DOCX = System.getProperty("user.dir") + "/OUT_ContentControlsMergeXML.docx";

    // Load input_template.docx
    WordprocessingMLPackage wordMLPackage = Docx4J.load(new File(input_DOCX));

    // Open the xml stream
    FileInputStream xmlStream = new FileInputStream(new File(input_XML));
Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_INSERT_XML | Docx4J.FLAG_BIND_BIND_XML);

    //Save the document 
    Docx4J.save(wordMLPackage, new File(OUTPUT_DOCX), Docx4J.FLAG_NONE);
    System.out.println("Saved: " + OUTPUT_DOCX);
    }   
}

I downloaded the invoice.docx used in this file. I tried adding a new content container with Text as "randomText" and Tag as "od:condition=c7" as the previous content container had Tag as "od:condition=c6" and tried to hide the container via XML. But I couldn't do it. Can I know the format or how do I create a container and access it via XML.


Solution

  • For how it works under the covers, see the od:condition tag section in opendope_conventions_v2.3.html

    To author a suitable template, it is easiest to use one of the OpenDoPE authoring addins for Word. That takes care of setting up the data structures for you.