Convert Markdown to DOCX – C# Examples

Markdown is a markup language with a plain-text-formatting syntax. Markdown is often used as a format for documentation and readme files since it allows writing in an easy-to-read and easy-to-write style. Its design allows it to be easily converted to many output formats, but initially, it was created to convert only to HTML. Using the Aspose.HTML class library in your C# application, you can easily convert Markdown into a DOCX file with just a few lines of code!

This article provides information on how to convert Markdown to DOCX using the Aspose.HTML for .NET API. You will learn about the supported Markdown to DOCX conversion scenarios and consider C# examples to illustrate them. Also, you can try an Online Markdown Converter to test the Aspose.HTML API functionality and convert Markdown on the fly.

Online Markdown Converter

You can convert Markdown to other formats with Aspose.HTML for .NET API in real time. Please load a Markdown file from the local file system, select the output format and run the example. The save options are set by default. You will immediately receive the conversion result as a separate file.

                
            

If you want to convert Markdown to DOCX programmatically, please see the following C# code examples.

Convert Markdown to DOCX in C#

Conversions from Markdown to other formats go through the Markdown to HTML conversion stage. If your scenario is required converting Markdown document, for instance, to the DOCX file format, the following example demonstrates how that is simple:

  1. Prepare a source Markdown document. In the example, we create a Markdown file from code.
  2. Prepare a path for converted DOCX file saving.
  3. Convert Markdown to HTML. Use the ConvertMarkdown(sourcePath) method to save Markdown as an HTML document.
  4. Use the ConvertHTML() method. You need to pass the HTMLDocument, DocSaveOptions, and output file path to the ConvertHTML() method for HTML to DOCX conversion.

If your case is to create a Markdown document from a user string directly in your code and convert it to a DOCX file, the following example could help you:

 1// Prepare a path to a source Markdown file
 2string sourcePath = Path.Combine(OutputDir, "document.md");
 3
 4// Prepare a simple Markdown example
 5var code = "### Hello, World!" +
 6           "\r\n" +
 7           "Convert Markdown to DOCX!";
 8
 9// Create a Markdown file
10File.WriteAllText(sourcePath, code);
11
12// Prepare a path to save the converted file
13string savePath = Path.Combine(OutputDir, "document-output.docx");
14
15// Convert Markdown to HTML document
16using var document = Converter.ConvertMarkdown(sourcePath);
17
18// Convert HTML document to DOCX file format
19Converter.ConvertHTML(document, new DocSaveOptions(), savePath);

Convert Markdown to DOCX using DocSaveOptions

If your case is to convert an existing Markdown document from a local file system, the following example could help you. You need to follow a few steps:

  1. Open an existing Markdown document. In the example, we load a Markdown file from a local file system ( nature.md).
  2. Prepare a path for converted DOCX file saving.
  3. Convert Markdown to HTML. Use the ConvertMarkdown(sourcePath) method of the Converter class to save Markdown as an HTML document.
  4. Create a new DocSaveOptions object and specify the required properties.
  5. Use the ConvertHTML() method. You need to pass the HTMLDocument, DocSaveOptions, and output file path to the ConvertHTML() method.

The following code snippet shows how to convert Markdown to DOCX with custom save options:

 1// Prepare a path to a source Markdown file
 2string sourcePath = Path.Combine(DataDir, "nature.md");
 3
 4// Prepare a path for converted DOCX file saving 
 5string savePath = Path.Combine(OutputDir, "nature-output.docx");
 6
 7// Convert Markdown to HTML 
 8using var document = Converter.ConvertMarkdown(sourcePath);
 9
10// Initialize DocSaveOptions. Set up the page-size 500x1000 pixels and margins 
11var options = new DocSaveOptions();
12options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 1000), new Margin(20, 20, 10, 10));
13
14// Convert the HTML document to DOCX file format
15Converter.ConvertHTML(document, options, savePath);

The DocSaveOptions class provides properties that give you full control over a wide range of parameters and improve the process of converting Markdown to DOCX format. In the example, we use the PageSetup property that specifies the page size of the DOCX document. To learn more about DocSaveOptions, please read the Fine-Tuning Converters article.

Aspose.HTML offers a free online MD to DOCX Converter that converts Markdown to DOCX file with high quality, easy and fast. Just upload, convert your files and get results in a few seconds!

Text “MD to DOCX Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.