2

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>
4

1 回答 1

1

可以使用运行 (rPr) 格式化来格式化纯文本内容控件;见http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/rPr_5.html

例如,Word 发出:

    <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>

富文本控件的内容还可以包括 pPr 格式(在给定上下文的情况下允许包含的任何 P)。

于 2015-02-16T09:36:59.633 回答