Page 1 of 1

MailMerger with mailing labels

PostPosted: Fri Aug 15, 2014 11:45 pm
by Sébastien Baudin
I'm using docx4j to merge a list of data from a Java application into a Word Labels document containing mergefields.

My Word document has the following structure:
Code: Select all
«TITRE»
«NOM» «PRENOM»
«RUE_NUM»
«NPA» «LOCALITE»

«Next Record»«TITRE»
«NOM» «PRENOM»
«RUE_NUM»
«NPA» «LOCALITE»

«Next Record»«TITRE»
«NOM» «PRENOM»
«RUE_NUM»
«NPA» «LOCALITE»


And I use the getConsolidatedResultCrude method from MailMerger to merge a List of map field. (In this case I give a list with 2 elements inside)
Code: Select all
   
private WordprocessingMLPackage mergeFileData(WordprocessingMLPackage wordMLPackage, List<Map<DataFieldName, String>> mapFields) {
  MailMerger.setMERGEFIELDInOutput(OutputField.KEEP_MERGEFIELD);
  try {
    return MailMerger.getConsolidatedResultCrude(wordMLPackage, mapFields);
  } catch (Docx4JException e) {
    LOGGER.error("Error when merging data and template", e);
    throw new RuntimeException(e);
  }
}


And the result in generate file is
Code: Select all
Madame
Dupond Léa
Chemin 11
1219 Genève

«Next Record»Madame
Dupond Léa
Chemin 11
1219 Genève

«Next Record»Madame
Dupond Léa
Chemin 11
1219 Genève

BREAK SECTION

Monsieur
Doe John
Rue Marterey 5
1005 Lausanne

«Next Record»Monsieur
Doe John
Rue 5
1005 Lausanne

«Next Record»Monsieur
Doe John
Rue 5
1005 Lausanne


and the result I'd like to have
Code: Select all
Madame
Dupond Léa
Chemin 11
1219 Genève

Monsieur
Doe John
Rue 5
1005 Lausanne


Is MailMerger able to use the NEXT instruction from MergeField or how it's possible to update my code or template to solve this problem.

Thanks

Sébastien

Re: MailMerger with mailing labels

PostPosted: Thu Sep 11, 2014 7:14 pm
by Sébastien Baudin
I resolve my issue by updating the docx4j sources.

You can find it here: https://github.com/plutext/docx4j/pull/133