Free Support Forum - aspose.com

Unable to removed non-merged regions

Hi everybody,

I have attached a template which makes use of regions for mail merge, and in my Java code which compiles this template I have also set setRemoveEmptyRegions(true);

However when the template is compiled, the unmerged region is still there, as the attachment shows.

What am I doing wrong?

Thanks.



EDIT:

The docs say something about a setCleanupOptions() method of MailMerge instance, but it does not seem to be present on my version, or at least my IDE does not “see” it.

I’m currently using version 10.4, I think, but I do not know how to check.

Ok, here is what I just tried to do: based on this article in the docs I created a modified version of the extractContent method so that it could just return the array of nodes between startNode and endNode without cloning them or making any modification, then I created this method:

protected static Document removeRegion(Document doc, String region) throws Exception {
DocumentBuilder builder = new DocumentBuilder(doc);
String fieldStart = “TableStart:” + region;

// mi sposto all’inizio della regione
if (builder.moveToMergeField(fieldStart, false, false)) {
String fieldEnd = “TableEnd:” + region;

// salvo nodo attuale
Node start = builder.getCurrentNode();

// mi sposto alla fine della regione
if (builder.moveToMergeField(fieldEnd, false, false)) {
// estraggo i nodi
ArrayList nodes = Report.extract(doc, start, builder.getCurrentNode());

// li cancello
for (Node node : nodes) {
node.remove();
}
}
}

return doc;
}

but when I try to call it as removeRegion(doc, “Produttore”); it would always fail to find the field TableStart:Produttore even if in the template document, which I attached in my previous post, it is clearly present, and is present also after the region merging.

Any help here would be appreciated.

Hi Matteo,


Thanks for your inquiry.
Matteo:
I’m currently using version 10.4, I think, but I do not know how to check.
You can determine Aspose.Words’ JAR version by unziping ‘Aspose.Words.jdk16.jar’ file. Please navigate to ‘META-INF’ folder and then open ‘MANIFEST.MF’ with Notepad. There you’ll find something like below:

Manifest-Version: 1.0
Specification-Title: Aspose.Words for Java
Specification-Version: 11.10.0.0
Specification-Vendor: Aspose Pty Ltd
Implementation-Title: Aspose.Words for Java
Implementation-Version: 11.10.0.0
Implementation-Vendor: Aspose Pty Ltd
Release-Date: 2012.06.30

Secondly, I encourage you please use the latest release versions of Aspose.Words as it contains newly introduced features, enhancements and fixes for issues reported earlier. I would suggest you please upgrade to the latest version (11.10.0). You can download it from the following link:
http://www.aspose.com/community/files/72/java-components/aspose.words-for-java/default.aspx

Once you manage to successfully upgrade to the latest version, please use the technique mentioned here to remove unmerged regions from a Document.

I hope, this helps.

Best regards,

I can confirm that our aspose.words version is 10.4, and we cannot
upgrade since our subscription is expired and we cannot afford to renew
it right now.

Could you please take a look to the removeRegion() method I wrote, which I posted in my second post in this thread, and give me some hints as for why it fails to find the unmerged region? In fact, if you look in both template and compiled report, it should find the field TableStart:Produttore but instead it doesn’t.

Hi Matteo,


Thanks for the additional information. First of all, please note that unfortunately, we don’t provide support for older released versions of Aspose.Words and we do not provide any fixes or patches for old versions of Aspose products either. So, we strongly recommend you please use the latest official release of Aspose.Words that is 11.10.0

Secondly, I think, the problem occurs when you move the cursor to a position where TablsStart/TablsEnd fields are located by using DocumentBuilder.moveToMergeField method. This method actually removes the MERGEFIELDs themselves. I think, you should move the cursor to the parent Paragraphs of TableStart/TableEnd fields and extract/remove the content between these Paragraphs. I hope, this helps.

Best regards,

I know that, normally, the moveToMergeField() method would remove it, in fact as you can see I passed the proper arguments so that it is NOT removed by the DocumentBuilder.

Here problem here is that the moveToMergeField() call returns false, meaning field not found, even if that merge field is clearly on both documents, and I cannot understand why it fails to find it.

And if it is so, how could I get the parent paragraph of a not-found field?

I’ve just been able to obtain “something” trying to search for a mergefield named region instead of “TableStart:” + region in my removeRegion() method.

The problem is that now it would get some nodes with the modified extract() method but when trying to delete them I get an exception saying Cannot remove because there is no parent.

Hi Matteo,


Thanks for the additional information. Yes, omitting the prefix ‘TableStart:’ helps in finding the MailMerge regions. Secondly, this error might occur if you removed parent of a node earlier. For example let’s consider the following code:

Document doc = new
Document();

DocumentBuilder builder = new DocumentBuilder(doc);

Run run = doc.FirstSection.Body.FirstParagraph.Runs[0];

doc.FirstSection.Remove();

// Now remove parent of run from the document.

run.ParentNode.Remove();

// Now if you atempt to remove run you will get an exception because you already removed parent of this node earlier.

run.Remove();


I hope, this helps in identifying the problem.

Best regards,

Yes, I understand the problem you’re explaining, and it could actually be 'caused by this.

I do not understand a couple of things though: if I had to omit the “TableStart:” when searching for a region mail merge field, how can I then match its corresponding end so to remove all that is included between the two?

I’ve done some logging, and the field it matches when looking for the region omitting the “TableStart:” prefix is of type FIELD_START, if I remove that one only will I remove the entire region?

Hi Matteo,

Thanks for your inquiry. Yes, FieldStart class represents a start of a Word field (e.g. MERGEFIELD, PAGE, NUMPAGES fields etc) in a document. TableStart field always comes before TableEnd field in a Word Document; therefore, calling moveToMergeField method for the first time moves the cursor to TableStart field and calling this method again will move the cursor to TableEnd field. Moreover, to remove all the content that is enclosed in a region, you need to remove all the Nodes between the TableStart and TableEnd nodes.

Best regards,