Search code examples
javadocx4j

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


I need to add space or tab into a paragraph, I can't use table because on the left I need to add a table, so it will became a nested table.

I try

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

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

Solution

  • The docx4j webapp or Helper Word AddIn will answer this for you.

    Here is the code I generated; for extra space (forcing it to use a separate run):

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

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

    Using tabs, 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>
    

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

    You don't actually need all the JAXBElements the generated code inserts in this case, so you can clean that up a bit if you want.

    And by the way, as an aside, the docx format does allow you to nest a table in a tc if you want :-)