Convert Word Documents to Markdown and Vice Versa in C#
Tags: csharp, word, fileformats, dotnetcore
Published: 2023-08-17 07:39:24

---
title: Convert Word Documents to Markdown and Vice Versa in C#
published: true
date: 2023-08-16 11:45:51 UTC
tags: csharp, word, fileformats, dotnetcore
canonical_url: https://www.syncfusion.com/blogs/post/convert-word-to-markdown-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41af43k491wvscf4v5hv.png
---
[Markdown](https://en.wikipedia.org/wiki/Markdown "Wikipedia Link: Markdown") has become a versatile and widely embraced markup language, offering flexibility and ease of use for various purposes. Sometimes, the need arises to convert Word documents to Markdown and vice versa. Converting Word documents to Markdown enables the seamless publishing of online documentation while converting Markdown to Word documents facilitates efficient collaboration and content editing with colleagues.

This blog post aims to demonstrate how to convert Word documents to Markdown and vice versa in C# using the Syncfusion [.NET Core Word Library](https://www.syncfusion.com/document-processing/word-framework/net-core/word-library ".NET Core Word Library") (DocIO) in the code. Its powerful and user-friendly features allow us to create, read, edit, and convert Word documents to various formats, including Markdown. The best part is that it doesn’t require [Microsoft Word](https://en.wikipedia.org/wiki/Microsoft_Word "Wikipedia Link: Microsoft Word") or any interop dependencies, simplifying the conversion process even further.

## Getting started

**Step 1:** Create a new C# .NET Core  **Console App**  in [Visual Studio](https://visualstudio.microsoft.com/ "Visual Studio").

**Step 2:**  Then, install the [Syncfusion.DocIO.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core "Syncfusion.DocIO.Net.Core NuGet package") NuGet package as a reference to the app from [NuGet Gallery](https://www.nuget.org/ "NuGet Gallery").

Include the following namespaces in the **Program.cs** file.

```csharp
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
```

The project is ready! Let’s write the code to convert a Word document to Markdown and vice versa.

## Convert a Word document to Markdown

Refer to the following code example to convert a Word document into Markdown.

```csharp
//Open a file as a stream.
using (FileStream fileStreamPath = new FileStream("Input.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    //Load an existing Word document.
    using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
    {
        //Create a file stream.
        using (FileStream outputFileStream = new FileStream("WordToMarkdown.md", FileMode.Create, FileAccess.ReadWrite))
        {
            //Save a Markdown file to the file stream.
            document.Save(outputFileStream, FormatType.Markdown);
        }
    }
}
```

We will get output like in the following screenshot by executing the previous code example.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/08/Converting-Word-document-to-Markdown.png" alt="Converting Word document to Markdown" style="width:100%">

  <figcaption>Converting Word document to Markdown</figcaption>

</figure>

## Customize image saving

When converting Word documents to Markdown, it’s essential to address image handling. Following are some tips to customize image-saving during the Word-to-Markdown conversion process.

### Save images as Base64

The .NET Core Word Library automatically saves images in Markdown files as Base64 strings when converting them to Word documents. This approach ensures the images are embedded within the Markdown document, simplifying sharing and management.

### Save images as separate files

Instead of embedding images within the Markdown document, you can save them as individual files. This approach offers more flexibility, allowing easy modification or replacement of images without altering the Markdown content. By adopting this method, you ensure a clear separation between the Markdown content and the image files, simplifying image management.

To implement this approach, you can utilize the [ImageNodeVisited](https://www.syncfusion.com/blogs/post/export-blazor-rich-text-editor-to-pdf.aspx "ImageNodeVisited event of Word Library") event. This event is triggered for each image node in the Word document. In the event handler, you can customize the image path in the output Markdown file. For example, you can save the images to a designated folder or upload them to the server and use that web URL in the Markdown.

Following is an example illustrating how to utilize the **ImageNodeVisited** event to save images as separate files.

```csharp
//Open a file as a stream.
using (FileStream fileStreamPath = new FileStream("Input.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    //Load an existing Word document.
    using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
    {
        //Hook the event to customize the image. 
        document.SaveOptions.ImageNodeVisited += SaveImage;
        //Create a file stream.
        using (FileStream outputFileStream = new FileStream("WordToMarkdown.md", FileMode.Create, FileAccess.ReadWrite))
        {           
            //Save a Markdown file to the file stream.
            document.Save(outputFileStream, FormatType.Markdown);
        }
    }
}
```

The following C# code illustrates the event handler to customize the image path and save the image in an external folder.

```csharp
static int imageCount = 0;
static void SaveImage(object sender, ImageNodeVisitedEventArgs args)
{
    //Image path to save the images in an external folder.
    string imagepath = @"E:\WordToMD\Image_" + imageCount + ".png";
    //Save the image stream as a file. 
    using (FileStream fileStreamOutput = File.Create(imagepath))
        args.ImageStream.CopyTo(fileStreamOutput);
    //Set the image URI to be used in the output Mmarkdown.
    args.Uri = imagepath;
    imageCount++;
}
```
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/08/Saving-images-in-a-separate-folder-during-conversion.png" alt="Saving images in a separate folder during conversion" style="width:100%">

  <figcaption>Saving images in a separate folder during conversion</figcaption>

</figure>

## Convert Markdown to a Word document

Consider a scenario where you’re developing a documentation management system for a software project. The system allows for collaboration using Markdown. However, stakeholders often require the final documents in Microsoft Word for further editing and formatting. In this case, you want to convert in the opposite direction. Let’s see how to do that with the .NET Core Word Library.

Refer to the following code snippet.

```csharp
//Open a file as a stream.
using (FileStream fileStreamPath = new FileStream("Input.md", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    //Load an existing Markdown file.
    using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Markdown))
    {
        //Create a file stream.
        using (FileStream outputFileStream = new FileStream("MarkdownToWord.docx", FileMode.Create, FileAccess.ReadWrite))
        {
            //Save a Word document to the file stream.
            document.Save(outputFileStream, FormatType.Docx);
        }
    }
}
```
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/08/Converting-Markdown-to-a-Word-document.png" alt="Converting Markdown to a Word document" style="width:100%">

  <figcaption>Converting Markdown to a Word document</figcaption>

</figure>

### Customize image data

When converting Markdown to a Word document using the .NET Word Library, you have the flexibility to modify the images included in the input Markdown file. You can also download images listed as URLs in the Markdown file and insert them into the resulting Word document.

The [ImageNodeVisited](https://help.syncfusion.com/cr/file-formats/Syncfusion.Office.Markdown.MdImportSettings.html#Syncfusion_Office_Markdown_MdImportSettings_ImageNodeVisited "ImageNodeVisited event of Word Library") event allows you to customize image data during the import process of a Markdown file. You can implement custom logic to handle image modifications by leveraging this event. Refer to the following code example to achieve this.

```csharp
//Open a file as a stream.
using (FileStream fileStreamPath = new FileStream("Input.md", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    //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 an existing Markdown file.
        document.Open(fileStreamPath, FormatType.Markdown);
        //Create a file stream.
        using (FileStream outputFileStream = new FileStream("MarkdownToWord.docx", FileMode.Create, FileAccess.ReadWrite))
        {
            //Save a Word document to the file stream.
            document.Save(outputFileStream, FormatType.Docx);
        }
    }
}
```

The following C# code illustrates the use of the event handler to customize the image based on the source path.

```csharp
private static void MdImportSettings_ImageNodeVisited(object sender, Syncfusion.Office.Markdown.MdImageNodeVisitedEventArgs args)
{
    //Retrieve the image from the local machine file path and use it.
    if (args.Uri == "Road-550.png")
        args.ImageStream = new FileStream(args.Uri), 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;
    }
    //Retrieve the image from the Bbase64 string and use it.
    else if (args.Uri.StartsWith("data:image/"))
    {
        string src = args.Uri;
        int startIndex = src.IndexOf(",");
        src = src.Substring(startIndex + 1);
        byte[] image = System.Convert.FromBase64String(src);
        Stream stream = new MemoryStream(image);
        //Set the retrieved image from the input Markdown.
        args.ImageStream = stream;
    }
}
```
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/08/Inserting-images-into-the-resultant-Word-document.png" alt="Inserting images into the resultant Word document" style="width:100%">

  <figcaption>Inserting images into the resultant Word document</figcaption>

</figure>

## GitHub reference

You can find all the examples for [converting Word documents to Markdown and vice versa using the Syncfusion .NET Core Word Library](https://github.com/SyncfusionExamples/markdown-conversion-in-csharp "Word to Markdown Conversion in C# GitHub demo") in the GitHub repository.

## Conclusion

In this blog, we have seen how to convert Word documents into Markdown and vice versa using the [Syncfusion .NET Core Word Library](https://www.syncfusion.com/document-processing/word-framework/net-core ".NET Core Word Library"). Take a moment to peruse our [Word-to-Markdown](https://help.syncfusion.com/file-formats/docio/convert-word-document-to-markdown-in-csharp "Word to Markdown conversion") and [Markdown-to-Word conversion](https://help.syncfusion.com/file-formats/docio/convert-markdown-to-word-document-in-csharp "Markdown to Word conversion") documentation, where you’ll find other options and features, all with accompanying code examples.

Apart from this conversion functionality, our Syncfusion .NET Word Library has the following significant functionalities:

- Create, read, and edit Word documents programmatically.
- Create complex reports by merging data into a Word template from various data sources through [mail merge](https://help.syncfusion.com/file-formats/docio/working-with-mail-merge "Working with Mail merge").
- [Merge](https://help.syncfusion.com/file-formats/docio/word-document/merging-word-documents "Merging Word documents"), [split](https://help.syncfusion.com/file-formats/docio/word-document/split-word-documents "Split Word documents"), and organize Word documents.
- Convert Word documents into [HTML](https://help.syncfusion.com/file-formats/docio/html "Word to HTML and HTML to Word conversions"), [RTF](https://help.syncfusion.com/file-formats/docio/rtf "RTF conversions in Word Library"), [PDF](https://help.syncfusion.com/file-formats/docio/word-to-pdf "Word document to PDF conversion"), [images](https://help.syncfusion.com/file-formats/docio/word-to-image "Rendering / Converting Word document to Image in Word Library"), and other formats.

You can find more Word Library examples at this [GitHub location](https://github.com/SyncfusionExamples/DocIO-Examples "Syncfusion Word Library GitHub demos").

If you are new to our Word Library, following our [Getting Started guide](https://help.syncfusion.com/file-formats/docio/getting-started "Getting started with simple word document") is highly recommended.

Are you already a Syncfusion user? You can download the product setup [here](https://www.syncfusion.com/account/downloads/studio/ "Essential Studio License and Downloads page"). If you’re not yet a Syncfusion user, you can download a [30-day free trial](https://www.syncfusion.com/downloads/fileformats/confirm "Get free evaluation of the Essential Studio products").

If you have questions, contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/word "Syncfusion Feedback Portal"). We are always happy to assist you!

## Related blogs

- [What’s New in 2023 Volume 2: File-Format Libraries](https://www.syncfusion.com/blogs/post/whats-new-2023-volume-2-file-format-libraries.aspx "Blog: What’s New in 2023 Volume 2: File-Format Libraries")
- [Restrict Editing of Word Documents Based on User in a Web Application](https://www.syncfusion.com/blogs/post/restrict-editing-of-word-documents-based-on-user-in-a-web-application.aspx "Blog: Restrict Editing of Word Documents Based on User in a Web Application")
- [3 Easy Ways to Convert Word Documents to Images in C#](https://www.syncfusion.com/blogs/post/convert-word-documents-to-images-in-csharp.aspx "Blog: 3 Easy Ways to Convert Word Documents to Images in C#")
- [Create and Update Table of Contents in Word Documents in .NET Core [C#]](https://www.syncfusion.com/blogs/post/create-and-update-table-of-contents-in-word-documents-in-net-core-c.aspx "Blog: Create and Update Table of Contents in Word Documents in .NET Core [C#]")