How to Convert RTF to DOCX in C# .Net Framework

Cloudmersive
2 min readAug 5, 2020

--

While RTF is a convenient enough format in many situations, it definitely has its share of flaws. I cannot count the number of times I have accidentally edited an RTF in the wrong program and ended up with a corrupted file. The level of formatting customization and flexibility is also lacking when compared with DOCX. With all that in mind, today’s lesson is going to cover a very simple method by which we can convert between these two formats, all set up in just a few minutes.

First we will need to install our API client, for which we can use the following command:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8

And now we call the RTF to DOCX conversion function from our library:

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class ConvertDocumentRtfToDocxExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new ConvertDocumentApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.try{// Convert Rich Text Format RTF to DOCX Documentbyte[] result = apiInstance.ConvertDocumentRtfToDocx(inputFile);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentRtfToDocx: " + e.Message );}}}}

And you’re done! Easy.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet