I am testing the latest version as we have noticed a problem with an older one, but that problem seems to be present in the latest version too.
Specifically, I created this test document to showcase the issue:
test-dev-3098.docx (15.4 KB)
when it gets mail merged with the following XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<rischi_n>
<rischio><![CDATA[RISCHIO N 1]]></rischio>
<prevenzioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 1]]></prevenzione>
</prevenzioni_specifiche_da_adottare>
<prevenzioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 2]]></prevenzione>
</prevenzioni_specifiche_da_adottare>
<prevenzioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 3]]></prevenzione>
</prevenzioni_specifiche_da_adottare>
<prevenzioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 1]]></prevenzione>
</prevenzioni_specifiche_adottate>
<prevenzioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 2]]></prevenzione>
</prevenzioni_specifiche_adottate>
<prevenzioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 3]]></prevenzione>
</prevenzioni_specifiche_adottate>
<protezioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 1]]></prevenzione>
</protezioni_specifiche_da_adottare>
<protezioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 2]]></prevenzione>
</protezioni_specifiche_da_adottare>
<protezioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 3]]></prevenzione>
</protezioni_specifiche_da_adottare>
<protezioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 1]]></prevenzione>
</protezioni_specifiche_adottate>
<protezioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 2]]></prevenzione>
</protezioni_specifiche_adottate>
<protezioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 3]]></prevenzione>
</protezioni_specifiche_adottate>
</rischi_n>
<rischi_n>
<rischio><![CDATA[RISCHIO N 2]]></rischio>
<prevenzioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 21]]></prevenzione>
</prevenzioni_specifiche_da_adottare>
<prevenzioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 22]]></prevenzione>
</prevenzioni_specifiche_da_adottare>
<prevenzioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 23]]></prevenzione>
</prevenzioni_specifiche_da_adottare>
<prevenzioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 21]]></prevenzione>
</prevenzioni_specifiche_adottate>
<prevenzioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 22]]></prevenzione>
</prevenzioni_specifiche_adottate>
<prevenzioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 23]]></prevenzione>
</prevenzioni_specifiche_adottate>
<protezioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 21]]></prevenzione>
</protezioni_specifiche_da_adottare>
<protezioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 22]]></prevenzione>
</protezioni_specifiche_da_adottare>
<protezioni_specifiche_da_adottare>
<prevenzione><![CDATA[PREVENZIONE DA ADOTTARE 23]]></prevenzione>
</protezioni_specifiche_da_adottare>
<protezioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 21]]></prevenzione>
</protezioni_specifiche_adottate>
<protezioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 22]]></prevenzione>
</protezioni_specifiche_adottate>
<protezioni_specifiche_adottate>
<prevenzione><![CDATA[PREVENZIONE ADOTTATE 23]]></prevenzione>
</protezioni_specifiche_adottate>
</rischi_n>
<rischi_a>
<rischio><![CDATA[RISCHIO A 1]]></rischio>
</rischi_a>
<rischi_a>
<rischio><![CDATA[RISCHIO A 2]]></rischio>
</rischi_a>
</root>
the result is this:
test-dev-3098.pdf (27.7 KB)
as you can see, this is wrong: the second “block” named rischi_a
has no content in its children for blocks like prevenzioni_specifiche_da_adottare
(it doesn’t even have the key in the data), and yet it gets merged duplicate contents.
Is this something we need to change on our side, or a problem in the library itself?
@mtassinari
It seems you are experiencing an issue with the mail merge functionality in Aspose.Words, specifically when using regions in your XML data. The problem you described, where the second block named rischi_a
is merging duplicate content despite having no corresponding data, can often arise from how the mail merge regions are defined and how the data is structured.
Possible Solutions
-
Check Region Definitions: Ensure that the mail merge regions in your document are correctly defined. Each region should start with a TableStart:
field and end with a TableEnd:
field. If these fields are not properly paired or if there are any mismatches in naming, it can lead to unexpected results during the merge.
-
Data Structure: The XML data structure should match the expected format for the mail merge. Each region in the document should correspond to a unique data source in your XML. If a region is defined but not populated with data, it may still attempt to merge based on previous entries.
-
Use of MergeDuplicateRegions
: If you are using the MergeDuplicateRegions
option, consider whether this is appropriate for your use case. This option can cause duplicate entries to be merged if the data source is not structured correctly.
-
Debugging the Data Source: You can debug the data source by logging or inspecting the data being passed to the mail merge. This can help identify if the data is being read correctly and if the expected values are present.
-
Testing with Simplified Data: Try testing with a simplified version of your XML data to isolate the issue. This can help determine if the problem lies within the data structure or the document itself.
Example Code Snippet
Here’s a basic example of how to set up a mail merge with regions using XML data:
DataSet dataSet = new DataSet();
dataSet.ReadXml("path_to_your_xml_file.xml");
Document doc = new Document("path_to_your_template.docx");
doc.MailMerge.ExecuteWithRegions(dataSet);
doc.Save("output.docx");
Make sure that your XML file is structured correctly and that the document template has the appropriate mail merge fields defined.
If the issue persists after checking these aspects, it may be beneficial to reach out to Aspose support with your specific document and XML data for further assistance.
Additional Resources
For more detailed information on using mail merge with regions, you can refer to the official documentation on MailMerge.ExecuteWithRegions.
If you have any further questions or need additional help, feel free to ask!
I would like to add that I was expecting to see this result:
test-dev-3098-expected.pdf (23.7 KB)
the mail merge operation is performed like this:
merge(document, report, true);
where report
is the XML data stream and merge()
is defined as
private static void merge(Document document, InputStream xmlStream, boolean withRegions) throws Exception {
// imposto trigger per merge immagini e HTML, e abilito sintassi Mustache
com.aspose.words.MailMerge mm = document.getMailMerge();
mm.setFieldMergingCallback(new ImageMerge());
mm.setTrimWhitespaces(true);
// creo il dataset
com.aspose.words.net.System.Data.DataSet dataSet = new com.aspose.words.net.System.Data.DataSet();
dataSet.readXml(xmlStream);
// eventuale merge delle regioni
if (withRegions) {
mm.setMergeDuplicateRegions(true);
// imposto pulizia campi non usati
mm.setCleanupOptions(
// rimozione regioni non compilate
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
// rimozione righe di tabella vuote
| MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS
// rimozione paragrafi vuoti
| MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS
);
// eseguo merge
mm.executeWithRegions(dataSet);
}
// imposto pulizia campi non usati
mm.setCleanupOptions(
// rimozione campi non compilati
MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
// rimozione campi innestati
| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS
// rimozione paragrafi vuoti
| MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS
);
// eseguo merge
mm.execute(dataSet.getTables().get(0));
// pulizia campi
FieldsHelper.convertFieldsToStaticText(document, FieldType.FIELD_IF);
}
I tried removing the line mm.setMergeDuplicateRegions(true);
but it changed nothing.
@mtassinari
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): WORDSNET-28224
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Could you please just confirm that the problem is in Aspose.Words library itself, and not in our mail merge code/logic I’ve shown?
@mtassinari We will investigate the issue further and provide you more information. but at first glance this is the problem is data source and how it is read into DataSet
.
The issues you have found earlier (filed as WORDSNET-28224) have been fixed in this Aspose.Words for .NET 25.8 update also available on NuGet.