以编程方式设置footnoteReference运行的样式

时间:2018-01-15 13:53:39

标签: openxml docx4j footnotes

我编写了一个带有docx4j的生成器,它将专有数据模型作为输入,并生成一个docx文件作为输出。

我尝试按照以下示例添加脚注(和脚注引用):https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/FootnoteAdd.java

但是,我没有看到如何在代表footnoteReference的运行中添加样式。我创建它就像在示例中一样:

CTFtnEdnRef ftnednref = wmlObjectFactory.createCTFtnEdnRef(); 
JAXBElement<org.docx4j.wml.CTFtnEdnRef> ftnednrefWrapped = wmlObjectFactory.createRFootnoteReference(ftnednref); 
r.getContent().add( ftnednrefWrapped); 
ftnednref.setId( BigInteger.valueOf( i) );

如何将样式信息添加到ftnednref?最终,这个“run”在我的结果中是一个正常的Run,但它没有RStyle FootnoteReference,这意味着它不在我的文档的上标中。我认为这种风格应该自动应用,但事实并非如此。我的结果如下:

<w:r>
  <w:rPr>
    <w:highlight w:val="darkCyan"/>
  </w:rPr>
  <w:footnoteReference w:id="2"/>
</w:r>

它重复使用前面运行的rPr。如何确保此footnoteReference-Run具有正确的样式?

1 个答案:

答案 0 :(得分:0)

如果在Word中创建示例文档,则可以使用docx4j webapp或Helper Word AddIn生成相应的Java代码。

我是这样的,我得到了:

        <w:r>
            <w:rPr>
                <w:rStyle w:val="FootnoteReference"/>
                <w:lang w:val="en-AU"/>
            </w:rPr>
            <w:footnoteReference w:id="1"/>
        </w:r>

        // Create object for r
        R r2 = wmlObjectFactory.createR(); 
        p.getContent().add( r2); 
            // Create object for rPr
            RPr rpr2 = wmlObjectFactory.createRPr(); 
            r2.setRPr(rpr2); 
                // Create object for rStyle
                RStyle rstyle = wmlObjectFactory.createRStyle(); 
                rpr2.setRStyle(rstyle); 
                    rstyle.setVal( "FootnoteReference"); 
                // Create object for lang
                CTLanguage language2 = wmlObjectFactory.createCTLanguage(); 
                rpr2.setLang(language2); 
                    language2.setVal( "en-AU"); 
            // Create object for footnoteReference (wrapped in JAXBElement) 
            CTFtnEdnRef ftnednref = wmlObjectFactory.createCTFtnEdnRef(); 
            JAXBElement<org.docx4j.wml.CTFtnEdnRef> ftnednrefWrapped = wmlObjectFactory.createRFootnoteReference(ftnednref); 
            r2.getContent().add( ftnednrefWrapped); 
                ftnednref.setId( BigInteger.valueOf( 1) ); 

所以你需要的是:

            // Create object for rStyle
            RStyle rstyle = wmlObjectFactory.createRStyle(); 
            rpr.setRStyle(rstyle); 
                rstyle.setVal( "FootnoteReference");

您还希望在样式部分中定义FootnoteReference样式。