Marcostox.Markdig.Renderers.Docx 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Marcostox.Markdig.Renderers.Docx --version 1.0.3
                    
NuGet\Install-Package Marcostox.Markdig.Renderers.Docx -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Marcostox.Markdig.Renderers.Docx" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Marcostox.Markdig.Renderers.Docx" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Marcostox.Markdig.Renderers.Docx" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Marcostox.Markdig.Renderers.Docx --version 1.0.3
                    
#r "nuget: Marcostox.Markdig.Renderers.Docx, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Marcostox.Markdig.Renderers.Docx@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Marcostox.Markdig.Renderers.Docx&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Marcostox.Markdig.Renderers.Docx&version=1.0.3
                    
Install as a Cake Tool

Markdig-DocX

build Nuget

A Docx renderer for the excelent Markdig parser.

The renderer allows to output markdown text into an OpenXml-compliant word processing document (Microsoft Word's docx as an example).

So you transform something like this markdown into something like this Word document:

<img src="./docs/Screenshot_20.png" width=500/>

Main features:

  • Direct manipulation with the document using good old OpenXml SDK API
  • Ability to "inject" multiple markdown snippets into an existing document
  • All the formatting is done with styles providing great flexibility in visual appearance

Curently supported Markdown elements (to be extended in future):

  • Headings (up to the level 6)
  • Text formatting: emphasis, italic, strikethrough, ~subscript~, ^superscript^
  • Text highlighting: +added+, =marked=
  • Code blocks and code fences
  • External hyperlinks, both explicit and automatic
  • Block quotes
  • Ordered and bulleted lists (single- and multi-level)
  • Horizontal line breaks

Not currently supported:

  • Internal links (aka cross-references)
  • Pictures embedding
  • Various format extensions (tables, grids etc.)

Installation

Fetch from NuGet.

Install-Package Morincer.Markdig.Renderers.Docx 

How to Use

More usage scenarios are presented in Tests

Markdown snippets for supported constructs are also located there.

Example 1. Quick Render Using Standard Template and Styles

There is a standard template provided with the package - it is contains definitions of all the currently used styles. The names of the standard template's styles are defaulted in DocumentStyles class used for rendering.

Meanwhile you're free to override any of these, it's advised to start with the standard template and just alter it as per your needs - Word's styling sometimes is not trivial.

Please note, that although the standard template document contains some body text (to demonstrate the formatting) it is being cut on loading.

var document = DocxTemplateHelper.Standard; // Load standard template
var styles = new DocumentStyles(); // Use standard styles

// create a renderer (with null logger)
var renderer = new DocxDocumentRenderer(document, styles, NullLogger<DocxDocumentRenderer>.Instance);

// build pipeline if you need extra emphasis features
var pipeline = new MarkdownPipelineBuilder().UseEmphasisExtras().Build();
// Run the convertion
Markdown.Convert(markdownString, renderer, pipeline);  

Example 2. Load Your Own Template

If (when) you decided to use your own document for a template, you need to load it and prepare yourself.

There are couple of helper methods defined in DocxTemplateHelper class to make the life easier.

Namely:

  • LoadFromResource - load the document from an embedded resource, optionally cleaning it's body
  • CleanContents - remove everything from the document's body
  • FindParagraphContainingText - find a paragraph with certain text - to be used for insert point positioning.

After your document is loaded, you can define the exact position to insert the markdown contents (by default, the text is inserted after the last identified paragraph). You do this by interacting with the Cursor property of the renderer.

/* The template contains a paragraph with text INSERT to be used as a text position */
// Load document from resources
var document = DocxTemplateHelper.LoadFromResource("Markdig.Renderers.Docx.Tests.Resources.docx.template-to-insert.docx");

// Find paragraph to be used as an insert position
var paragraph = DocxTemplateHelper.FindParagraphContainingText(document, "INSERT");

// Instantiate renderer with default styles and no logger        
var renderer = new DocxDocumentRenderer(document);
// set insert position to be after the paragraph found above
renderer.Cursor.SetAfter(paragraph);
// Do the rendering
Markdown.Convert("**Markdown Paragraph 1 - bold** text\n\nParagraph2", renderer);
// Remove the insert position paragraph from the document        
paragraph!.Remove();

Styling

All the default styles used are listed in the standard template, these styles are all prefixed with "MD ".

It is recommended just stick to these styles in your own documents (just copy-paste-then-delete text from the standard template to inject styles).

If you decided to change the styling, please note the following:

  • You'll need to set properties in DocumentStyles to match your style names
  • Be careful when defining styles for lists - they're really tricky in MS Word and quite easy to mess.

Converti Markdown in DOCX direttamente in memoria

Se vuoi ottenere un file DOCX direttamente in memoria (ad esempio per inviarlo come risposta HTTP o salvarlo senza scrivere su disco), puoi usare il metodo helper:

using Markdig.Renderers.Docx;
using System.IO;

// Converte il markdown in un MemoryStream che contiene il DOCX
var docxStream = MarkdownExtensions.ToDocxStream("# Titolo\n\nTesto di esempio");

// Ora puoi salvare il file dove vuoi, ad esempio:
using (var file = File.Create("output.docx"))
{
    docxStream.CopyTo(file);
}

Puoi anche opzionalmente passare i tuoi DocumentStyles o una pipeline personalizzata:

using Markdig;
using Markdig.Renderers.Docx;

// Stili personalizzati e/o pipeline personalizzata
var styles = new DocumentStyles();
// Ad esempio: styles.Heading1 = "MyCustomHeading1";

var pipeline = new MarkdownPipelineBuilder()
    .UseEmphasisExtras()
    .Build();

var docxStream = MarkdownExtensions.ToDocxStream("# Titolo", styles, pipeline);
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 148 6/5/2025
1.0.4 144 6/5/2025
1.0.3 152 6/5/2025
1.0.1 147 6/5/2025