Ive spent the past few hours installing and configuring docx4j and playing around with a few of the sample classes.
I'm now at the point where I have created a table and want to set the contents of a specific cell, but I'm struggling to work out how to do this.
I have created a table with 3 rows and 3 columns.
- Code: Select all
// try some styles
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Title", "Hello world");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading1", "Heading 1");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading2", "Heading 2");
wordMLPackage.getMainDocumentPart().addStyledParagraphOfText("Heading3", "Heading 3");
wordMLPackage.getMainDocumentPart().addParagraphOfText("from docx4j!");
// add an image
File file = new File("e:\\temp\\login_cqms.jpg" );
byte bytes[] = new byte[(int) file.length()];
bytes = FileUtils.readFileToByteArray(file);
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
org.docx4j.wml.P p = newImage( wordMLPackage, bytes, filenameHint, altText, id1, id2 );
wordMLPackage.getMainDocumentPart().addObject(p);
// create a table
int writableWidthTwips = wordMLPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips/3);
wordMLPackage.getMainDocumentPart().addObject(tbl);
String filePath = "e:\\temp\\docx4j.docx";
wordMLPackage.save(new java.io.File(filePath) );
How, for example
a. would I reference the 3rd column in the 1st row and set its text to 'this is the text'?
b. insert an embedded image into the 2nd cell of the 1st row (Ive already managed to insert an image into a document, just not into a specific cell)?
Any help much appreciated.