Document.Range.Replace throws InvalidCastException

Im running version 9.4.0 of Aspose.Words.


I run this code as a workaround for the lack of support fore soft hyphenation i Aspose.Words:

Document doc = new Document(file.FullName, new Aspose.Words.LoadOptions());
doc.Range.Replace(((char)31).ToString(), “”, false, false);

In one of my documents this method throws the exeption :
Test method TrossenUtskickTestProject.FileConverterTest.DocToOutputFormatTest threw exception: System.InvalidCastException: Unable to cast object of type ‘Aspose.Words.SpecialChar’ to type ‘Aspose.Words.Run’.

I have narrowed the problem down to the word “til­lämpning” on page 8, line 1. If I remove that word the code runs ok.

Edit: I now see that an alternative to the regular soft hyphen is used. The two syllables “till” and “lämpning” put together would result i three L´s and that´s not allowed in the Swedish language. This special charachter removes one L in the cases the two syllabels are put together on the same line.
It´s hard to express these things in English, but I´m doing my best.

Dear Mathias,


Thank you for your request.

I have just reproduced your issue and observed that exception happens due to hyphen in the word pointed by you. As workaround I would recommend you use the following code:

Node[] symbols = doc.GetChildNodes(NodeType.SpecialChar, true).ToArray();
// Remove option hyphens.
foreach (SpecialChar symbol in symbols)
{
if (symbol.GetText() == ControlChar.OptionalHyphenChar.ToString())
symbol.Remove();
}

instead of the following line:
doc.Range.Replace(((char)31).ToString(), “”, false, false);

I linked your request to related record in our problem tracking system and will keep your informed about any updates regarding issue fixing.


That almost solved the problem. Although it leaves a space where the soft hyphen used to be. The resulting word becomes "til lämpning" instead of "tillämpning".
For some reason my old code that i supposed to remove the 'regular' soft hyphens, can be used to remove the space left by your code example.

This code does the trick:

Node[] symbols = doc.GetChildNodes(NodeType.SpecialChar, true).ToArray();
foreach (SpecialChar symbol in symbols)
if (symbol.GetText() == ControlChar.OptionalHyphenChar.ToString())
symbol.Remove();

doc.Range.Replace(((char)31).ToString(), "", false, false);

Thanks
/Mathias

Dear Mathias,


It is really great you found way to sidestep the problem!

Please feel free to ask us if you have any questions.


The issues you have found earlier (filed as WORDSNET-4035) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.