C#: Convert Markdown to Word and PDF

2024-04-10 00:59:36 Written by  support iceblue
Rate this item
(0 votes)

Markdown, as a lightweight markup language, is favored by programmers and technical document writers for its simplicity, readability, and clear syntax. However, in specific scenarios, there is often a need to convert Markdown documents into Word documents with rich formatting capabilities and control over the layout or to generate PDF files suitable for printing and easy viewing. This article is going to demonstrate how to convert Markdown content into Word documents or PDF files with Spire.Doc for .NET to meet various document processing requirements in different scenarios.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Markdown Files to Word Documents with C#

With Spire.Doc for .NET, we can load a Markdown file using Document.LoadFromFile(string fileName, FileFormat.Markdown) method and then convert it to other formats using Document.SaveToFile(string fileName, fileFormat FileFormat) method.

Since images in Markdown files are stored as links, directly converting a Markdown file to a Word document is suitable for Markdown files that do not contain images. If the file contains images, further processing of the images is required after conversion.

Here are the steps to convert a Markdown file to a Word document:

  • Create an instance of Document class.
  • Load a Markdown file using Document.LoadFromFile(string fileName, FileFormat.Markdown) method.
  • Convert the file to a Word document and save it using Document.SaveToFile(string fileName, FileFormat.Docx) method.
  • C#
using Spire.Doc;

namespace MdToDocx
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an object of Document class
            Document doc = new Document();

            // Load a Markdown file
            doc.LoadFromFile("Sample.md", FileFormat.Markdown);

            // Convert the Markdown file to a Word document
            doc.SaveToFile("MarkdownToWord.docx", FileFormat.Docx);
            doc.Close();
        }
    }
}

C#: Convert Markdown to Word and PDF

Convert Markdown Files to PDF Files with C#

We can also directly convert Markdown files to PDF files by using the FileFormat.PDF Enum as the parameter. Here are the steps to convert a Markdown file to a PDF file:

  • Create an instance of Document class.
  • Load a Markdown file using Document.LoadFromFile(string fileName, FileFormat.Markdown) method.
  • Convert the file to a PDF file and save it using Document.SaveToFile(string fileName, FileFormat.Docx) method.
  • C#
using Spire.Doc;

namespace MdToDocx
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create an object of Document class
            Document doc = new Document();

            // Load a Markdown file
            doc.LoadFromFile("Sample.md", FileFormat.Markdown);

            // Convert the Markdown file to a PDF file
            doc.SaveToFile("MarkdownToPDF.pdf", FileFormat.PDF);
            doc.Close();
        }
    }
}

C#: Convert Markdown to Word and PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.