0

I'm trying to create a table and add it to MainDocumentPart using docx4j library as below but its corrupting the document and it's not opening ( im using MS Word 2010 with docx format )

// creating docx

    WordprocessingMLPackage wordPackage =   WordprocessingMLPackage.createPackage();

      // creating a table

      // use ObjectFactory for creating the table, table row and table cell

       ObjectFactory factory =  Context.getWmlObjectFactory();


       // create the table

       Tbl table = factory.createTbl();


       // create a row

       R row = factory.createR();


       // create a cell

       Tc cell = factory.createTc();


       // add the content to cell

       cell.getContent().add(wordPackage.getMainDocumentPart().createParagraphOfText("table cell data added"));


       // add the cell to row

       row.getContent().add(cell);


       // add the row to table

       table.getContent().add(row);


       // adding the table to main document part

       wordPackage.getMainDocumentPart().addObject(table);


    wordPackage.save(new File("D:\\Programs\\test\\Doc222.docx"));
JavaGeek
  • 1,535
  • 9
  • 39
  • 63

1 Answers1

2

R is not a row; it is a run.

To generate your table, the easiest way is to create a suitable docx in Microsoft Word (or LibreOffice/OpenOffice or whatever), and upload it to the docx4j demo webapp.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84