Html String to RTF Formatted String

Hi,

I want to convert a piece of HTML (as a string) to a RTF formatted string.

Something like this:

String aContentAsHtml = @"some text";
String aContentAsRtf = [aspose method].ConvertString(aContentAsHtml, SaveFormat.Rtf);

My case is in fact a bit similar to this one: https://forum.aspose.com/t/convert-html-string-to-rtf-string/114845

But i don’t want to save the output directly to a RTF file, just convert the HTML from string to string.

Is this possible?

Thanks,

Martín.

Hi Martin,

Thanks for your request. In your case, you can save RTF to stream and then extract RTF string from this stream. Code will look like this:

private static string GetRtfString(string html)
{
    string rtfString = "";
    // Create a document and insert HTML into it.
    DocumentBuilder builder = new DocumentBuilder();
    builder.InsertHtml(html);
    // Save document to stream as RTF and get RTF string.
    using (MemoryStream rtfStream = new MemoryStream())
    {
        builder.Document.Save(rtfStream, SaveFormat.Rtf);
        byte[] rtfBytes = rtfStream.ToArray();
        rtfString = Encoding.UTF8.GetString(rtfBytes);
    }
    return rtfString;
}

Hope this helps.

Best regards,

Thanks for your help Alexey. It’s exactly what I was looking for.

As I was using the above code with a evaluation version of Aspose.Words another question was raised. I hope you can answer this one too as it is a weird thing to explain.

Sometimes, my converted HTML code to RTF format causes the RTF Document to show weird signs/symbols of some kind. Example:

UPDATE ONGEVEER EEN UUR GELEDEN

Also, text colors don’t always show up as I expect. Are these things caused by possible limitations of the evaluation version of doesn’t Aspose.Words fully support HTML formatting when converting it to RTF?

Thanks in advance,
Martín.

Hi Martín,

Thanks for your request. Could you please attach your source HTML and output RTF here? We will check the issue and provide you more information.

Best regards,

Hi again,

It turns out that Altova Stylevision (which I use to generate the RTF file with an XML/XSL and all translated RTF pieces) causes the problems with the fonts and colors.

Thanks for your time though!

Martín.

Hi Martín,

Please let us know if you need more assistance, we are always glad to help you.

Best regards,

A post was split to a new topic: How to retrieve the body section from the RTF string?