The OfficeOpenXML classes in the com-hfg library are still a work in progress but functional Word .docx files can be created.
Docx docx = new Docx();
DocumentPart docPart = docx.getDocumentPart();
WmlBody body = docPart.getBody();
WmlParagraph p = body.addParagraph();
WmlTextRun run = p.addTextRun();
run.addText("Hello Word!");
File testFile = new File("testSimple.docx");
docx.write(testFile);
Or simplifying the code further and using method chaining, the above code could be expressed thusly:
Docx docx = new Docx();
docx.getDocumentPart().getBody().addParagraph("Hello World!");
docx.write(new File("testSimple.docx"));
The resulting docx file:
Docx docx = new Docx();
DocumentPart docPart = docx.getDocumentPart();
WmlBody body = docPart.getBody();
body.addParagraph().br(2);
body.addParagraph().addTextRun("Hello Word w/ styling via the text run's rPr").getProperties().setBold().setColor(HTMLColor.DARK_GREEN).setFont("Courier New").setShadow();
File testFile = new File("testStyling.docx");
docx.write(testFile);
The resulting output looks like this:
Docx docx = new Docx();
DocumentPart docPart = docx.getDocumentPart();
WmlBody body = docPart.getBody();
WmlSectionProperties sectionProperties = body.getSectionProperties();
sectionProperties.setPageSize(PaperSize.US_LETTER, Orientation.HORIZONAL);
sectionProperties.getPageMargins().setTop(new Pixels(50)).setBottom(new Pixels(50)).setLeft(new Pixels(50)).setRight(new Pixels(50)).setGutter(new Pixels(0));
WmlTextRun textRun = body.addParagraph().addTextRun();
InputStream imgStream = getClass().getResourceAsStream("../../image/rsrc/test.png");
if (null == imgStream)
{
throw new RuntimeException("The img rsrc couldn't be found!");
}
ImagePart imagePart = new ImagePart(imgStream, "test.png");
imagePart.setDimensions(new GfxSize2D().setWidth(new Pixels(350)).setHeight(new Pixels(450)));
textRun.addMedia(imagePart);
textRun.br().addText("Fig 1. Test image on a horizontal page");
File testFile = new File("testImage.docx");
docx.write(testFile);
This resulting output looks like this:
Docx docx = new Docx();
DocumentPart docPart = docx.getDocumentPart();
WmlBody body = docPart.getBody();
WmlTableStyle tableStyle = new WmlTableStyle("testTableStyle", docx);
WmlTableProperties tableProperties = tableStyle.getTableProperties();
tableProperties.getBorders().addBorder(new WmlTableBorder().setStyle(WmlLineBorderStyle.single).setColor(Color.LIGHT_GRAY));
tableProperties.getTableCellMargins().addMargins(new CSSDeclaration(CSSProperty.margin, "2px 5px;"));
tableStyle.getTextRunProperties().setSize(new Points(10)).setFont("Helvetica");
// Define properties of the first column
WmlTableStyleProperties tblStyleProperties = tableStyle.getProperties(WmlTableStyleProperties.Type.firstCol);
tblStyleProperties.getTableCellProperties().setVerticalJustification(WmlVerticalJustification.bottom).getShading().setFill(Color.GRAY);
tblStyleProperties.getParagraphProperties().setJustification(WmlJustification.right);
tblStyleProperties.getTextRunProperties().setBold().setColor(Color.WHITE);
docx.getStylesPart().addStyle(tableStyle);
WmlTable table = body.addTable();
table.getTableProperties().setStyle(tableStyle.getId());
WmlTableRow row = table.addRow();
row.addCell("Table Type:");
row.addCell("Styled");
row = table.addRow();
row.addCell("Run Date:");
row.addCell(new Date().toString());
File testFile = new File("testTable.docx");
docx.write(testFile);
This produces a table that looks like this: