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

How to add space or tab to format text in Paragraph docx4j

我需要在段落中添加space或制表符,我不能使用table,因为在左侧我需要添加一个table,所以它会变成嵌套 table.

我试试

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

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

docx4j webapp or Helper Word AddIn会为您解答。

这是我生成的代码;对于额外的 space(强制它使用单独的 运行):

    <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 中嵌套 table,如果您愿意的话:-)