Problem with mail merge using XML

Hi,

I was trying out mail merge via XML using the classes you provide on GitHub.

I am attaching my test template, the test XML, and the mail merge classes as I have modified those a little bit.

My test setup code goes as follows:

Document doc = new Document("/path/to/header_sgi_it.doc");
doc.getMailMerge().setCleanupOptions(
// rimozione regioni non compilate
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
// rimozione campi non compilati
| MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
// rimozione campi innestati
| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS
);

Now, if I do

IMailMergeDataSource data = new DataTable("content of test.xml")
doc.getMailMerge().execute(data);

and then save the document as PDF, I get a totally empty PDF; if instead I do

IMailMergeDataSourceRoot data = new DataSet("content of test.xml");
doc.getMailMerge().setMergeDuplicateRegions(true);
doc.getMailMerge().executeWithRegions(data);

and again save as PDF, the region is merged, but not the fields outside any region.

What am I doing wrong?

Thanks.

Hi Matteo ,

With Aspose.Words for Java 16.11.0, you just need to execute the following code to perform mail merge via XML:

Document doc = new Document("D:\temp\header_sgi_it.doc");

doc.getMailMerge().setCleanupOptions(
// rimozione regioni non compilate
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
// rimozione campi non compilati
| MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
// rimozione campi innestati
| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS
);

DataSet ds = new DataSet();
ds.readXml("D:\temp\test.xml");

doc.getMailMerge().executeWithRegions(ds);

doc.save("D:\temp\awjava-16.11.0.pdf");

Just add the following line at top of your Main class:

import com.aspose.words.net.System.Data.DataSet;

Hope, this helps.

Best regards,

Hi,

currently we are using version 13.6, and we only have available up to version 14.2 to update.

Could you please suggest how to make it work with such versions?

Hi Matteo,

Unfortunately, it is difficult to say what the problem is and how to resolve this using a very old version of Aspose.Words for Java 13.6 or 14.2. Please note that we don’t provide support for older versions of Aspose.Words and we do not provide any fixes or patches for old versions of Aspose products either. We always encourage our customers to use the latest versions of Aspose.Words (to date it’s 16.11.0) as it contains newly introduced features, enhancements and fixes for issues reported earlier. Thanks for your understanding.

Best regards,

Yes, I do understand that we are using an old and unsupported version, however currently upgrading it is not an option, so I hoped there was a known “quick-fix” or workaround for this issue.

Thanks anyway.

Hi,

You may request a 30-day temporary license to evaluate the new functionality of Aspose.Words for Java 16.11.0.

Best regards,

*awais.hafeez:
Hi Matteo ,

With Aspose.Words for Java 16.11.0, you just need to execute the following code to perform mail merge via XML:

Document doc = new Document(“D:\temp\header_sgi_it.doc”);

doc.getMailMerge().setCleanupOptions(
// rimozione regioni non compilate
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
// rimozione campi non compilati
| MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
// rimozione campi innestati
| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS
);

DataSet ds = new DataSet();
ds.readXml(“D:\temp\test.xml”);

doc.getMailMerge().executeWithRegions(ds);

doc.save(“D:\temp\awjava-16.11.0.pdf”);

Just add the following line at top of your Main class:

import com.aspose.words.net.System.Data.DataSet;

Hope, this helps.

Best regards,*

Hi,

I have tried your suggestion, however instead of the line

ds.readXml(“D:\temp\test.xml”);

I used this

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>

Elenco Lavoratori

SISTEMA DI GESTIONE DIMOSTRATIVO

";
dataSet.readXml(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));

The template used is still the one I originally attached, and the end result did not change, see this new attachment.

As you can see, only the region was merged, but I need to merge both data in the regions and outside of those.

Here is for reference the Java method which performs the merge:

private static void merge(Document document, String xml) throws Exception {
MailMerge mm = document.getMailMerge();
mm.setFieldMergingCallback(new XmlImageMerge());
mm.setMergeDuplicateRegions(true);
mm.setCleanupOptions(
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
| MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS
);

DataSet dataSet = new DataSet();
dataSet.readXml(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));

mm.executeWithRegions(dataSet);
}

Hi Matteo,

Thanks for your inquiry. We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system as WORDSJAVA-1521. Our product team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,

*awais.hafeez:
Hi Matteo,

Thanks for your inquiry. We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system as WORDSJAVA-1521. Our product team will further look into the details of this problem and we will keep you updated on the status of correction. We apologize for your inconvenience.

Best regards,*

Good!

Is there an available estimate on when this bug will be solved? Will the fix be in version 17.5.0?

Hi Matteo,

Thanks for your inquiry. We try our best to deal with every customer request in a timely fashion, we unfortunately cannot guarantee a delivery date to every customer issue. Our developers work on issues on a first come, first served basis. We feel this is the fairest and most appropriate way to satisfy the needs of the majority of our customers.

Currently, your issue is pending for analysis and is in the queue. Once our product team completes the analysis of your issue, we will then be able to provide you an estimate.

Thanks for your patience and understanding.

The issues you have found earlier (filed as WORDSJAVA-1521) have been fixed in this Aspose.Words for .NET 17.6 update and this Aspose.Words for Java 17.6 update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

*aspose.notifier:
The issues you have found earlier (filed as WORDSJAVA-1521) have been fixed in this Aspose.Words for .NET 17.6 update and this Aspose.Words for Java 17.6 update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.*

Hi,

the issue does not seem to be solved, please see the attached ZIP archive.

I tried again with the new version, the merge is done like this:

// imposto trigger per merge immagini e HTML
com.aspose.words.MailMerge mm = document.getMailMerge();
mm.setFieldMergingCallback(new ImageMerge());
mm.setMergeDuplicateRegions(true);

// imposto pulizia campi non usati
document.getMailMerge().setCleanupOptions(
// rimozione regioni non compilate
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
// rimozione campi non compilati
| MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
// rimozione campi innestati
| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS
);

// uso merge con regioni
DataSet dataSet = new DataSet();
dataSet.readXml(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));

// eseguo merge
mm.executeWithRegions(dataSet);

However, the fields outside the regions are still ignored.

Hi Matteo,

Thanks for your inquiry. The problem occurs because “titolo” merge field is outside the “test” mail merge region. You need to call both “executeWithRegions” and “execute” methods:

DataSet ds = new DataSet();
ds.readXml("D:\temp\data.xml");

doc.getMailMerge().executeWithRegions(ds);
doc.getMailMerge().execute(ds.getTables().get(0));

doc.save("D:\temp\awjava-17.6.doc");

Hope, this helps.

Best regards,

Performing a double merge seems to work, however, I now wonder why this was previously acknowledged as a bug and put on the issue tracker, while now you just tell me to do a double merge… Shouldn’t I have done so since the first time I reported the whole issue?

Hi Matteo,

Thanks for your inquiry. WORDSJAVA-1521 was indeed a bug because the double merge (ExecuteWithRegions and Execute both methods) could not produce expected results in your previous/original usecase.

Yes, you need to separately call both ExecuteWithRegions and Execute methods as they do very different things. The MailMerge.ExecuteWithRegions method performs a mail merge operation only on ‘mail merge regions’ inside document and the MailMerge.Execute method performs a mail merge operation on simple merge fields (outside the mail merge regions). So, you have to make two calls. This is by design.

If we can help you with anything else, please feel free to ask.

Best regards,

Thanks for the explanation, it is clearer now.

I would like to ask you one more thing: given that a double call to both executeWithRegions() and execute() is needed, how do I set the cleanup options correctly?

This is what I do now:

// imposto trigger per merge immagini e HTML
com.aspose.words.MailMerge mm = document.getMailMerge();
mm.setFieldMergingCallback(new ImageMerge());

// carico i dati
DataSet dataSet = new DataSet();
dataSet.readXml(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));

// eventuale merge delle regioni
if (withRegions) {
mm.setMergeDuplicateRegions(true);

// imposto pulizia campi non usati
mm.setCleanupOptions(
// rimozione regioni non compilate
MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS
);

// 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
);

// eseguo merge
mm.execute(dataSet.getTables().get(0));

Is it correct as is, or would you recommend changing anything?

EDIT:

*WORDSJAVA-1521 was indeed a bug because the double merge
(ExecuteWithRegions and Execute both methods) could not produce expected
results in your previous/original usecase.

Yes, you
need to separately call both ExecuteWithRegions and Execute methods as
they do very different things. The MailMerge.ExecuteWithRegions method
performs a mail merge operation only on ‘mail merge regions’ inside
document and the MailMerge.Execute method performs a mail merge
operation on simple merge fields (outside the mail merge regions). So,
you have to make two calls. This is by design.*

I re-read the thread, and my post which prompted the creation of the issue on your bug tracker did not use a double merge call, so why not just say to try to call both methods before opening the issue?

Hi Matteo,

Thanks for your inquiry. Regarding WORDSJAVA-1521, please try using the following code with 17.4.0 and 17.6 versions of Aspose.Words for Java on your end:

Document doc = new Document(“D:\temp\header_sgi_it.doc”);

DataSet ds = new DataSet();
ds.readXml(“D:\temp\test.xml”);

doc.getMailMerge().executeWithRegions(ds);
doc.getMailMerge().execute(ds.getTables().get(0));

doc.save(“D:\temp\awjava-17.4.doc”);

I have attached output documents produced by above code here for your reference. You can see that the 17.4.0 version left the ‘titolo’ field un-merged.

Secondly, in this case, first call executeWithRegions method then define MailMergeCleanupOptions just before the last call to execute method.

Hope, this helps.

Best regards,

awais.hafeez:
Secondly, in this case, first call executeWithRegions method then define MailMergeCleanupOptions just before the last call to execute method.

Thanks for the suggestion, however, I was under the impression that, even if you specify the REMOVE_UNUSED_REGIONS cleanup options, those won’t really be removed if you just perform a simple merge, isn’t that right?

Hi Matteo,

Thanks for your inquiry. You are right; specifying REMOVE_UNUSED_REGIONS and then calling doc.getMailMerge().execute(ds.getTables().get(0)) will not remove mail merge regions from document. This option applies only to mail merge with regions.

Best regards,