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

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.

Image for post
Image for post

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store