简体   繁体   English

Word,docx4j和内容控件文本格式

[英]Word, docx4j and content control text formatting

I'm using docx4j to create Word documents using a Word template. 我正在使用docx4j使用Word模板创建Word文档。 The template contents control which in the document are filled with text by my Java code. 模板内容控制我的Java代码用文本填充文档中的哪些内容。 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. 实际上,整个文档显示为灰色(包括文档标题中的图像),因此我不确定问题是否特定于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 这是内容控件的XML

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

A plain text content control can be formatted using run (rPr) formatting; 可以使用运行(rPr)格式设置纯文本内容控件的格式。 see http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/rPr_5.html 参见http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/rPr_5.html

For example, Word emits: 例如,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>

A rich text control's contents can also include pPr formatting (on any P's it is allowed to contain given its context). 富文本控件的内容还可以包括pPr格式(在给定的上下文中,可以包含任何P)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM