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 .(RichText )
at . ()
at . ()
at . ()
at . ()
at . ()
at . ()
at .()
at . ()
at ?.(Stream , Document , LoadOptions )
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.
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.
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:
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:
Update manually elements of richText.Styles list to have correct values for RunIndex property.
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”.
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?
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.
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.
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.
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.