Convert Markdown to Word in C#

This guide covers the details of converting Markdown to Word in C#. It explains the step-by-step program flow and provides a working code snippet to render Markdown to DOCX in C#. Moreover, this information is valid for developing a converter in any operating system like Windows, MacOS, Linux, etc. where .NET is configured.

Steps to Render MD File to Word in C#

  1. Set up the IDE by installing the Aspose.HTML library
  2. Create a sample markdown input file
  3. Save the sample markdown file and convert it to HTML file format with the convert_markdown method
  4. Export the converted HTML file to Markdown by invoking the convert_html method

These steps elaborate on how to convert Markdown to Word in C#. As a first pre-requisite, make a markdown file by specifying any custom markdown string. Next, export the input file to HTML format which is then rendered as a Word document.

Code to Convert Markdown to Word in C#

using System.IO;
using Aspose.Html;
class Program
{
static void Main(string[] args)
{
License lic = new License();
lic.SetLicense("license.lic");
// Set path for Markdown file
string sourcePath = "input.md";
// Create a Markdown example
var code = "### Hello, World!" + "\r\n" + "Convert Markdown to DOCX!";
// Create a Markdown file
File.WriteAllText(sourcePath, code);
// Set path for DOCX file
string savePath = "document-output.docx";
// Convert Markdown to HTML document
using var document = Aspose.Html.Converters.Converter.ConvertMarkdown(sourcePath);
// Convert HTML document to DOCX file format
Aspose.Html.Converters.Converter.ConvertHTML(document, new Aspose.Html.Saving.DocSaveOptions(), savePath);
System.Console.WriteLine("MD to DOCX successfully");
}
}

This code snippet shows the process of developing an MD to Word converter in C#. It creates a Markdown file and exports it to the disk or a stream for further processing. Subsequently, the markdown file is exported to an HTML file with the convertMarkdown method. Finally, the HTML contents are rendered to Microsoft Word format by creating a DocSaveOptions class instance and invoking the convert_html method.

In this tutorial, you have learned to export MD file to Word in C#. However, if you want to learn EPUB to PDF conversion then read the article on Convert EPUB to PDF in C#.

 English