1. Products
  2.   Word Processing
  3.   Swift
  4.   MarkdownToDocx
 
  

Convert Markdown Files to Word Files via Free Swift Library

Open Source Swift Library to Convert Markdown Files to Word DOCX Documents with Text, Tables, Images, Lists, Fonts and Custom Formatting Support.

What is MarkdownToDocx?

In today's fast-paced development world, the ability to seamlessly convert content between formats is a superpower. Markdown has become the universal language for writing documentation, blog posts, and readmes due to its simplicity. Converting Markdown files to Microsoft Word format has never been easier for Swift developers. The MarkdownToDocx library offers a powerful, flexible solution for transforming plain text Markdown documents into professionally formatted DOCX files. This comprehensive guide explores everything you need to know about this essential Swift library. Beyond basic formatting, the library handles more complex Markdown features. Strikethrough text renders with proper formatting, blockquotes appear with distinctive styling, and tables convert with full structure preservation including headers and data cells.

MarkdownToDocx is an open-source Swift library that seamlessly converts Markdown documents into Microsoft Word DOCX format. Built on Apple's MarkdownToDocx parser and leveraging ZIPFoundation for archive creation, this library bridges the gap between simple text formatting and professional document generation. The library excels in its simplicity while offering advanced customization options for developers who need granular control over document styling. Whether you're building a note-taking app, documentation generator, or content management system, MarkdownToDocx provides the tools you need.

Previous Next

Getting Started with MarkdownToDocx

The recommended way to install MarkdownToDocx is using GitHub, Please use the following command for a smooth installation.

Install MarkdownToDocx via GitHub

$ go get https://github.com/riyadshauk/markdown-docx-swift.git   

Adding MarkdownToDocx to your project takes just a few steps. Open your Package.swift file and add the dependency:

Add the following Dependency to the Dependencies Argument

dependencies: [
    .package(url: "https://github.com/riyadshauk/markdown-docx-swift.git", from: "1.0.0")
]

Converting Markdown Files to DOCX via Swift

The open source MarkdownToDocx library has included support for loading and converting markdown files to Word DOCX files using Swift commands. You can also convert Markdown files directly without loading them into memory first. This approach is more efficient for large files since it doesn't require loading the entire file into a string first. Here is a simple example that demonstrates how software developers can convert a markdown file to Word DOCX file using Swift commands.

How to Convert Markdown Files to DOCX using Swift Library?

import MarkdownToDocx

let inputURL = FileManager.default.urls(
    for: .documentDirectory, 
    in: .userDomainMask
)[0].appendingPathComponent("input.md")

let outputURL = FileManager.default.urls(
    for: .documentDirectory, 
    in: .userDomainMask
)[0].appendingPathComponent("output.docx")

do {
    let converter = MarkdownToDocxConverter()
    let docxData = try converter.convert(markdownFile: inputURL)
    try docxData.write(to: outputURL)
    print("File converted successfully!")
} catch {
    print("Conversion error: \(error)")
}

Text Formatting Support

The MarkdownToDocx library handles all essential text formatting options. Bold text, italic text, and strikethrough formatting are fully supported. Inline code blocks are also rendered correctly, maintaining the monospace formatting that distinguishes code from regular text. The following simple code example demonstrates how easy it is to convert formatted text. The converter automatically recognizes Markdown syntax and applies the corresponding formatting in the output DOCX file.

How to Convert Formatted Text in Word DOCX via Swift Library?

let markdown = """
This is **bold text** and this is *italic text*.
You can also use ~~strikethrough~~ formatting.
Here's some `inline code` in the middle of a sentence.
"""

let converter = MarkdownToDocxConverter()
let docxData = try converter.convert(markdown: markdown)
try docxData.write(to: outputURL)

Convert Nested Markdown Lists via Swift Library

The open source MarkdownToDocx library has provided complete support for handling indented bullet-point and numbered lists in Markdown file inside Swift apps. Both bullet lists and numbered lists work seamlessly. The library properly handles nested lists, maintaining the correct indentation and numbering schemes. The indentation property in the ListStyles configuration allows you to adjust how nested items are positioned, ensuring your lists look exactly the way you want them. Here is a simple example that shows how to do it.

How to Convert Nested Markdown Lists via Swift Library?

let markdown = """
## Shopping List

- Fruits
  - Apples
  - Bananas
  - Oranges
- Vegetables
  - Carrots
  - Broccoli

## Steps to Follow

1. First step
2. Second step
   1. Sub-step one
   2. Sub-step two
3. Third step
"""

let converter = MarkdownToDocxConverter()
let docxData = try converter.convert(markdown: markdown)

Render Markdown Tables via Swift Library

Table support is one of the standout features. The MarkdownToDocx library correctly interprets Markdown tables and renders them with proper borders and cell formatting inside Swift applications. Tables maintain their structure perfectly, with customizable borders, cell padding, and text alignment options available through the styling configuration. The following example demonstrates, how to use the library to read Markdown tables and renders them with proper borders and cell formatting.

How to Render Markdown Tables via Swift Library?

let markdown = """
| Feature | Status | Notes |
|---------|--------|-------|
| Headings | ✅ | All levels supported |
| Lists | ✅ | Nested lists work |
| Tables | ✅ | Full support |
| Images | ✅ | Basic support |
"""

let converter = MarkdownToDocxConverter()
let docxData = try converter.convert(markdown: markdown)






















Parse Markdown Documents via Swift API