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 for ASP.NET is required for this tutorial.
- Download Trial Version
Setup download and installation required.
Creating the Application
-
In Visual Studio, create a new Console App.
-
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.
-
Switch the Package source to Text Control Offline Packages and install the package TXTextControl.TextControl.ASP.SDK.
-
Create a new class named TXMarkdown and paste the following code into the class:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersusing 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; } } } } -
Now, you can use this class in your application to convert any Markdown file to an Office Open XML (DOCX) file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersvar 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. | |