Can not escape special characters <% using ASPOSE.Word

Hi,

When I insert HTML using documentbuilder.InsertHtml() method and the html has un-encoded characters <% (no space b/w them) then the out put document is corrupted.
e.g. try passing the below html

<html xmlns:str="http://exslt.org/strings" xmlns:XsltExtension="urn:XsltCustomExtension">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p style="text-align:justify;">This a question for a  variable <%  5. This a question for a  variable 5%.</p>
</body>
</html>

The output document will be corrupted and will not have the text <% 5. This a question for a variable 5%.>

I tried to convert the above html using online PDF converter tool at https://pdfcrowd.com/#convert_by_input and it is converted correctly.

I also encoded the above html and passed it to the InsertHtml() method but Aspose.Word just output as is and was not decoded as was decoded automatically by the above online converter

Could you have a look in to this issue and suggest any solution to it?

Hi
Thanks for your request. But it is not quite clear for me what you mean. It seem when you inserted your HTML into the message body some of characters were escaped. Could you please attach your input HTML as a file (you can zip it to attach in forum)? Also, it would be great if you attach your output and expected output here. We will check the issue and provide you more information.
Best regards,

Hi,
I can not upload files b/c it is restricted. I have tried to provide enough info to explain. See below

  1. I have the below richtext
    This a question for a variable <% 5. This a question for a variable 5%.
  2. Which we have encoded to the below text after some processesing
    &lt;p style="text-align:justify;"&gt;This a question for a variable &lt;% 5. This a question for a variable 5%.&lt;/p&gt;
  3. Before passing the above text to Aspose.Word, the above text is decoded back and inserted as html text. See below
<html xmlns:str="http://exslt.org/strings" xlns:xsltextension="urn:XsltCustomExtension">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <div>
        <!--CASE 1: <% is not encoded (this is not a vaild html)-->
        <p style="text-align:justify;">This a question for a variable <% 5. This a question for a variable 5%.</p>
    </div>
</body>
</html>
  1. Passing the above html text to the InsertHtml(), I want the below output
    This a question for a variable <% 5. This a question for a variable 5%.

I see the issue is <%. it has special meaning in Html, which make the above Html an invalid html.

But I also tried to pass the below encoded Html text to InsertHtml(), but it outputs the encoded text as is, Aspose.Word does not decode the text automatically

<html xmlns:str="http://exslt.org/strings" xmlns:xsltextension="urn:XsltCustomExtension">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <div>
        &lt;p style="text-align:justify;"&gt;This a question for a variable &lt;% 5. This a question for a variable 5%.&lt;/p&gt;
    </div>
</body>
</html>

Hello

Thanks for your inquiry. I think in your case you can try using Replace functionality of Aspose.Words to replace special characters after inserting HTML. Please see the following link to learn more:

https://docs.aspose.com/words/net/find-and-replace/

Also I have tried the following code and all works fine on my side:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml(@"<html xmlns:str='http://exslt.org/strings' xmlns:XsltExtension='urn:XsltCustomExtension'>
<head>
   <meta http-equiv='Content-Type'content='text/html; charset=UTF-8' />
</head>
<body>
    <p style='text-align:justify;'>This a question for a  variable <%  5. This a question for a  variable 5%.</p>
</body>
</html>");
doc.Save("C:\\Temp\\out.doc");

Please see the attached document.

Best regards,

Hi,
I am using Aspose.Word v9.5, in which version you tested it. Could you please test this in v9.5 and see if still get the correct output?
Regards
Riaz Afridi

The attached document is not correct, see the html tags, these should not be there
This a question for a variable <% 5. This a question for a variable 5%.

Thanks for your request. Yes, I see the issue. But should not your HTML look like this:

<html xmlns:str="http://exslt.org/strings" xmlns:xsltextension="urn:XsltCustomExtension">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p style="text-align: justify;">This a question for a variable<% 5. This a question for a variable 5%></p>
</body>
</html>

In your original HTML there is opening <%, but there is no closing %> and that is why the problem occurs.

Best regards,