DOCX4J Insert a line break

五迷三道 提交于 2019-12-23 02:42:16

问题


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

This is the XML for the variable:

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

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

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.

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>

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


回答1:


There are two ways to do this.

The first is to use tabs:

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

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"); 

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>

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

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.



来源:https://stackoverflow.com/questions/44135393/docx4j-insert-a-line-break

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!