Range.Replace replaces content in wrong direction for textboxes using C#

Hello,

About the library:

The error occurs with “Aspose-Word for Java”, version 17.4 as well as 19.4.

Additional info: in rare cases, we still use the old version 13.9.0.0 in other projects. The error I’m about to describe does not happen in these projects and therefore seems to be related to the new versions.

Error Description:

When searching and replacing text placeholders that are present in the document more than once, the placeholders in text boxes are found and replaced in the wrong order: These are not replaced in the order of their appearance in the document, but instead get replaced in a separate process in the end (which is incorrect).

This issue seems to be related (maybe even identical) to another problem I’ve reported in the past about tables. This was solved successfully by you in the past.

I need all placeholders to be found in the exact order of the document (from top to bottom). It used to work that way in earlier Aspose-versions (i.e. 13.9.0.0) without any problems.

Effect on our customers:

As soon as a customer changes within a report, data from customers is shown mixed. This leads to unreliable data generation for our customers.

Steps to reproduction:

I have prepared a simple example with the erroneous scenario: please see the archive file " FindAndReplaceBug2.zip ".
FindAndReplaceBug2.zip (189.8 KB)
Comments and further information can be found in the source code.

At initial startup, inputDocWithTextFields.docx is read and the 10 placeholders found within it are being replaced. 2 of those placeholders refer to loading of the data, while the other 8 show the actual numbers. The result is saved to output.docx.

Currently I’m using the method “com.aspose.words.Range.replace(String pattern, String replacement, FindReplaceOptions options)”.

Further information:

I have also included an altered input file called “inputDocWithoutTextFields.docx” within the archive file which shows that the replacement process works correctly if there are no text boxes present.

Additionally, I have included two .pngs that show the respective failure (new library) or success (old library) with the same input document. This proves that the functionality was working correctly in older versions.

I would be grateful for a swift solution to my problem. Thank you in advance!

Best regards,
Alexander Lyalin

Eudemonia-Solutions.AG

@Alex-Reporter

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-18466. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

A post was split to a new topic: Find and replace is not working - Order is wrong

@Alex-Reporter

It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-18466) as ‘Not a Bug’.

Please, check “FindReplaceOrder.png” picture to see order of matches which produces Word (It is view of the navigation panel). Please also attempt to search in the document using appropriate form shows that shapes are selected after the other content was processed. So, Aspose.Words mimics the MS Word behavior.

Hello Tahir,

please excuse the late reply.
Thanks for investigating the problem.

I hope that MS Word will improve sometime.

Best regards

@Alex-Reporter I think that setting the UseLegacyOrder property on FindReplaceOptions will do the Replace in the order that you want.

1 Like

@LNGLEEVFDEV
thank you very much, I have now downloaded version 20.2 and tried it out. This is exactly the solution to my problem without rebuilding the project.
Thanks a lot.

@LNGLEEVFDEV

We always encourage positive feedback from our customers. Please let us know if you have any query related to Aspose.Words.