Free Swift Library to Read & Write DOCX Documents

Open Source Swift API That allows iOS & macOS Developers to Read, Write & Manipulate .docx Files with Smart Fonts Handling, Paragraphs and Headings Support.

What is DOCX Library?

Working with Microsoft Word documents in Swift has traditionally been a challenge for software developers due to limited native support. However, the open source DOCX Swift API offers a practical and lightweight solution. This Swift-based library enables software developers to programmatically create, read, modify, and manage .docx files without relying on Microsoft Office or external tools. Unlike bulky office automation suites or cloud-based Word document solutions, this library keeps things simple and efficient, making it a great fit for lightweight document generation on iOS, iPadOS, macOS, and server-side Swift apps. It supports core features like adding paragraphs, text runs, headings, images, and simple formatting.

The open-source DOCX Swift API empowers Swift developers to integrate Word document generation directly into their apps. The API has provided several benefits such as cross-platform swift compatibility, lightweight and dependency free, offline DOCX generation and several more. One of the key advantages of the Open Source DOCX Swift API is its open source nature, which means that developers have full access to the source code and can modify it to suit their specific needs. This level of customization allows developers to tailor the API to their unique requirements, ensuring that their apps are equipped with the features and capabilities necessary to stand out in a competitive market. With this API, users can effortlessly read, write, and manipulate DOCX files, enabling them to access and modify the content of documents with precision and flexibility.

Previous Next

Getting Started with DOCX

The recommended way DOCX into your project is by using GitHub. Please use the following command for a smooth installation.

Install DOCX Library via CocoaPods

Install DOCX via GitHub 

Install DOCX via GitHub

go get github.com/shinjukunian/DocX.git 

Create DOCX Documents from Scratch

With the open source DocX Swift library, software developers can generate .docx files from the ground up with ease. This includes adding paragraphs, headings, image, styles and inline text elements. Developers can easily define the content structure with an intuitive, Swift-native API. The following code example demonstrates how to create a new Word document and adds a simple paragraph saying "Hello from DocX Swift!" inside Swift applications.

How to Create New Word Document with Simple Paragraph via Swift API?

import DocX

let doc = Document()

let p = Paragraph()
p.appendChild(Text("Hello from DocX Swift!"))
doc.appendChild(p)

try doc.save(to: URL(fileURLWithPath: "MyDocument.docx"))

Smart Font Handling & Page Layout Control

To ensure cross-platform compatibility, developers can configure DocX to use standard Word processing fonts instead of embedding specific font names. This prevents issues where a document might look different on a machine that doesn't have the original font installed. Moreover, with PageDefinition, you can specify the page size and margins of your document. The library includes presets for standard sizes like A4 and US Letter, but also allows for completely custom dimensions and margins, giving you precise control over the final layout. Here is a simple example that shows how to defining a custom page size inside Swift applications.

How to Defining a Custom Page Size via Swift API?

import Foundation

// 1. Your attributed string
let string = NSAttributedString(string: "This content will be on a custom-sized page.")

// 2. Define a custom page layout
let customPage = PageDefinition(
    pageSize: .init(
        width: Measurement(value: 8, unit: .inches),
        height: Measurement(value: 5, unit: .inches)
    ),
    pageMargins: .init(
        top: .init(value: 0.5, unit: .inches),
        bottom: .init(value: 0.5, unit: .inches),
        left: .init(value: 1, unit: .inches),
        right: .init(value: 1, unit: .inches)
    )
)

// 3. Apply the page definition via DocXOptions
var options = DocXOptions()
options.pageDefinition = customPage

// 4. Write the document
let url = URL(fileURLWithPath: "/path/to/your/CustomPage.docx")
try string.writeDocX(to: url, options: options)

Comprehensive Attribute Support

The Swift DocX library supports a wide array of text attributes, ensuring that your carefully crafted rich text is faithfully represented in the final .docx file. This includes, fonts and colors, Paragraph styles, character styles, embedding images, and many more. It also supports Furigana (Ruby Annotations) features which is a standout feature for developers working with Japanese text, as it correctly renders phonetic guides.