Free Support Forum - aspose.com

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.


<span style=“font-size:10.0pt;
font-family:“Courier New”;color:#2B91AF;mso-font-kerning:0pt;mso-ansi-language:
PL;mso-no-proof:yes”>Document<span style=“font-size:10.0pt;font-family:
“Courier New”;mso-font-kerning:0pt;mso-ansi-language:PL;mso-no-proof:yes”> doc
= new Document(MyDir

  • “in.docx”);<o:p></o:p>

doc.Save(MyDir + "AsposeOut.rtf", SaveFormat.Rtf);

Please read following documentation links to load and save documents.
http://www.aspose.com/docs/display/wordsnet/Load+or+Create+a+Document
http://www.aspose.com/docs/display/wordsnet/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.

http://www.aspose.com/docs/display/wordsnet/DocumentBuilder+Overview
http://www.aspose.com/docs/display/wordsnet/DocumentBuilder+Class

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:

<span lang=“EN-US” style=“font-size:10.0pt;font-family:
“Courier New”;mso-fareast-font-family:“Times New Roman”;color:#2B91AF;
mso-ansi-language:EN-US;mso-fareast-language:EN-NZ”>MemoryStream<span lang=“EN-US” style=“font-size:10.0pt;font-family:“Courier New”;mso-fareast-font-family:
“Times New Roman”;color:#333333;mso-ansi-language:EN-US;mso-fareast-language:
EN-NZ”> rtfStream = <span lang=“EN-US” style=“font-size:10.0pt;
font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;color:blue;
mso-ansi-language:EN-US;mso-fareast-language:EN-NZ”>new<span lang=“EN-US” style=“font-size:10.0pt;font-family:“Courier New”;mso-fareast-font-family:“Times New Roman”;
color:#333333;mso-ansi-language:EN-US;mso-fareast-language:EN-NZ”> <span lang=“EN-US” style=“font-size:10.0pt;font-family:“Courier New”;mso-fareast-font-family:
“Times New Roman”;color:#2B91AF;mso-ansi-language:EN-US;mso-fareast-language:
EN-NZ”>MemoryStream<span lang=“EN-US” style=“font-size:10.0pt;font-family:
“Courier New”;mso-fareast-font-family:“Times New Roman”;color:#333333;
mso-ansi-language:EN-US;mso-fareast-language:EN-NZ”>();<span style=“font-size:9.0pt;font-family:“Arial”,“sans-serif”;mso-fareast-font-family:
“Times New Roman”;color:#333333;mso-fareast-language:EN-NZ”><o:p></o:p>

doc.Save(rtfStream, SaveFormat.Rtf);

//Get RTF string

string html = Encoding.UTF8.GetString(rtfStream.ToArray());


Thanks,