Word, docx4j and content control text formatting

2019-08-03 09:26发布

问题:

I'm using docx4j to create Word documents using a Word template. The template contents control which in the document are filled with text by my Java code. The problem is that the formatting I have added to some of the controls has no effect. I have tried formatting with both text content controls and rich text content controls. In fact, the entire document appears grey (including the image in the document header) so I'm not sure that the problem is specific to docx4j. Here is my code:

private void replaceTextValue(WordprocessingMLPackage template, String name, String placeholder ) throws Exception{     


    List<Object> texts = getAllSdtElementFromObject(template.getMainDocumentPart());

    for (Object text : texts) {         

        SdtElement textElement = (SdtElement) text; // SdtElement is an Interface, not a Class

        List<Object> cList = textElement.getSdtContent().getContent();

        SdtPr pr = textElement.getSdtPr();
        List<Object> al = pr.getRPrOrAliasOrLock();

        for (Object alias : al) {   // go through all SdtPr objects

            if ( alias.getClass().toString().contains("org.docx4j.wml.Tag")) {

                String CTagVal = ((org.docx4j.wml.Tag) alias).getVal();

                if (CTagVal.equalsIgnoreCase(placeholder))  {

                ClassFinder finder = new ClassFinder(Text.class); 
                new TraversalUtil(cList, finder);

                // taken from the TraveseFind example
                // https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/TraverseFind.java
                for (Object o : finder.results) {
                    Object o2 = XmlUtils.unwrap(o);
                    if (o2 instanceof org.docx4j.wml.Text) {
                    org.docx4j.wml.Text txt = (org.docx4j.wml.Text)o2;
                    txt.setValue(name);
                    } else {
                    System.out.println( XmlUtils.marshaltoString(o, true, true));
                    }
                    }

                }

        }           

    }
}
}

Here is the XML of a Content Control

<w:sdt>
<w:sdtPr>
<w:alias w:val="Aufgabengebiet"/>
<w:id w:val="-996718060"/>
<w:placeholder>
<w:docPart w:val="DefaultPlaceholder_1082065158"/>
</w:placeholder>
<w:showingPlcHdr/>
<w:text/>
</w:sdtPr>
<w:sdtContent>
<w:p w:rsidRDefault="00A858B9" w:rsidR="00066661" w:rsidP="00A858B9">
<w:r w:rsidRPr="00FD7E66">
<w:rPr>
<w:rStyle w:val="Platzhaltertext"/>
</w:rPr>
<w:t>Klicken Sie hier, um Text einzugeben.</w:t>
</w:r>
</w:p>
</w:sdtContent>
</w:sdt>

回答1:

A plain text content control can be formatted using run (rPr) formatting; see http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/rPr_5.html

For example, Word emits:

    <w:sdt>
  <w:sdtPr>
    <w:rPr>
      <w:rStyle w:val="IntenseEmphasis"/>
    </w:rPr>
    <w:id w:val="-2141179504"/>
    <w:placeholder>
      <w:docPart w:val="DefaultPlaceholder_1082065158"/>
    </w:placeholder>
    <w:text/>
  </w:sdtPr>
  <w:sdtContent>
    <w:p >
      <w:pPr>
        <w:rPr>
          <w:rStyle w:val="IntenseEmphasis"/>
        </w:rPr>
      </w:pPr>
      <w:r w:rsidRPr="00B61E2E">
        <w:rPr>
          <w:rStyle w:val="IntenseEmphasis"/>
        </w:rPr>
        <w:t>Klicken Sie hier, um Text einzugeben.</w:t>
      </w:r>
    </w:p>
  </w:sdtContent>
</w:sdt>

A rich text control's contents can also include pPr formatting (on any P's it is allowed to contain given its context).