title: Create Word (.docx) Documents Programatically With docx4j
body: <p>i want to Load  a template word document and  add content to it and save it as new document. here is what i found :</p>
  
  <pre><code>private static WordprocessingMLPackage getTemplate(String name) throws Docx4JException, FileNotFoundException {
    WordprocessingMLPackage template = WordprocessingMLPackage.load(new java.io.File(name));
    return template;
  }
  
  private static List&lt;Object&gt; getAllElementFromObject(Object obj, Class&lt;?&gt; toSearch) {
    List&lt;Object&gt; result = new ArrayList&lt;Object&gt;();
    if (obj instanceof JAXBElement) obj = ((JAXBElement&lt;?&gt;) obj).getValue();
    if (obj.getClass().equals(toSearch))
      result.add(obj);
    else if (obj instanceof ContentAccessor) {
      List&lt;?&gt; children = ((ContentAccessor) obj).getContent();
      for (Object child : children) {
        result.addAll(getAllElementFromObject(child, toSearch));
      }
    }
    return result;
  }
  
  private static void replacePlaceholder(WordprocessingMLPackage template, String name, String placeholder) {
    List&lt;Object&gt; texts = getAllElementFromObject(template.getMainDocumentPart(), Text.class);
    for (Object text : texts) {
      Text textElement = (Text) text;
      if (textElement.getValue().equals(placeholder)) {
        textElement.setValue(name);
      }
    }
  }
  
  private void writeDocxToStream(WordprocessingMLPackage template, String target) throws IOException, Docx4JException {
    File f = new File(target);
    template.save(f);
  }
  </code></pre>
  
  <p>i created i sample.docx file in the drive D, and then i called the main method :</p>
  
  <pre><code>public static void main(String[] args) throws Docx4JException, Exception  {
    WordprocessingMLPackage template = getTemplate("D:\\sample.docx");
    replacePlaceholder (template,"fayza", "nom");
  }
  </code></pre>
  
  <p>unfortunately this is not working , it guess the template cant load , i tried but still not working, any help please </p>