Page 1 of 1

Add dynamically columns to table with Docx4j

PostPosted: Fri Mar 24, 2017 12:48 am
by Majestic
Hello !
I have to fill a word template throught my java application. I use docx4j to manipulate the word template. It looks like that :
Image

And I would like to add column to the center table. I succeed to fill the template, to add rows but I do not know how to add column. I searched for hours but I only found how to add rows... Here's the result I want :
Image

This is how I add rows :
Code: Select all
private void replaceZones(Bill bill, WordprocessingMLPackage p) throws Docx4JException, JAXBException {
        BigDecimal total = BigDecimal.ZERO;
        Set<Zone> zones = zoneRepository.findAllByCompany(bill.getCompany());
        List<Map<String, String>> listZones = new ArrayList<>();
        for (Zone zone : zones) {
            Map<String, String> lineZone = new HashMap<String, String>();
            lineZone.put(ZONE_TAG, zone.getName());
            lineZone.put(ZONE_TOTAL_TAG, computeTotalZone(bill, zone, total) + " €");
            listZones.add(lineZone);
        }
        Map<String, String> lineBlank = new HashMap<String, String>();
        lineBlank.put(ZONE_TAG, "");
        lineBlank.put(ZONE_TOTAL_TAG, "");
        listZones.add(lineBlank);
        replaceTable(new String[] { ZONE_TAG, ZONE_TOTAL_TAG }, listZones, p, 2);
    }


Thanks !

Re: Add dynamically columns to table with Docx4j

PostPosted: Sun Mar 26, 2017 10:09 am
by jason
Adding a column requires some understanding of Word's table model.

The free "Open XML Explained" PDF ebook might cover this, or the OpenXML spec itself.

Docx4j has a table model you might be able to work with / adapt to help: https://github.com/plutext/docx4j/tree/ ... odel/table

If you come up with a nice API for adding a column, it'd make a good contribution!