Word Template Demo Project

Content Type: Sample
Categories:

Overview

DEMO PROJECT

  • Generate fully styled Word documents populated with data from your Mendix application!
  • Dynamically populate rich text fields, images, item lists (bullets) and tables with your Mendix data.
  • Even Excel graphs (Stacked Bar / Line) based on Mendix data are included!
  • No need for Aspose or other paid third party license; pure Mendix and Java!
  • The Word document can also be downloaded to PDF. For PDF charts aren’t supported

Also see my blog for more details: https://medium.com/mendix/how-i-automated-word-templating-in-mendix-with-the-apache-poi-api-4549163b0acc

Documentation

Demo urlhttps://wordexporter127-sandbox.mxapps.io/index.html

Description

Generate fully styled Word documents populated with data from your Mendix application! Even Excel graphs based on Mendix data are included! No need for Aspose or other paid third party license; pure Mendix and Java!

Typical usage scenario

When your Mendix app needs to be able to generate Word documents with proper styling as well as potential Excel graphs based on data from your Mendix application.

Features

  • The Word document is based on a template, maintained in Word;
  • All styles from the Word document (font size, color, etc) will be maintained;
  • The Word document can be updated with data coming from your Mendix app, preserving all styling. This works for plain text and when used in bullets / headers / tables as well;
  • If your Word document has Stacked Bar Chart templates, the Stacked Bar Charts can be updated with data coming from your Mendix app! Currently only Stacked Bar Charts and Line Charts are included;
  • Exporting to PDF is not fully working yet, see limitations. Mostly charts aren’t working.

Limitations (not included)

  1. Other types of Chart templates than Stacked Bar and Line Charts;
  2. PDF export is not working perfectly yet. For plain text it will work, but Charts are not included and table margins are looking off;

Supported scenarios 

  • Export to Word replacing tokens (=BookmarkValue) in the template for Mendix values (=ReplacementValue), preserving font styles;
  • Adding custom tables, rich text, (un)ordered lists and images
  • Adding one or many Stacked Bar and/or Line Charts with data form Mendix.

Dependencies

The solution uses quite some jar files. Be aware those can conflict with the Mendix modules also using Apache POI jars, like the Excel Importer and Excel Exporter modules.

  • For replacing words in Word:
    • poi-5.2.2, poi-ooxml-5.2.2, poi-ooxml-full-5.2.2
  • For updating Excel charts to in Word templates the jars:
    • xmlbeans-5.0.3
    • commons-collections4-4.3
    • commons-compress-1.18
  • For transforming Word docs to PDF the jars:
    • fr.opensagres.poi.xwpf.converter.core-2.0.4
    • fr.opensagres.poi.xwpf.converter.pdf-2.0.4
    • fr.opensagres.xdocreport.itext.extension-2.0.4
    • itext-2.1.7

Installation

This is a test project which comes with data, created by an after startup microflow. If the database is empty it will create both the entities which are needed for proper demonstration of the power of the module:

  1. WordReplacer: Mendix entities which are used to change the content of the Word file. The Java action ‘JA_WordDoc_Generate’ will search the Word file and if hits of ‘BookmarkValue’ are found, change them to the content of ‘ReplacementValue’;
  2. Chart / ChartValue: Mendix entities used to populate the Excel Stacked Bar Chart categories and series. It can be confusing to understand what categories and series are within the context of this module, hence you should really check with the example entities created within this module in after startup. Send an e-mail to ivo.sturm@firstconsulting to get the example word document which is needed for step 1.

 

Configuration

  1. Run the test project;
  2. Read the content in the group boxes on the home page:
    1. Instruction
    2. Features
    3. Dependencies
    4. Limitations
  3. Step 1: Upload the Word template file which can be found in the resources folder of this project
  4. Step 2: If needed, change the Word file, as well as change the bookmark values;
  5. Step 3: If needed, change the Excel Charts in the Word file, as well as change the series and categories values;
  6. Step 4: Export the Word document with updated Word and Excel content from your Mendix application.

For a demo, see https://wordexporter127-sandbox.mxapps.io/index.html?profile=Responsive

If in doubt on what is going wrong, proper Trace logging is added; set the Log level of the ‘Word’ node to Trace to see what is happening within Mendix!

 

 

 

Releases

Version: 5.3.0
Framework Version: 9.18.1
Release Notes: Bugfix: - In Word Replacement dollar sign wasn't properly escaped Enhancement - In Rich Text less limited spacing between bullet/number and text in each of the (sub)levels of bullet/number lists
Version: 5.2.2
Framework Version: 9.18.1
Release Notes: Bugfix - In Rich Text, ordered lists numbering wasn't working well for sublevels.
Version: 5.2.0
Framework Version: 9.18.1
Release Notes: * Added support for headers * Added alignment settings for footers * Added hyperlink support for rich text items
Version: 5.1.0
Framework Version: 9.18.1
Release Notes: Added support for actual Image objects. Also added an implementation of png pictures. This logic reuses the Rich Text Item logic by converting the Image object to a base64 encoded string.
Version: 5.0.0
Framework Version: 9.12.4
Release Notes: New features: PDF First decent version of PDF exporting. Works for (replaced) text, tables, rich text and item lists now. Doesn't support charts, these will not be printed to PDF. Known bugs: text in tables cells can sometimes be too small for cell. For item lists only unordered / bullet lists are supported yet. Document Settings Introduced Document Settings object to being able to change font name and size of all text altered by Java code. Any text already in template will stay as is. Footers Support for page footer including page numbering. Setting of font type and font size for all text being added / changed via Java action. Tables Added 4 attributes on TableCell: 1. TextHorizontalAlignment 2. BorderType 3. BorderBottomStyle 4. BorderTopStyle Bug fixes: 1. Fix for font size and font color implementation if using CK Editor 2. Charts are now removed from the template if not being targeted/populated via Mendix Studio Pro data. Clean up - Upgraded PDF userlib files (opensagres) from 2.0.2 to 2.0.4 - Domain model naming improvements, renamed logging from Word Exporter to Word Template, - Clean up of code by adding generic functions in WordUtils package.
Version: 4.1.0
Framework Version: 9.12.4
Release Notes: Added 4 attributes on TableCell: 1. TextHorizontalAlignment 2. BorderType 3. BorderBottomStyle 4. BorderTopStyle Clean up of code Fix for template tables which are now removed if not used in word replacements nor actual tables
Version: 4.0.0
Framework Version: 9.12.4
Release Notes: * Code clean up. Removed unused variables, deprecated code. * Upgraded project to Mendix 9.12.4
Version: 3.2.0
Framework Version: 8.18.1
Release Notes: Upgraded to Apache POI library version 5.2.2 to keep compatibility with latest Excel Importer and Excel Exporter (XLSReport) modules.
Version: 3.1.0
Framework Version: 8.18.1
Release Notes: * Fixed bug where for Word replacements the first row of tables was deleted (if not used for positioning Table data, Item Lists, Rich Text * Minor code improvements
Version: 3.0.0
Framework Version: 8.18.1
Release Notes: * Upgraded to Apache POI 4.1.2 library * Rich Text: Added Rich Text item support via ckEditor * Tables: Added support for different Cell styling * Images: Support for image exports via Rich Text. Only ckEditor widget supports this, RichText doesn't. * List Items: Added font styling within Item List elements * Charts: Only plots the series that are added via Mendix. If less series in template, no problem, the Java actio will add them, if more series in template, the java action will remove those. * Charts: Changed ChartValue.CategoryValue attribute from Integer to Decimal * Template: Multiple templates can now be managed. Only one can be the active template and templates can be toggled to (in)active.