Markdown to Word Conversion
17 Dec 202412 minutes to read
Markdown is a lightweight markup language that adds formatting elements to plain text documents. The .NET Word (DocIO) library supports the conversion of Markdown to Word document and vice versa, which mostly follows the CommonMark specification and GitHub-flavored syntax.
Assemblies and NuGet packages required
Refer to the following links for assemblies and NuGet packages required based on platforms to convert a Markdown file to a Word document using the .NET Word Library (DocIO).
Convert Markdown to Word document
Convert an existing markdown file to a Word document (DOC, DOCX and RTF) using the .NET Word (DocIO) library.
The following code example shows how to convert Markdown to Word document.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
//Open the file as a Stream.
using (FileStream docStream = new FileStream("Input.md", FileMode.Open, FileAccess.Read))
{
//Load the file stream into a Markdown file.
using (WordDocument document = new WordDocument(docStream, FormatType.Markdown))
{
//Save as a Word document into the MemoryStream.
MemoryStream outputStream = new MemoryStream();
document.Save(outputStream, FormatType.Docx);
}
}
//Open an existing Markdown file.
using (WordDocument document = new WordDocument("Input.md", FormatType.Markdown))
{
//Save as a Word document.
document.Save("MarkdownToWord.docx", FormatType.Docx);
}
'Open an existing Markdown file.
Using document As WordDocument = New WordDocument("Input.md", FormatType.Markdown)
'Save as a Word document.
document.Save("MarkdownToWord.docx", FormatType.Docx)
End Using
You can download a complete working sample from GitHub.
TIPS
You can also save the markdown file as HTML, PDF, and Image.
Customize image data
The .NET Word (DocIO) library provides a ImageNodeVisited event, which customizes image data while importing a Markdown file. Implement the logic to customize the image data by using this ImageNodeVisited event.
The following code example shows how to load image data based on the image source path when importing the Markdown files.
//Create a Word document instance.
using (WordDocument document = new WordDocument())
{
//Hook the event to customize the image while importing Markdown.
document.MdImportSettings.ImageNodeVisited += MdImportSettings_ImageNodeVisited;
//Open the Markdown file.
document.Open("Input.md");
//Save as a Word document to the MemoryStream.
MemoryStream outputStream = new MemoryStream();
document.Save(outputStream, FormatType.Docx);
}
//Create a Word document instance.
using (WordDocument document = new WordDocument())
{
//Hook the event to customize the image while importing Markdown.
document.MdImportSettings.ImageNodeVisited += MdImportSettings_ImageNodeVisited;
//Open the Markdown file.
document.Open("Input.md");
//Save as a Word document.
document.Save("Sample.docx");
}
'Create a Word document instance.
Using document As WordDocument = New WordDocument()
'Hook the event to customize the image while importing Markdown.
document.MdImportSettings.ImageNodeVisited += MdImportSettings_ImageNodeVisited
'Open the Markdown file.
document.Open("Input.md")
'Save as a Word document.
document.Save("Sample.docx")
End Using
The following code examples show the event handler to customize the image based on the source path.
private static void MdImportSettings_ImageNodeVisited(object sender, Syncfusion.Office.Markdown.MdImageNodeVisitedEventArgs args)
{
//Set the image stream based on the image name from the input Markdown.
if (args.Uri == "Image_1.png")
args.ImageStream = new FileStream("Image_1.png", FileMode.Open);
else if (args.Uri == "Image_2.png")
args.ImageStream = new FileStream("Image_2.png", FileMode.Open);
//Retrieve the image from the website and use it.
else if (args.Uri.StartsWith("https://"))
{
WebClient client = new WebClient();
//Download the image as a stream.
byte[] image = client.DownloadData(args.Uri);
Stream stream = new MemoryStream(image);
//Set the retrieved image from the input Markdown.
args.ImageStream = stream;
}
}
private static void MdImportSettings_ImageNodeVisited(object sender, Syncfusion.Office.Markdown.MdImageNodeVisitedEventArgs args)
{
//Set the image stream based on the image name from the input Markdown.
if (args.Uri == "Image_1.png")
args.ImageStream = new FileStream("Image_1.png", FileMode.Open);
else if (args.Uri == "Image_2.png")
args.ImageStream = new FileStream("Image_2.png", FileMode.Open);
//Retrieve the image from the website and use it.
else if (args.Uri.StartsWith("https://"))
{
WebClient client = new WebClient();
//Download the image as a stream.
byte[] image = client.DownloadData(args.Uri);
Stream stream = new MemoryStream(image);
//Set the retrieved image from the input Markdown.
args.ImageStream = stream;
}
}
Private Shared Sub MdImportSettings_ImageNodeVisited(ByVal sender As Object, ByVal args As Syncfusion.Office.Markdown.MdImageNodeVisitedEventArgs)
'Set the image stream based on the image name from the input Markdown.
If args.Uri Is "Image_1.png" Then
args.ImageStream = New FileStream("Image_1.png", FileMode.Open)
ElseIf args.Uri Is "Image_2.png" Then
args.ImageStream = New FileStream("Image_2.png", FileMode.Open)
'Retrieve the image from the website and use it.
ElseIf args.Uri.StartsWith("https://") Then
Dim client As WebClient = New WebClient()
'Download the image as a stream.
Dim image As Byte() = client.DownloadData(args.Uri)
Dim stream As Stream = New MemoryStream(image)
'Set the retrieved image from the input Markdown.
args.ImageStream = stream
End If
End Sub
You can download a complete working sample from GitHub.
NOTE
Hook the event handler before opening a Word document as per the above code example.
Supported Markdown Syntax
Element |
Syntax |
Description |
Bold |
Sample content for **bold text**. |
For bold, add ** to front and back of the text. |
Italic |
Sample content for *Italic text*. |
For Italic, add * to front and back of the text. |
Bold and Italics |
Sample content for ***bold and Italic text***. |
For bold and Italics, add *** to the front and back of the text. |
Strikethrough |
Sample content for ~~strike through text~~. |
For strike through, add ~~ to front and back of the text. |
Subscript |
<sub>Subscript text</sub> |
For subscript, add <sub> to the front and </sub> to the back of the text. |
Superscript |
<sup>Superscript text</sup> |
For superscript, add <sup> to the front and </sup> to the back of the text. |
Heading 1 |
#Heading 1 content |
For heading 1, add # to start of the line. |
Heading 2 |
##Heading 2 content |
For heading 2, add ## to start of the line. |
Heading 3 |
###Heading 3 content |
For heading 3, add ### to start of the line. |
Heading 4 |
####Heading 4 content |
For heading 4, add #### to start of the line. |
Heading 5 |
#####Heading 5 content |
For heading 5, add ##### to start of the line. |
Heading 6 |
######Heading 6 content |
For heading 6, add ###### to start of the line. |
Block quotes |
>Block quotes text |
For block quotes, add>to start of the line. |
Code span |
`Code span text` |
For code span, add ` to front and back of the text. |
Indented code block |
4 spaces |
For indented code block, add 4 spaces at the beginning of line. |
Fenced code block |
``` |
For fenced code block, add ``` in the new line before and after the content. |
Ordered List |
1. First |
For ordered list, preceding the text with 1. (number with dot and one space) |
Unordered List |
- First |
For unordered list, preceding the text with – (hyphen and space). |
Links |
Link text without title text : |
For hyperlink, enclose the link text within the brackets [ ], and then enclose the URL as first parameter and title as second parameter within the parentheses(). |
Table |
Create a table using the pipes and underscores as given in the syntax to create 2 x 2 table. You can also set column alignments using the syntax below, default it is left aligned. Right alignment: |
|
Horizontal Line |
--- (three hyphen characters) |
For horizontal line, add --- (three hyphens) in a new line. |
Image |
![Alternate text] (URL path) |
For image, enclose an alternative text within the brackets [], and then link of the image source within parentheses (). If URL path is base64string, then it will be preserved properly in Word document. Otherwise, you can also |
Escape Character |
\(any syntax) |
Escape any markdown syntax by adding \ as prefix to the syntax. |
Online Demo
- Explore how to convert the Markdown file to Word document using the .NET Word Library (DocIO) in a live demo here.