DOCX4J插入换行符 [英] DOCX4J Insert a line break

查看:1308
本文介绍了DOCX4J插入换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DOCX中有一个变量,我想用一个值替换。首先,该变量不是放在行的开头,而是放在一些标签之后。我的价值是一个邮政地址,我希望街道和邮政编码(+城市)在不同的行中有相同的缩进。街道替换了他的行中的变量,邮政编码在一个新的行中:

I have a variable in a DOCX that I want to replace with a value. First, that variable is not placed at the beginning of the line but after some tabs. My value is a postal address and I want to have the street and zip code (+city) in different line with the same indentation. The street replace the variable in his line, and the zip code is in a new line like that:

                            4 Privet Drive
                            Little Whinging

这是变量的XML:

<w:p>
    <w:pPr>
        <w:tabs>
            <w:tab w:val="left" w:pos="6120"/>
        </w:tabs>
        <w:ind w:firstLine="6300"/>
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
            <w:b/>
            <w:sz w:val="22"/>
            <w:szCs w:val="20"/>
        </w:rPr>
    </w:pPr>
    <w:r>
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
            <w:b/>
            <w:sz w:val="22"/>
             <w:szCs w:val="20"/>
        </w:rPr>
         <w:t>$address$</w:t>
    </w:r>
</w:p>

我可以替换变量并将邮政编码和城市放在新行中(我用 getJAXBNodesViaXPath()检索我的变量)

I can replace the variable and put the zip code and city in new line with (I use getJAXBNodesViaXPath() to retrieve my variable)

r.clear(); //r contains the variable it's <w:r> </w:r> in my XML exemple, it's a list
org.docx4j.wml.Text text = factory.createText(); //For the street
r.add(k, text);
org.docx4j.wml.Br br = factory.createBr();
r.add(k, br);
org.docx4j.wml.Text text2 = factory.createText();
r.add(k, text2);
text.setValue(zip);
text2.setValue(street);

它将varibale替换为street并使用zip创建一个新行。街道位于好的地方(左侧),但是城市位于新线的开头。

It replace the varibale with the street and create a new line with the zip. The street is at the good place (on the left) but the city it's at the beginning of the new line.

在新的DOCX中,我的XML看起来像这样: / p>

In the new DOCX my XML looks like that:

<w:p>
    <w:pPr>
        <w:tabs>
            <w:tab w:val="left" w:pos="6120"/>
        </w:tabs>
        <w:ind w:firstLine="6300"/>
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
            <w:b/>
            <w:sz w:val="22"/>
            <w:szCs w:val="20"/>
        </w:rPr>
    </w:pPr>
    <w:r>
        <w:rPr>
            <w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/>
            <w:b/>
            <w:sz w:val="22"/>
             <w:szCs w:val="20"/>
        </w:rPr>
         <w:t>4 Privet Drive</w:t>
         <:br/>
         <w:t>Little Whinging</w:t>
    </w:r>
</w:p>

所以我不知道如何将第二行设置为第一行的相同位置? / p>

So I don't know how to set the second line to the same position of the first ?

推荐答案

有两种方法可以做到这一点。

There are two ways to do this.

第一个是使用标签:

    <w:p>
        <w:r>
            <w:tab/>
            <w:t>4 Privet Drive</w:t>
            <w:br/>
            <w:tab/>
            <w:t>Little Whinging</w:t>
        </w:r>
    </w:p>

对应代码,假设P p:

Corresponding code, assuming P p:

        // Create object for r
        R r = wmlObjectFactory.createR(); 
        p.getContent().add( r); 
            // Create object for tab (wrapped in JAXBElement) 
            R.Tab rtab = wmlObjectFactory.createRTab(); 
            JAXBElement<org.docx4j.wml.R.Tab> rtabWrapped = wmlObjectFactory.createRTab(rtab); 
            r.getContent().add( rtabWrapped); 
            // Create object for t (wrapped in JAXBElement) 
            Text text = wmlObjectFactory.createText(); 
            JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRT(text); 
            r.getContent().add( textWrapped); 
                text.setValue( "4 Privet Drive"); 
            // Create object for br
            Br br = wmlObjectFactory.createBr(); 
            r.getContent().add( br); 
            // Create object for tab (wrapped in JAXBElement) 
            R.Tab rtab2 = wmlObjectFactory.createRTab(); 
            JAXBElement<org.docx4j.wml.R.Tab> rtabWrapped2 = wmlObjectFactory.createRTab(rtab2); 
            r.getContent().add( rtabWrapped2); 
            // Create object for t (wrapped in JAXBElement) 
            Text text2 = wmlObjectFactory.createText(); 
            JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRT(text2); 
            r3.getContent().add( textWrapped2); 
                text2.setValue( "Little Whinging"); 

第二种是使用w:ind / @ w:left:

The second is to use w:ind/@w:left:

    <w:p>
        <w:pPr>
            <w:ind w:left="720"/>
        </w:pPr>
        <w:r>
            <w:t>4 Privet Drive</w:t>
            <w:br/>
            <w:t>Little Whinging</w:t>
        </w:r>
    </w:p>

为此,Java的关键位是:

For that, the key bit of Java is:

            // Create object for ind
            PPrBase.Ind pprbaseind = wmlObjectFactory.createPPrBaseInd(); 
            ppr.setInd(pprbaseind); 
                pprbaseind.setLeft( BigInteger.valueOf( 720) ); 

一般来说,要自己回答这样的问题,请在Word中创建一些看起来正确的内容,然后检查XML。您可以使用Docx4j Word Helper AddIn或Docx4j Webapp生成相应的代码。

In general, to answer questions like this for yourself, create something which looks right in Word, then inspect the XML. You can use the Docx4j Word Helper AddIn, or the Docx4j Webapp, to generate corresponding code.

这篇关于DOCX4J插入换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆