DEV Community

Jeremy K.
Jeremy K.

Posted on

Convert Markdown to PDF in C#

In daily development, we often need to convert lightweight Markdown documents into fixed-format, easily shareable PDF files. This article details how to efficiently convert Markdown to PDF using C# code with the Spire.Doc for .NET library, covering core steps and advanced examples.


I. Quick Environment Setup

Spire.Doc for .NET supports .NET Framework/.NET Core/.NET 5+, with cross-platform compatibility for Windows/Linux/macOS. The core installation method is via NuGet:

# NuGet Command Line Installation
Install-Package Spire.Doc
Enter fullscreen mode Exit fullscreen mode

II. Basic Conversion: Core Principles and Code Breakdown

1. Core Logic

The underlying mechanism of Markdown-to-PDF conversion in Spire.Doc follows three key steps: "Parse Markdown → Build Document Model → Export to PDF". It relies on two core objects: Document (document container) and FileFormat (format identifier). The workflow is as follows:

Markdown File → LoadFromFile → Document Model → SaveToFile(PDF)
Enter fullscreen mode Exit fullscreen mode

2. Complete Basic Code (with Line-by-Line Comments)

using Spire.Doc;

namespace MdToDocx
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize Document (core container to hold all document content)
            Document doc = new Document();

            // Load Markdown file: Specify FileFormat.Markdown to trigger syntax parsing
            doc.LoadFromFile("Sample.md", FileFormat.Markdown);

            // Export to PDF: Specify FileFormat.PDF; the underlying engine automatically renders the model to PDF
            doc.SaveToFile("MarkdownToPDF.pdf", FileFormat.PDF);
            doc.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Core API Details

  • Document: Creates the document container; all Markdown parsing and PDF export operations are based on this object.
  • LoadFromFile: The second parameter FileFormat.Markdown enables parsing of core Markdown syntax (headings, lists, tables, etc.).
  • SaveToFile: Exports the document to PDF format, automatically handling line breaks, indentation, and other styling.

III. Advanced Scenario: Custom PDF Page Settings

When converting Markdown to PDF, you can customize the output PDF's page size, orientation, and margins using PageSetup:

using Spire.Doc;

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

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

            // Page configuration (Section corresponds to a "page object" in PDF; multiple Sections support columns/pagination)
            Section section = doc.Sections[0]; // Fixed: Corrected "document" to "doc" for consistency
            // Page size: A4/Letter/A3
            section.PageSetup.PageSize = PageSize.A4;
            // Page orientation: Portrait/Landscape
            section.PageSetup.Orientation = PageOrientation.Portrait;
            // Margins: Top/Bottom/Left/Right (unit: points)
            section.PageSetup.Margins.Top = 17.9f;
            section.PageSetup.Margins.Bottom = 17.9f;
            section.PageSetup.Margins.Left = 17.9f;
            section.PageSetup.Margins.Right = 17.9f;

            // Export to PDF format
            doc.SaveToFile("MarkdownToPDF.pdf", FileFormat.PDF);
            doc.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

With the examples above, you can easily convert Markdown documents to high-quality PDF files. This article covers the complete workflow from basic conversion to advanced configuration, making it simple to handle both single-document and batch conversion tasks.

Top comments (0)