Convert Word Documents to Markdown and Vice Versa in C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Convert Word Documents to Markdown and Vice Versa in C#

Convert Word Documents to Markdown and Vice Versa in C#

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.

Streamline your Word document workflow effortlessly with Syncfusion’s robust Word Library.

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;

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);
        }
    }
}

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

Get insights into the Syncfusion’s Word Library and its stunning array of features with its extensive documentation.

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);
        }
    }
}

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++;
}
Saving images in a separate folder during conversion
Saving images in a separate folder during conversion

Witness the power of the Syncfusion Word Library in action with the examples featuring its dynamic functionalities.

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);
        }
    }
}
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);
        }
    }
}

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

Syncfusion’s high-performance Word Library lets you create, edit, and manage Word documents without Microsoft Office or interop dependencies.

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.
  • Mergesplit, and organize Word documents.
  • Convert Word documents into HTMLRTF, PDFimages, 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 forumsupport portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed