DOCX4J Insert a line break

女生的网名这么多〃 提交于 2019-12-07 02:45:27

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.

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