Aspose.Words .NET - Converting HTML to RTF

Hello,

I’m currently working in .NET 3.5. I downloaded Aspose.Words to try out some samples and check out some of the API for converting HTML to RTF.

I did some basic research, but I was not able to find out the calls that converts the HTML to RTF. All I need to do is convert the HTML (string) to RTF (string) - so that the RTF can be used by ActiveReports for printing purposes.

Please let me know if you can help me with that. Thanks, -Manish

Hi Manish,

Thanks for your inquiry. Please use the following code snippet to convert an HTML to RTF.

Document doc = new Document(MyDir + "in.docx");
doc.Save(MyDir + "AsposeOut.rtf", SaveFormat.Rtf);

Please read following documentation links to load and save documents.
https://docs.aspose.com/words/net/create-or-load-a-document/
https://docs.aspose.com/words/net/save-a-document/

You may use DocumentBuilder.InsertHtml method to inserts HTML into a document as shown in following code example. Please read following documentation links for your kind reference.

https://reference.aspose.com/words/net/aspose.words/documentbuilder/
https://reference.aspose.com/words/net/aspose.words/documentbuilder/

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertHtml("Paragraph right" +"**Implicit paragraph left**" +"Div center" +"Heading 1 left.");
doc.Save(MyDir + "AsposeOut.rtf", SaveFormat.Rtf);

Hello Tahir,
So is it necessary that I convert I create the “doc” first and insert html in it using the builder for converting it to RTF? Are there no function calls to do that?
All I’m trying to do is -

Dim html As String
Dim rtf As String
html = "Paragraph right" + "**Implicit paragraph left**" + "Div center" + "Heading 1 left."

// this is what I need to do - want to pass the html and convert that to RTF
rtf = Converstion Call(html) 

Let me know.
Thanks,
-Manish

Hi Manish,

Thanks for your inquiry.

Yes, Aspose.Words works based on the document object model and that is how you load and save documents. You can wrap these calls into a single method called ConvertHtmlToRtf if requried.

Furthermore to convert to RTF without saving to disk you can use a stream:

MemoryStream rtfStream = new MemoryStream();
doc.Save(rtfStream, SaveFormat.Rtf);
// Get RTF string
string html = Encoding.UTF8.GetString(rtfStream.ToArray());

Thanks,