title: Docx4j - How to replace placeholder with value
body: <p>I've been trying to work through the examples <a href="https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/FieldsMailMerge.java" rel="noreferrer">FieldMailMerge</a> and <a href="https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/VariableReplace.java" rel="noreferrer">VariableReplace</a>  but can't seem to get a local test case running. I'm basically trying to start with one docx template document and have it create x docx documents from that one template with the variables replaced.</p>
  
  <p>In the code below <code>docx4jReplaceSimpleTest()</code> tries to replace a single variable but fails to do so. The ${} values in the template files are removed as part of the processing therefore I believe it's finding them but not replacing them for some reason. I understand it could be due to formatting as explained in the comments of the sample code but for troubleshooting just to get something working I'm trying it anyways. </p>
  
  <p>In the code below <code>docx4jReplaceTwoPeopleTest()</code>, the one I want to get working, I'm trying to do it in what I believe is the proper way, but that's not finding or replacing anything. It's not even removing the ${} from the docx file.</p>
  
  <pre><code>public static void main(String[] args) throws Exception
  {
      docx4jReplaceTwoPeopleTest();
      docx4jReplaceSimpleTest();
  }
  
  private static void docx4jReplaceTwoPeopleTest() throws Exception
  {
        String docxFile = "C:/temp/template.docx";
  
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(docxFile));
  
        List&lt;Map&lt;DataFieldName, String&gt;&gt; data = new ArrayList&lt;Map&lt;DataFieldName, String&gt;&gt;();
  
        Map&lt;DataFieldName, String&gt; map1 = new HashMap&lt;DataFieldName, String&gt;();
        map1.put(new DataFieldName("Person.Firstname"), "myFirstname");
        map1.put(new DataFieldName("Person.Lastname"), "myLastname");
        data.add(map1);
  
        Map&lt;DataFieldName, String&gt; map2 = new HashMap&lt;DataFieldName, String&gt;();
        map2.put(new DataFieldName("Person.Firstname"), "myFriendsFirstname");
        map2.put(new DataFieldName("Person.Lastname"), "myFriendsLastname");
        data.add(map2);
  
        org.docx4j.model.fields.merge.MailMerger.setMERGEFIELDInOutput(OutputField.KEEP_MERGEFIELD);
  
        int x=0;
        for(Map&lt;DataFieldName, String&gt; docMapping: data) 
        {
          org.docx4j.model.fields.merge.MailMerger.performMerge(wordMLPackage, docMapping, true);
          wordMLPackage.save(new java.io.File("C:/temp/OUT__MAIL_MERGE_" + x++ + ".docx") );
        }
  }
  
  private static void docx4jReplaceSimpleTest() throws Exception
  {
        String docxFile = "C:/temp/template.docx";
  
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(docxFile));
  
        HashMap&lt;String, String&gt; mappings = new HashMap&lt;String, String&gt;();
        mappings.put("Person.Firstname", "myFirstname");
        mappings.put("Person.Lastname", "myLastname");
  
        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
        documentPart.variableReplace(mappings);
  
      wordMLPackage.save(new java.io.File("C:/temp/OUT_SIMPLE.docx") );
  }
  </code></pre>
  
  <p>The docx file consists of the following text (no formatting is done):</p>
  
  <pre><code>This is a letter to someone
  Hi ${Person.Firstname} ${Person.Lastname},
  How are you?
  Thank you again. I wish to see you soon ${Person.Firstname}
  Regards,
  Someone
  </code></pre>
  
  <p>Notice that I'm also trying to replace Person.Firstname at least twice as well. As the lastname is not even replaced I don't think this has anything to do with it but I'm adding it just in case.</p>