Page 1 of 1

Help creating tabs and newlines using addParagraph()

PostPosted: Thu Jul 12, 2012 7:39 pm
by gat89
Hi I just started using docx4java and I am incredibly impressed. I had a quick question I was hoping someone could help answer. I am trying to add new lines and tabs into an existing Word Document but I am having some problems. Here is my code:

wordMLPackage.getMainDocumentPart().addParagraphOfText("\t\t\t\t\tTesting Tabs");
wordMLPackage.getMainDocumentPart().addParagraphOfText("Testing \n\n newlines");

The problem is when I open my Word doc it doesn't seem to recognize this. Can someone please help? Thanks!

Re: Help creating tabs and newlines using addParagraph()

PostPosted: Fri Jul 13, 2012 9:57 am
by gat89
Please can someone help answer this question? I've been trying to figure this out all night =( I'm sure it's a simple answer right?

Re: Help creating tabs and newlines using addParagraph()

PostPosted: Sat Jul 14, 2012 1:33 am
by gat89
Okay I am desperate here. If I can't figure out how to use this package I don't think it will work for me. I will paypal $5 to the person who can first help answer my question!!

Re: Help creating tabs and newlines using addParagraph()

PostPosted: Sat Jul 14, 2012 11:02 pm
by jason
Have you had a look at the Getting Started guide?

The general advice is to create a docx containing what you want (preferably in Word, but you could also do it in a recent version of OpenOffice/LibreOffice), and then have a look at it.

When I do that with the things you are asking about, I get:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
    <w:p>
      <w:r>
        <w:tab/>
        <w:t>that was a tab</w:t>
      </w:r>
      <w:r>
        <w:br/>
        <w:t>and that was a new line</w:t>
      </w:r>
    </w:p>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


You should be able to have all that in the one w:r, so here it is on a plate (albeit untested):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
                org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
                org.docx4j.wml.P  p = factory.createP();
               
                org.docx4j.wml.R  run = factory.createR();
                p.getContent().add(run);
               
                run.getContent().add(factory.createRTab() );           
               
                org.docx4j.wml.Text  text1 = factory.createText();
                t.setValue("that was a tab");
               
                run.getContent().add(text1 );          
               
                run.getContent().add(factory.createBr() );
               
                org.docx4j.wml.Text  text2 = factory.createText();
                t.setValue("and that was a new line");
               
                // Check you got what you expected
                System.out.println(XmlUtils.marshaltoString(p, true));

 
Parsed in 0.016 seconds, using GeSHi 1.0.8.4

Re: Help creating tabs and newlines using addParagraph()

PostPosted: Tue Jul 17, 2012 9:19 am
by conphident4
Hey,
not sure u found the solution... yea it is quite simple but when getting started makes u feel lost.. basically as Jason said - u have to understand the various components of the word doc & then it will be a piece of cake.

here is the code for what you are looking for:

Code: Select all
public static void main(String args[]) throws Docx4JException {
      
      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
      
      ObjectFactory obj = new ObjectFactory();
       
      // paragraph element / object
      P para = obj.createP();
       
      // run object - number of runs may comprise a single paragraph element
      R run = obj.createR();
       
      //run comprises of various elements such as text, format, tab etc...
      // tab element
      R.Tab tab = obj.createRTab();
        // Text element
        Text text = obj.createText();
       
        // set text value
        text.setValue("testing tabs");
       
        // to add tab to a run
        run.getContent().add(tab);
        run.getContent().add(tab);
        run.getContent().add(tab);
       
        // add text to run
        run.getContent().add(text);
        para.getContent().add(run);
        //add the paragraph object to the main document part
        wordMLPackage.getMainDocumentPart().addObject(para);
       
        text = new Text();
        run = new R();
        para = new P();
        text.setValue("testing ");
        // to preserve the space in an xml
        text.setSpace("preserve");
        run.getContent().add(text);
        para.getContent().add(run);
        wordMLPackage.getMainDocumentPart().addObject(para);
       
        //adds a new line
        wordMLPackage.getMainDocumentPart().addParagraphOfText("");
        //adds a new line & the text in it
        wordMLPackage.getMainDocumentPart().addParagraphOfText("newlines");

        wordMLPackage.save(new File("C:\\docs\\testingTabs.docx"));
       
       
   }