Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could use the <a href="https://github.com/plutext/docx4j" rel="nofollow">docx4j</a> library. Here is the documentation for <a href="http://htmlpreview.github.io/?https://github.com/plutext/docx4j/blob/master/docs/Docx4j_GettingStarted.html" rel="nofollow">Getting started</a>.</p> <p>I recommend that you create a template file first, with all the formatting, and as much structure as you can. Then you should put in content-controls where you want your text to go. The content controls could contain other content, that you could use to repeat some common structure.</p> <p>In Word, the <em>Content Controls</em> can be found in the <em>Developer tab</em> on the main ribbon. They are named <em>Rich Text Content Control</em> and <em>Plain Text Content Control</em>. You might need to enable the Developer Tab first though. In the options, under <em>Customize ribbon</em>, check <em>Developer</em>.</p> <p>To change the tag of a Content Control, click its handle on the page to select it, and then press the <em>Control Properties</em> button on the ribbon. You will then get a dialog where you can set the title and the tag.</p> <p>In Java, the content controls will be represented in the object model as <a href="http://www.docx4java.org/docx4j/javadoc-2.6.0/index.html?org/docx4j/wml/SdtBlock.html" rel="nofollow"><code>SdtBlock</code></a> or <a href="http://www.docx4java.org/docx4j/javadoc-2.6.0/index.html?org/docx4j/wml/SdtRun.html" rel="nofollow"><code>SdtRun</code></a>. When you are processing the template, you should replace those with the content you want.</p> <p>The <a href="http://www.docx4java.org/docx4j/javadoc-2.6.0/index.html?org/docx4j/math/package-summary.html" rel="nofollow"><code>org.docx4j.math</code></a> package contains the classes for creating math formulas.</p> <hr> <p>Here is an example:</p> <pre><code>import java.*; import java.io.*; import java.util.*; import javax.xml.bind.*; import javax.xml.namespace.*; import org.docx4j.wml.*; import org.docx4j.math.*; import org.docx4j.openpackaging.packages.*; import org.docx4j.openpackaging.parts.WordprocessingML.*; public class Processor { public static void main(String[] args) throws Exception { File inFile = new File(args[0]); File outFile = new File(args[1]); WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inFile); MainDocumentPart mdp = wordMLPackage.getMainDocumentPart(); Processor processor = new Processor(); processor.processContent(mdp.getContent()); wordMLPackage.save(outFile); } private Stack&lt;String&gt; tags = new Stack&lt;String&gt;(); private void pushTag(String tag) { tags.push(tag); } private String getTag() { return tags.peek(); } private void popTag() { tags.pop(); } private static final org.docx4j.wml.ObjectFactory wmlFactory = new org.docx4j.wml.ObjectFactory(); private static final org.docx4j.math.ObjectFactory mathFactory = new org.docx4j.math.ObjectFactory(); public void processContent(List&lt;Object&gt; content) { for (Object child : content) { if (child instanceof SdtBlock) { processBlock((SdtBlock) child); } else if (child instanceof P) { processP((P) child); } else if (child instanceof JAXBElement) { JAXBElement&lt;?&gt; elem = (JAXBElement&lt;?&gt;) child; Class&lt;?&gt; elemType = elem.getDeclaredType(); if (elemType.equals(CTOMath.class)) { processOMath((CTOMath) elem.getValue()); } } } } public void processP(P p) { processContent(p.getContent()); } public void processBlock(SdtBlock block) { String tag = block.getSdtPr().getTag().getVal(); pushTag(tag); processContent(block.getSdtContent().getContent()); popTag(); } public void processOMath(CTOMath oMath) { String tag = getTag(); // tag of innermost &lt;w:sdt&gt; if (getTag().equals("MyEquation")) { List&lt;Object&gt; content = oMath.getEGOMathElements(); content.clear(); content.add(makeRun("A=\u03c0")); content.add(makeSSup(makeRun("r"), makeRun("2"))); } } private JAXBElement&lt;CTR&gt; makeRun(String text) { // &lt;m:r&gt; CTR run = mathFactory.createCTR(); List&lt;Object&gt; content = run.getContent(); // &lt;w:rPr&gt;&lt;w:rFonts&gt; RPr pr = wmlFactory.createRPr(); RFonts rFonts = wmlFactory.createRFonts(); rFonts.setAscii("Cambria Math"); rFonts.setHAnsi("Cambria Math"); pr.setRFonts(rFonts); content.add(wmlFactory.createSdtPrRPr(pr)); // &lt;m:t&gt; CTText ctText = mathFactory.createCTText(); ctText.setValue(text); content.add(mathFactory.createCTRTMath(ctText)); return mathFactory.createCTOMathArgR(run); } private JAXBElement&lt;CTSSup&gt; makeSSup(Object expr, Object exp) { // &lt;m:ssup&gt; CTSSup ssup = mathFactory.createCTSSup(); // &lt;m:e&gt; CTOMathArg eArg = mathFactory.createCTOMathArg(); eArg.getEGOMathElements().add(expr); ssup.setE(eArg); // &lt;m:sup&gt; CTOMathArg supArg = mathFactory.createCTOMathArg(); supArg.getEGOMathElements().add(exp); ssup.setSup(supArg); return mathFactory.createCTOMathArgSSup(ssup); } } </code></pre> <p>It will look for block-level content-controls named "MyEquation", and replace the math-expressions in them with <code>A=&pi;r<sup>2</sup></code>.</p> <p>Specifically, it will look for</p> <pre class="lang-xml prettyprint-override"><code>&lt;w:sdt&gt; &lt;w:sdtPr&gt; &lt;w:tag w:val="MyEquation"/&gt; &lt;/w:sdtPr&gt; &lt;w:sdtContent&gt; &lt;w:p&gt; &lt;m:oMath&gt; &lt;/m:oMath&gt; &lt;/w:p&gt; &lt;/w:sdtContent&gt; &lt;/w:sdt&gt; </code></pre> <p>and replace it with</p> <pre class="lang-xml prettyprint-override"><code>&lt;w:p&gt; &lt;m:oMath&gt; &lt;m:r&gt; &lt;w:rPr&gt; &lt;w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/&gt; &lt;/w:rPr&gt; &lt;m:t&gt;A=π&lt;/m:t&gt; &lt;/m:r&gt; &lt;m:sSup&gt; &lt;m:sSupPr&gt; &lt;m:ctrlPr&gt; &lt;w:rPr&gt; &lt;w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/&gt; &lt;/w:rPr&gt; &lt;/m:ctrlPr&gt; &lt;/m:sSupPr&gt; &lt;m:e&gt; &lt;m:r&gt; &lt;w:rPr&gt; &lt;w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/&gt; &lt;/w:rPr&gt; &lt;m:t&gt;r&lt;/m:t&gt; &lt;/m:r&gt; &lt;/m:e&gt; &lt;m:sup&gt; &lt;m:r&gt; &lt;w:rPr&gt; &lt;w:rFonts w:ascii="Cambria Math" w:hAnsi="Cambria Math"/&gt; &lt;/w:rPr&gt; &lt;m:t&gt;2&lt;/m:t&gt; &lt;/m:r&gt; &lt;/m:sup&gt; &lt;/m:sSup&gt; &lt;/m:oMath&gt; &lt;/w:p&gt; </code></pre> <hr> <p>You can make the equation in Word, and look inside the docx-file. They are stored as zip-files, containing a lot of XML. Specifically, you want to look in the <code>word/document.xml</code> file.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload