如何在段落docx4j中添加空格或制表符以格式化文本

时间:2019-03-29 16:00:20

标签: java docx4j

我需要在段落中添加空格或制表符,不能使用表格,因为在左侧需要添加表格,因此它将变成嵌套表格。

我尝试

//Paragraph without spaces or tabs
//Paragraph       with spaces or tabs

P paragraph = factory.createP();
paragraph.getContent().add(factory.createTabs());
paragraph.getContent().add(factory.createRTab());

1 个答案:

答案 0 :(得分:1)

docx4j webappHelper Word AddIn将为您回答。

这是我生成的代码;以获得额外的空间(强制其使用单独的运行):

    <w:p>
        <w:r>
            <w:t>Paragraph</w:t>
        </w:r>
        <w:r>
            <w:t xml:space="preserve">      </w:t>
        </w:r>
        <w:r>
            <w:t>that was some space.</w:t>
        </w:r>
    </w:p>

假设P p:

       // Create object for r
        R r = wmlObjectFactory.createR(); 
        p.getContent().add( r); 
            // 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( "Paragraph"); 
        // Create object for r
        R r3 = wmlObjectFactory.createR(); 
        p.getContent().add( r3); 
            // Create object for t (wrapped in JAXBElement) 
            Text text3 = wmlObjectFactory.createText(); 
            JAXBElement<org.docx4j.wml.Text> textWrapped3 = wmlObjectFactory.createRT(text3); 
            r3.getContent().add( textWrapped3); 
                text3.setValue( "      "); 
                text3.setSpace( "preserve"); 
        // Create object for r
        R r5 = wmlObjectFactory.createR(); 
        p.getContent().add( r5); 
            // Create object for t (wrapped in JAXBElement) 
            Text text5 = wmlObjectFactory.createText(); 
            JAXBElement<org.docx4j.wml.Text> textWrapped5 = wmlObjectFactory.createRT(text5); 
            r5.getContent().add( textWrapped5); 
                text5.setValue( "that was some space."); 

使用标签,XML:

    <w:p>
        <w:r>
            <w:t>Paragraph</w:t>
        </w:r>
        <w:r>
            <w:tab/>
            <w:t>that was a tab</w:t>
        </w:r>
    </w:p>

假设P p:

        // Create object for r
        R r = wmlObjectFactory.createR(); 
        p.getContent().add( r); 
            // 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( "Paragraph"); 
        // Create object for r
        R r2 = wmlObjectFactory.createR(); 
        p.getContent().add( r2); 
            // Create object for tab (wrapped in JAXBElement) 
            R.Tab rtab = wmlObjectFactory.createRTab(); 
            JAXBElement<org.docx4j.wml.R.Tab> rtabWrapped = wmlObjectFactory.createRTab(rtab); 
            r2.getContent().add( rtabWrapped); 
            // Create object for t (wrapped in JAXBElement) 
            Text text2 = wmlObjectFactory.createText(); 
            JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRT(text2); 
            r2.getContent().add( textWrapped2); 
                text2.setValue( "that was a tab");

在这种情况下,您实际上并不需要生成的代码插入的所有JAXBElement,因此您可以根据需要进行一些清理。

顺便说一句,顺便说一句,docx格式的确允许您在tc中嵌套表:-)