Convert Word Documents to Markdown and Vice Versa in C#

Jollen Moyani - Aug 17 '23 - - Dev Community

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 (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 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.

Step 2: Then, install the Syncfusion.DocIO.Net.Core NuGet package as a reference to the app from NuGet Gallery.

Include the following namespaces in the Program.cs file.

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
Enter fullscreen mode Exit fullscreen mode

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.

//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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Converting Word document to Markdown

Converting Word document to Markdown

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 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.

//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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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++;
}
Enter fullscreen mode Exit fullscreen mode

Saving images in a separate folder during conversion

Saving images in a separate folder during conversion

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.

//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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Converting Markdown to a Word document

Converting Markdown to a Word document

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 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.

//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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Inserting images into the resultant Word document

Inserting images into the resultant Word document

GitHub reference

You can find all the examples for converting Word documents to Markdown and vice versa using the Syncfusion .NET Core Word Library 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. Take a moment to peruse our Word-to-Markdown and 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.
  • Merge, split, and organize Word documents.
  • Convert Word documents into HTML, RTF, PDF, images, and other formats.

You can find more Word Library examples at this GitHub location.

If you are new to our Word Library, following our Getting Started guide is highly recommended.

Are you already a Syncfusion user? You can download the product setup here. If you’re not yet a Syncfusion user, you can download a 30-day free trial.

If you have questions, contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Related blogs

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .