Good evening,
I’m having a problem with mail merge.
Introduction:
I’m using version 22.12, and the same problem occurs when trying the online mail merge from the Aspose website, where, I assume, the latest version is available.
Problem:
A case where the mail merge of a document composed of a single table, in addition to populating the table by adding rows for each set of values in the XML, also repeats the entire body of the document N times (equal to the number of rows generated).
I found this in the Aspose documentation:
https://docs.aspose.com/words/net/types-of-mail-merge-operations
“SimpleMailMerge operation … The main limitation of using this type is the whole document content will be repeated for each record in the data source.”
I don’t know if this could be the cause, and if so, what solution can I adopt?
For now, I’ve solved the problem by simply adding an extra node to the XML at the same level as the table (<EXTREMELY_SAFE_TAG>IAM_SAFE</EXTREMELY_SAFE_TAG>).
I’m attaching the template input files, the metadata (I’m attaching the docx to convert to xml because it won’t let me attach the xml directly), and the resulting output.
demo.docx (812,9 KB)
dati.docx (13,6 KB)
Result_demo.docx (418,6 KB)
result obtained from the online mail merge:
demoMerged.docx (379,3 KB)
Below is the code used for the mail merge:
public void stampaUnione(final File mergedFile, final File metadatiFile, final File img, 
                             final boolean cancellaCampi) throws Exception {
        
        //Leggo la sorgente dati da un file xml e ne creo un DataSet
        final DataSet dataSet = new DataSet();
        dataSet.readXml(metadatiFile.getAbsolutePath());
        // Apro il documento template
        final MailMerge mm = this.doc.getMailMerge();
        // imposto pulizia campi non usati
        if (cancellaCampi) {
            mm.setCleanupOptions(
                    // rimozione regioni non compilate
                    MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
                    // rimozione campi non compilati
                    | MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
                    // rimozione campi innestati
                    | MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS
            );
        }
        
        //Callback per il merge di un'immagine, se passata come input
        if (img != null) {
            mm.setFieldMergingCallback(new HandleMergeImageField(img));
        }
        //Eseguo prima la chiamata ad un execute per le regioni
        mm.executeWithRegions(dataSet);
        //Seccessivamente eseguo la execute per i campi fuori regione
        //Nota: Si assume che a livello 0 ci sia un elemento tag contenitore
        mm.execute(dataSet.getTables().get(0));
        if (cancellaCampi) {
            mm.deleteFields();
        }
        
        this.doc.save(mergedFile.getAbsolutePath());     
    }
As mentioned in the introduction, I also checked the online version of Aspose, and even there (despite not replacing the mail merge tags) it generates the body N times.
Can you help me understand the cause and possibly find a solution?
Thank you.