Examples of generating OfficeOpenXML Word (.docx) documents via com-hfg objects.

The OfficeOpenXML classes in the com-hfg library are still a work in progress but functional Word .docx files can be created.

  1. Hello Word
  2. Simple Styling
  3. Document Containing an Image
  4. Styled Table in Word

Example 1: Hello Word

  1. Docx docx = new Docx();
  2.  
  3. DocumentPart docPart = docx.getDocumentPart();
  4.  
  5. WmlBody body = docPart.getBody();
  6.  
  7. WmlParagraph p = body.addParagraph();
  8. WmlTextRun run = p.addTextRun();
  9. run.addText("Hello Word!");
  10.  
  11. File testFile = new File("testSimple.docx");
  12. docx.write(testFile);
Or simplifying the code further and using method chaining, the above code could be expressed thusly:
  1. Docx docx = new Docx();
  2. docx.getDocumentPart().getBody().addParagraph("Hello World!");
  3. docx.write(new File("testSimple.docx"));
The resulting docx file:
testSimple.docx

Example 2: Simple Styling

  1. Docx docx = new Docx();
  2.  
  3. DocumentPart docPart = docx.getDocumentPart();
  4.  
  5. WmlBody body = docPart.getBody();
  6.  
  7. body.addParagraph().br(2);
  8.  
  9. body.addParagraph().addTextRun("Hello Word w/ styling via the text run's rPr").getProperties().setBold().setColor(HTMLColor.DARK_GREEN).setFont("Courier New").setShadow();
  10.  
  11. File testFile = new File("testStyling.docx");
  12. docx.write(testFile);
The resulting output looks like this:

Docx:
testStyling.docx

Example 3: Document Containing an Image

Also demonstrates page rotation.
  1. Docx docx = new Docx();
  2.  
  3. DocumentPart docPart = docx.getDocumentPart();
  4.  
  5. WmlBody body = docPart.getBody();
  6.  
  7. WmlSectionProperties sectionProperties = body.getSectionProperties();
  8. sectionProperties.setPageSize(PaperSize.US_LETTER, Orientation.HORIZONAL);
  9. sectionProperties.getPageMargins().setTop(new Pixels(50)).setBottom(new Pixels(50)).setLeft(new Pixels(50)).setRight(new Pixels(50)).setGutter(new Pixels(0));
  10.  
  11. WmlTextRun textRun = body.addParagraph().addTextRun();
  12.  
  13. InputStream imgStream = getClass().getResourceAsStream("../../image/rsrc/test.png");
  14. if (null == imgStream)
  15. {
  16. throw new RuntimeException("The img rsrc couldn't be found!");
  17. }
  18.  
  19. ImagePart imagePart = new ImagePart(imgStream, "test.png");
  20. imagePart.setDimensions(new GfxSize2D().setWidth(new Pixels(350)).setHeight(new Pixels(450)));
  21.  
  22. textRun.addMedia(imagePart);
  23. textRun.br().addText("Fig 1. Test image on a horizontal page");
  24.  
  25. File testFile = new File("testImage.docx");
  26. docx.write(testFile);
This resulting output looks like this:

Docx:
testImage.docx

Example 4: Styled Table in Word

  1. Docx docx = new Docx();
  2.  
  3. DocumentPart docPart = docx.getDocumentPart();
  4.  
  5. WmlBody body = docPart.getBody();
  6.  
  7. WmlTableStyle tableStyle = new WmlTableStyle("testTableStyle", docx);
  8. WmlTableProperties tableProperties = tableStyle.getTableProperties();
  9. tableProperties.getBorders().addBorder(new WmlTableBorder().setStyle(WmlLineBorderStyle.single).setColor(Color.LIGHT_GRAY));
  10.  
  11. tableProperties.getTableCellMargins().addMargins(new CSSDeclaration(CSSProperty.margin, "2px 5px;"));
  12.  
  13. tableStyle.getTextRunProperties().setSize(new Points(10)).setFont("Helvetica");
  14.  
  15. // Define properties of the first column
  16. WmlTableStyleProperties tblStyleProperties = tableStyle.getProperties(WmlTableStyleProperties.Type.firstCol);
  17. tblStyleProperties.getTableCellProperties().setVerticalJustification(WmlVerticalJustification.bottom).getShading().setFill(Color.GRAY);
  18. tblStyleProperties.getParagraphProperties().setJustification(WmlJustification.right);
  19. tblStyleProperties.getTextRunProperties().setBold().setColor(Color.WHITE);
  20.  
  21. docx.getStylesPart().addStyle(tableStyle);
  22.  
  23. WmlTable table = body.addTable();
  24. table.getTableProperties().setStyle(tableStyle.getId());
  25.  
  26. WmlTableRow row = table.addRow();
  27. row.addCell("Table Type:");
  28. row.addCell("Styled");
  29.  
  30. row = table.addRow();
  31. row.addCell("Run Date:");
  32. row.addCell(new Date().toString());
  33.  
  34. File testFile = new File("testTable.docx");
  35. docx.write(testFile);
This produces a table that looks like this:

Docx:
testTable.docx
Return to Main Page