Search code examples
javadocxdocx4j

Creating a new section to .docx file in Docx4J


I am trying to insert a new section to .docx file. The inserted section should function the same way added sections through word processor does. I am using the Docx4j library for this task.

Here is code I use to create new .docx file and:

  1. Add paragraph containing a run and text
  2. Add "continuous" section to the document
  3. Add another paragraph, containing run and text
  4. Save the document to file

    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
    
    // create new paragraph with a run containing text and add it to the document.
    P paragraph1 = objectFactory.createP(); // create new paragraph
    R run1 = objectFactory.createR(); // create new run 
    Text text1 = objectFactory.createText(); // create text
    
    text1.setValue("This is text in paragraph 1 that should be located in section 1.");
    run1.getContent().add(text1); // add text ton the run
    paragraph1.getContent().add(run1); // add run to paragraph
    wordMLPackage.getMainDocumentPart().addObject(paragraph1); // add to main document part
    
    // create new section and add it to the document
    SectPr sectPr = objectFactory.createSectPr(); // create new section
    
    SectPr.Type sectPrType = objectFactory.createSectPrType();
    sectPrType.setVal("continuous"); // "continuous" means no page break before section
    
    sectPr.setType(sectPrType);
    
    wordMLPackage.getMainDocumentPart().addObject(sectPr); // add section to document part
    
    // proceed to create another paragraph with a run containing text.
    P paragraph2= objectFactory.createP(); // create new paragraph
    R run2 = objectFactory.createR(); // create new run 
    Text text2 = objectFactory.createText(); // create text
    
    text2.setValue("This is text in paragraph 2 that should be located in section 2.");
    run2.getContent().add(text2); // add text ton the run
    paragraph2.getContent().add(run2); // add run to paragraph
    wordMLPackage.getMainDocumentPart().addObject(paragraph2); // add to main document part
    
    wordMLPackage.save(new java.io.File("should contain_two_sections.docx")); // save
    

Created file contains the paragraphs defined in the code. The section is either missing or just doesn't work as inserting sections "normally" through a word processor (ie. LibreOffice Writer or Microsoft Word) does.

I've read through the Docx4J documentation, SO questions like this and in the Docx4J examples in GitHub repo, but I haven't found any working examples to add the described functionality.


Solution

  • You are adding your sectPr as a sibling to the top-level paragraphs; instead it should be within w:p/w:pPr.

    To avoid mistakes like this, you should generate Java code from a working Word docx, using either the docx4j webapp, or the Helper AddIn.

    As a side note, a sectPr is allowed as the last element in the body, but that one is added using setSectPr