Repeatative text replacement does not work

Hi Aspose team,

in attached project you can find code that is opening OneNote document, making text replacement and overwriting file. If you run it 2x there is problem.

After first run:

06.12.2021 11:25:10
Starting file: OneNoteText\OneNoteText.one
Original Text: Roadshow PPT Folder
New Text: Oliver Test Change
Original Text: Montag, 6. Dezember 2021
Original Text: 11:11
Original Text:
Original Text: Roadshow PPT Folder
New Text: Oliver Test Change
Saved file: OneNoteText\OneNoteText.one

06.12.2021 11:25:11
Duration in [ms]:998

After second run:

06.12.2021 11:25:19
Starting file: OneNoteText\OneNoteText.one
Error Message: Index and length must refer to a location within the string.
Parameter name: length
Error Stack Trace: at System.String.Substring(Int32 startIndex, Int32 length)
at :clubs: .:heart:(RichText :slight_smile:)
at :clubs: .:spades: :slight_smile:()
at :clubs: .:spades: :slight_smile:()
at :heart: .:spades: :slight_smile:()
at :slight_smile: .:spades: :slight_smile:()
at :spades: .:spades: :slight_smile:()
at :slight_smile: .:spades: :slight_smile:()
at :slight_smile: .:slight_smile:()
at :slight_smile: .:spades: :slight_smile:()
at :heart: ?.:slight_smile:(Stream :slight_smile:, Document :heart:, LoadOptions :clubs:)
at Aspose.Note.Document…ctor(Stream inStream, LoadOptions loadOptions)
at Aspose.Note.Document…ctor(Stream inStream)
at AsposeOneNote.OneNote.UpdateText(String Filename) in C:_Projects\Bugs\Aspose\0\Aspose.Bugs\OneNote.cs:line 65
06.12.2021 11:25:19
Duration in [ms]:548

To repeat locally make copy of OneNoteText - Original.one and save it as a OneNoteText.one. Document OneNoteText - Damaged.one is file where one change is already done.

Thanks for help,
Oliver

OneNoteTextReplacement.zip (116.9 KB)

@dr.doc,

Please notice, after an initial test, I am able to reproduce the issue as you mentioned by using your sample document and sample code. I found an exception “Index and length must refer to a location within the string” on second attempt. I have logged a ticket with an id “NOTENET-5529” for your issue. We need to investigate it thoroughly if your code needs tweak or it is an issue with the API. We will look into it soon.

Once we have an update on it, we will let you know.

@dr.doc,

We investigated the issue further.
The reason of your issue is that after replacement RichText.Styles list is not updated according to text’s replacement. Let’s take a look at the example: RichText with “SomeTextAnotherText” text and two styles in RichText.Styles(see attached notenet5529.png).
notenet5529.png (6.8 KB)

Initially, RichText.Styles[0] defines style for “SomeText” and RichText.Styles[1] is for “AnotherText”.
If we run the code below:

richText.Text = richText.Text.Replace("SomeText", "NewText");

our richText will have “NewTextAnotherText” value that is 1 letter shorter than the original one.
And now Style[1].RunningIndex = 19 is invalid: it is too big for new text.

There are a couple of options to fix this issue:

  1. Update manually elements of richText.Styles list to have correct values for RunIndex property.
  2. The first simplest solution if it is appropriate: make replaced and replacement string of the same size. By using whitespaces, for example. Your sample works well with "Oliver Test Change " and “Roadshow PPT Folder”.
  3. The second simplest solution if it is appropriate: drop the formatting.
                        richText.Styles.Clear();
                        if (richText.ParagraphStyle != null)
                        {
                            richText.ParagraphStyle = ParagraphStyle.Default;
                        }

Please note, we are working on refactoring of public API for RichText element that will fix RunIndex property automatically. We plan to release it not later than in Aspose.Note for .NET 22.5 release.

Thanks for quick reply.
I adjusted coding and now setting RunIndex after making replacements. Still I noticed one other problem that if box with text was changed it is changing its location in OneNote page.

For example, box with text was at the page top and after few changes it went more down to the middle of page. If I am changing RunIndex why other style values are changed?

@dr.doc,

Could you please share sample code (runnable) and sample document to reproduce the issue, we will check it soon.

Just use already attached project where you’ll run application few times and you’ll see.
After I executed EXE 4-5 times I got:

Original:BeforeChanges.jpg (21.6 KB)

After making replacements: AfterChanges.jpg (13.3 KB)

@dr.doc,

Thanks for the screenshots.

We have recorded your findings with your existing ticket “NOTENET-5529” into our database. We will evaluate and look into it.

This is a known issue. It was already logged as “NOTENET-5496”. There is no ETA for it now but if this issue is equally important to you, we can rework our plans for your needs.

Yes, itwill be great if you can rework plans as without this fix I would not make any changes in OneNote documents as they’ll ruin layout so, in my opinion, this one is critical also.

@dr.doc,

Thanks for your confirmation.

Sure, we will rework our plans and get back to you once any update is available on it.

Hi Amjad,

any update/ETA here?

Thx

@dr.doc,

We are sorry but no update for the issue at the moment. However, we will check if we could provide you an ETA on it. Once we have an update on it, we will let you know here.

@dr.doc,

We are working on new public API for RichText object that will avoid error to fix styles. We plan to publish these fixes in upcoming Aspose.Note for .NET 22.5.

Hi Aspose team,

any update here? And any chance to get new API earlier?

Thx

@dr.doc,

As we told you the fix is scheduled to be included in the upcoming Aspose.Note for .NET 22.5 which will be released in May, 2022. Anyways, we will still check if the fix could be published a bit earlier (in earlier releases (if possible)). If new updates are available regarding it, we will update you here.

@dr.doc,

The APIs are under development. But we can prepare unofficial release once we have finished the implementation and testing.

Using new RichText api from Aspose.Note for .Net 22.7 the function UpdateText can be refactored in the following way:

    public void UpdateText(string Filename)
    {
        using (var fs = File.Open(Filename, System.IO.FileMode.OpenOrCreate))
        {
            try
            {
                Console.WriteLine("Starting file: " + Filename);
                Aspose.Note.Document doc = new Aspose.Note.Document(fs);
                IList<RichText> textNodes = doc.GetChildNodes<RichText>();
                foreach (RichText richText in textNodes)
                {
                    var OldTextToDisplay = richText.Text;

                    Console.WriteLine("Original Text: " + richText.Text);
                    if (richText.Text.Contains("Roadshow PPT Folder"))
                    {
                        richText.Replace("Roadshow PPT Folder", "Oliver Test Change");
                    }
                    else
                    {
                        richText.Replace("Oliver Test Change", "Roadshow PPT Folder");
                    }

                    if (OldTextToDisplay != richText.Text)
                    {
                        Console.WriteLine("New Text: " + richText.Text);
                    }
                }

                fs.SetLength(0);
                doc.Save(fs, SaveFormat.One);

                Console.WriteLine("Saved file: " + Filename);
                Console.WriteLine("===============================");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Message: " + ex.Message);
                Console.WriteLine("Error Stack Trace: " + ex.StackTrace);
            }
        }
    }

Hope that will help to resolve your issues.
Thanks!