How to apply new line in docx file generation using DOCX4J

11,067

Solution 1

The below will code will generate out put as line by line.

Key is here add Text(1) ->paragraph1 and Br ->paragraph1 after Text(2) ->paragraph1

Br is element like Text and P. by using this we can go for new line.

ObjectFactory factory = Context.getWmlObjectFactory();
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
        .createPackage();

P spc = factory.createP();
R rspc = factory.createR();

Text t1 = factory.createText();
t1.setValue("tset");
rspc.getContent().add(t1);
Br br = factory.createBr(); // this Br element is used break the current and go for next line
rspc.getContent().add(br);
Text t2 = factory.createText();
t2.setValue("\r\n tset2");
rspc.getContent().add(t2);

spc.getContent().add(rspc);

wordMLPackage.getMainDocumentPart().addObject(spc);

wordMLPackage.save(new java.io.File("helloworld.docx"));

OUTPUT:

tset

tset2

Solution 2

You can use ObjectFactory.createBr() to create a new line ending.

    ObjectFactory factory = Context.getWmlObjectFactory();

    R run = factory.createR();

    Text text1 = factory.createText();
    text1.setValue("asd");
    run.getContent().add(text1);

    Br nl = factory.createBr();
    run.getContent().add(nl);

    Text text2 = factory.createText();
    text2.setValue("efg");
    run.getContent().add(text2);

    P para = factory.createP();
    para.getParagraphContent().add(run);

    WordprocessingMLPackage wordMLPackage =
            WordprocessingMLPackage.createPackage();
    wordMLPackage.getMainDocumentPart().addObject(para);

    wordMLPackage.getMainDocumentPart().addParagraphOfText("p1");
    wordMLPackage.getMainDocumentPart().addParagraphOfText("p2");

    wordMLPackage.save(new File("test.docx"));
Share:
11,067
Weddy
Author by

Weddy

System.out.println("Weddy"); printf("is"); Console.WriteLine("my"); <html><body>name</body></html> document.write('and'); console.log('I am'); print('glad'); <?php echo 'to meet' ?> MsgBox "you."

Updated on June 14, 2022

Comments

  • Weddy
    Weddy about 2 years

    By the tutorials that I have seen. I learned how to add text on generating a docx file. but then Every time I add a line of text. I noticed that there is always a space between the first line of text and the second line of text. just like hitting the enter key twice. I know that the main cause is that everytime I add a line of text, I use a paragraph. and a paragraph starts with a space after another paragraph.

    This is how I add a text

    ObjectFactory factory;
    factory = Context.getWmlObjectFactory();
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    P spc = factory.createP();
    R rspc = factory.createR();
    rspc.getContent().add(wordMLPackage.getMainDocumentPart().createParagraphOfText("sample"));
    spc.getContent().add(rspc);
    
    java.io.InputStream is = new java.io.FileInputStream(file);
    wordMLPackage.getMainDocumentPart().addObject(spc);
    

    so this code successfully runs and produces the right output. but when i add another paragraph. or text. i want it to be just under the first line of text. is there any way that i can add a simple line of text without using a paragraph? thanks in advance

    EDIT: I've also tried adding a simple org.docx4j.wml.Text like this

    Text newtext = factory.createText();
    newtext.setValue("sample new text");
    wordMLPackage.getMainDocumentPart().addObject(newtext);
    

    the program will run but when i open the generated docx file, it will just prompt a message saying that there are problem with the contents.