Free Support Forum - aspose.com

Consolidating Runs during Find and Replace

Hi There,

I’m aware that this is an old thread, but seems like this behavior has changed in the latest version(s?).
We’ve had Aspose.Words .NET v17.7 until yesterday then upgraded to v18.10. We were actually relying on this functionality to consolidate Runs using Replace.

Now with v18.10 our entire app breaks due to, what seems to be, a breaking change in the behavior of consolidating Runs.

Questions:

  1. Can you guys confirm that you’ve made a breaking change?
  2. Is there a way to upgrade to the latest version but preserve this functionality?

Thanks,
Itzik

@ispitzen,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Aspose.Words 18.11 generated output DOCX file showing the undesired behavior
  • Your expected DOCX Word document. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document by using old Aspose.Words 17.7.
  • Please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please do not include Aspose.Words.dll files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

Hi There,

Thanks for your response!

I’m attaching here a zip file of Console c# project (Visual Studio 2015 compatible) which includes:
[Files are Under bin\debug]:

  1. A much simplified input file named “consecutive_runs.docx”.
  2. Two sample output docx files (both as docx and as zip files for both Aspose v17.7 and v18.10 outputs).
  3. The code which uses the “Range.Replace” method to consolidate Runs.

In the following images, you can see in the XML structure that in 18.10 (first image) the runs remain multiple for the replaced text while in 17.7 the text was consolidated into one run after Replace (in a way which is consistent with how Aspose.Words behaved since forever and as described in this thread).

18.10.png (23.7 KB)

17.7.png (26.3 KB)

ConsecutiveRuns.zip (5.8 MB)

I hope this provides you enough info and examples to work with, please let me know if you need anything else. Looking forward to hearing back from you!

Thanks,
Itzik

@ispitzen,

You can fix the “consecutive_runs_coutput_18.10.docx” document by using the following code:

Document doc = new Document("D:\\ConsecutiveRuns\\consecutive_runs_coutput_18.10.docx");

doc.JoinRunsWithSameFormatting();

doc.Save("D:\\ConsecutiveRuns\\18.11.docx"); 

Please see 18.11.zip (39.2 KB)

Hi Hafeez,

Thanks for your response, but unfortunately, this is not good enough for us. While in the simplified program you cannot see it, we do have plenty of “tags” we need to process which we are identifying and replacing which adjacent text with the same formatting and which we’ve used Replace thus far to consolidate the Runs for.
Can you please explain what’s the reason for the major breaking change in behavior? Also, what would be the best way tp preserve the original functionality which we’ve relied on?

Thanks,
Itzik

@ispitzen,

We are working on your query and will get back to you soon.

@ispitzen,

We tested the scenario and have managed to reproduce the same problem on our end. For the sake of any correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-17732. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@ispitzen,

Regarding WORDSNET-17732, we have completed the work on your issue and concluded to close this issue as ‘Not a Bug’. Please see the following analysis details:

Your case can be reproduced with the following simplified code:

Document doc = new Document(@"consecutive_runs.docx");
doc.Range.Replace("[[[if cn=”lease.hasMonetaryCapOnGuaranty && lease.monetaryCapOnGuarantyPeriod == 0”]]]",
                "<!!!!>", new FindReplaceOptions(FindReplaceDirection.Forward));
doc.Range.Replace("<!!!!>", "[[[if cn=”lease.hasMonetaryCapOnGuaranty && lease.monetaryCapOnGuarantyPeriod == 0”]]]"
    , new FindReplaceOptions(FindReplaceDirection.Forward));
doc.Save(@"consecutive_runs_aw_out.docx");

Actually, for the example above latest version of Aspose.Words does not perform any replacement, so you see runs, which were read from the source.

We had added a feature to escape the ampersand character. Such functionality allows to preserve symbols sequence associated with declared meta-characters as is. So, “&&” will be changed to “&” and no occurrences will be found.

Possible solution:
You have to implement additional escaping for the ampersand character i.e. replace “&” with “&&” for both pattern and replacement strings.

So, in the latest version, find/replace feature does not change document if no occurrence is found. You can also look at source document and see that output has the same Run count as original. You can call JoinRunsWithSameFormatting method explicitly if you want runs to be joined.