Stop DocumentBuilder.Write from lower-casing certain words

I am using Aspose.Words.DocumentBuilder in .Net. Whenever my string contains the term “LT;”, it always gets lower-cased to “lt;” in the document. Is there a way to stop this from happening, so that the original text is preserved in the document?

@DanielBodarev Could you please provide your input document and code that will allow us to reproduce the problem? We will check the issue and provide you more information.

I tried doc.Save() after Write statement, and the document that gets saved has all the correct formatting. I don’t think this is an issue on Aspose’s end. Sorry for the confusion, I will be closing the ticket

@DanielBodarev It is perfect that you managed to isolate the problem. Please feel free to ask in case of any issues, we are always glad to help you.

I found the underlying issue. If I call Document.Range.Replace and try to replace "&lt;" with "<", the replace doesn’t work. How can I make this work?

@DanielBodarev & character is reserved for metacharacters, like &p for paragraph break and &l for line break. To make your code work you should escape & character with another &. For example see the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("&lt;&lt;&lt;&lt;&lt;&lt;&lt;");
Console.WriteLine(doc.ToString(SaveFormat.Text).Trim());
doc.Range.Replace("&&lt;", "<");
Console.WriteLine(doc.ToString(SaveFormat.Text).Trim());

That worked perfectly. Thank you very much!

1 Like