Products Technologies Demo Docs Blog Support Company

Converting Markdown to DOCX and PDF in C#

Markdown is a popular, lightweight markup language to create formatted text in a simple way. This article shows how to convert Markdown files to Office Open XML and PDF documents using TX Text Control and Markdig - a powerful and extensible Markdown processor for .NET.

Converting Markdown to DOCX and PDF in C#

Markdown is a popular, lightweight markup language to create formatted text in a simple way. Specifically for developers, it became a standard to describe projects such as in Readme files in GitHub repositories and other resources.

This article shows how convert Markdown files to Office Open XML and PDF documents using TX Text Control and Markdig - a powerful and extensible Markdown processor for .NET.

Use the following steps to create a console application to convert Markdown to Office Open XML and PDF documents.

Prerequisites

A trial version of TX Text Control .NET Server is required for this tutorial.

Creating the Application

  1. In Visual Studio, create a new Console App.
    Markdown to DOCX

  2. In the Solution Explorer, select the created project and choose Manage NuGet Packages... from the Project main menu. Set the Package source to nuget.org and search for Markdig. Select the package Markdig and click Install.
    Markdown to DOCX

  3. Switch the Package source to Text Control Offline Packages and install the package TXTextControl.TextControl.ASP.SDK.
    Markdown to DOCX

  4. Create a new class named TXMarkdown and paste the following code into the class:

    using Markdig;
    
    namespace TXTextControl {
            public static class Markdown {
                    public static byte[] Convert(string markdown, BinaryStreamType binaryStreamType) {
    
             var pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
             var htmlString = Markdig.Markdown.ToHtml(markdown, pipeline);
    
             using (ServerTextControl tx = new ServerTextControl()) {
                tx.Create();
    
                tx.Load(htmlString, StringStreamType.HTMLFormat);
    
                byte[] results;
                tx.Save(out results, binaryStreamType);
    
                return results;
             }
          }
       }
    }
  5. Now, you can use this class in your application to convert any Markdown file to an Office Open XML (DOCX) file:

    var mdFile = System.IO.File.ReadAllText("sample.md");
    
    byte[] document = TXTextControl.Markdown.Convert(
      mdFile, 
      TXTextControl.BinaryStreamType.WordprocessingML);

The converted results of the following sample Markdown file can be seen in the screenshot below:

---
__Advertisement__

- __[pica](https://nodeca.github.io/pica/demo/)__ - high quality and fast image
  resize in browser.
- __[babelfish](https://github.com/nodeca/babelfish/)__ - developer friendly
  i18n with plurals support and easy syntax.

You will like those projects!

---

# h1 Heading
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading


## Horizontal Rules

___

---

***

## Emphasis

**This is bold text**

__This is bold text__

*This is italic text*

_This is italic text_

~~Strikethrough~~


## Blockquotes


> Blockquotes can also be nested...
>> ...by using additional greater-than signs right next to each other...
> > > ...or with spaces between arrows.


## Lists

Unordered

+ Create a list by starting a line with `+`, `-`, or `*`
+ Sub-lists are made by indenting 2 spaces:
  - Marker character change forces new list start:
    * Ac tristique libero volutpat at
    + Facilisis in pretium nisl aliquet
    - Nulla volutpat aliquam velit
+ Very easy!

Ordered

1. Lorem ipsum dolor sit amet
2. Consectetur adipiscing elit
3. Integer molestie lorem at massa


1. You can use sequential numbers...
1. ...or keep all the numbers as `1.`

Start numbering with offset:

57. foo
1. bar

## Tables

| Option | Description |
| ------ | ----------- |
| data   | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext    | extension to be used for dest files. |

Right aligned columns

| Option | Description |
| ------:| -----------:|
| data   | path to data files to supply the data that will be passed into templates. |
| engine | engine to be used for processing templates. Handlebars is the default. |
| ext    | extension to be used for dest files. |
Markdown to DOCX

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…


ActiveXASP.NETWindows Forms

Expert Implementation Services for Legacy System Modernization

We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.


ActiveXASP.NETWindows Forms

Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5

TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…