title: Content Control in PHP
body: <p>I am trying to find an api or php technique that can read a docx document, remove some MS Content Control Values, and then recreate the same docx document while preserving all MSWord content and styling elements.</p>
  
  <p>You might be wondering how this could be useful.  For example, you could set a question in MSWord like so: </p>
  
  <pre><code>The sky is blue in colour.
  </code></pre>
  
  <p>Then, at the other end when viewing the processed file in MSWord, a user sees only:</p>
  
  <pre><code>The Sky is      in colour.
  </code></pre>
  
  <p>On one hand, you can extract the data "blue" from the Content Control elements into your db. On the other hand, you can let other end users view the processed file in MSWord without revealing the "answer" blue.  How cool is that.</p>
  
  <p>The idea I have in mind is generally like so:</p>
  
  <p><strong>1. Open an MSWord-created docx file (MSWord 2007 and later).  The file contains some usual MSWord text and styling elements along with custom Content Control elements (that you created in developer's MSWord/Visual Studio) and looks like (pseudo) :</strong></p>
  
  <blockquote>
    <p><code>... some standard MS styles or runs ...</code><br>
    ...
    <code>&lt;SdtRun&gt; The sky is &lt;/SdtRun&gt;</code><br>
    <code>&lt;Sdt Content Control attribute: colour&gt;</code><br>
    <code>&lt;Sdt Content Control value&gt; blue** &lt;/Sdt Content Control value&gt;</code><br>
    <code>&lt;/Sdt Content Control attribute&gt;</code><br>
    <code>&lt;SdtRun&gt; in colour.&gt;&lt;/SdtRun&gt;</code><br>
    ...</p>
  </blockquote>
  
  <p><strong>2. Remove the <em>value</em> inside the content control element while preserving the whitespace that the value contains.</strong></p>
  
  <p><strong>3. Generate a new MSword document without the value of the specified content control element.  All the usual MSWord text and styling elements are preserved.  Alternatively, the newly generated docx can do without the original Content Control tags and whitespaces preserved in static text form.</strong></p>
  
  <p><strong>4. Achieve the above without having to apply Content Control binding techniques</strong> <em>(there are situations where you would rather not bind Content Control elements but that is another topic all by itself)</em>.</p>
  
  <p>The challenge is to create a new MSWord docx and preserve all standard MSWord content (text,style etc) without using any pre-defined templates.  I have looked around quite abit for apis but can't seem to find any that can both read and write MSWord docx documents.  I can only identify a few that can write to pre-made templates (e.g. phpdocx).</p>
  
  <p>I am not even sure if the above approach is suitable but the gist of it is really to remove Content Control Values from a docx document without destroying the original document's content, styling and layout.  I guess the solution could possibly involve
  <br>(i)targeting document.xml itself though a XML api or
  <br>(ii)targeting the native docx though a docx api, or
  <br>(iii)any other techniques you might like to share.</p>
  
  <p>Are there any one who can share some pointers?</p>
  
  <p>=======added details on 23 Feb 13========</p>
  
  <p>I would like to share a little more so as to give context to the original question.  The gist of what I am trying to achieve is to create a way for people to upload data into a db through an editable MSWord template.</p>
  
  <p>An example of such use could be in a marketing research firm wanting to create online survey forms in MSword on-the-fly, uploading these forms, and immediately launching these survey forms online, all by just uploading an MSWord Doc via a webpage.  On the docx itself, some people may desire the flexibility to copy-and-paste data-field placeholders(i.e. Content Control tags) across the same form, and not be significantly limited by a fixed-length template.  </p>
  
  <p>Data-binding would have been ideal for a fixed-length fixed-layout scenario.  However in this case, end-users would not be able to easily edit the templates because copying-n-pasting data-binded tags would also mean duplicating the same values across the nth copy-and-pasted tags (MSword automatically does that).  I tried another approach where I directly parse the values from document.xml rather than from customXml and was able to extract the sdt nodes from document.xml and reconstruct the desired data structures by simply assigning meanings to certain content control tags and identifying them by node attributes.</p>
  
  <p>I was even able to parse images by way of "blip" nodes, which I've not yet found a way to do so from custom.XML.  In this case, the users can duplicate content control tags, including image tags, without limitations and still be able to capture values from these tags.  </p>
  
  <p>Lastly, some might desire the ability to receive a returned "public" version of the original docx they've uploaded, stripped of certain "default" content control values.  Hence, the above question was raised.   I'll study deeper into the various apis like phpWord.  Meanwhile, I hope to hear more from you.</p>