Idiomatic Kotlin Multiplatform RTF reader & writer — a port of RTFParserKit (Apache 2.0). Runs on JVM, Android, iOS, JS, WebAssembly, and Native.
  • Kotlin 97.8%
  • JavaScript 1.4%
  • Java 0.8%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
adam a8f3e71d87 Add pageBreakBefore to RtfParagraphStyle
Writes \pagebb in the paragraph prefix, so a paragraph can start on a
new page without a separate RtfPageBreak. A bare \page after a tall
picture starts the next paragraph with a break character, and Word lays
that line out on the current page together with the paragraph's
space-before, which can push the paragraph an extra page later.

Includes writer tests, a reader test that \pagebb parses as a flag with
no text leakage, a round trip, and a page break recipe in the module
page.
2026-10-29 22:26:23 -07:00
.github/workflows Add Dokka API docs and gh-pages deploy workflow 2026-06-28 18:12:00 -07:00
build-logic Add Dokka API docs and gh-pages deploy workflow 2026-06-28 18:12:00 -07:00
gradle Add Dokka API docs and gh-pages deploy workflow 2026-06-28 18:12:00 -07:00
kotlin-js-store Single-byte codepage tables in common + text/markdown converters 2026-06-24 09:54:39 -07:00
rtf-compose Add Dokka API docs and gh-pages deploy workflow 2026-06-28 18:12:00 -07:00
rtf-core Add picture support to the writer and reader 2026-10-29 20:42:02 -07:00
rtf-io-kotlinx Add Dokka API docs and gh-pages deploy workflow 2026-06-28 18:12:00 -07:00
rtf-io-okio Add Dokka API docs and gh-pages deploy workflow 2026-06-28 18:12:00 -07:00
rtf-reader Add pageBreakBefore to RtfParagraphStyle 2026-10-29 22:26:23 -07:00
rtf-writer Add pageBreakBefore to RtfParagraphStyle 2026-10-29 22:26:23 -07:00
sample-cli Rename package to com.darkrockstudios.libs.rtfparserkmp 2026-06-24 11:49:42 -07:00
tools Rename package to com.darkrockstudios.libs.rtfparserkmp 2026-06-24 11:49:42 -07:00
.gitattributes Scaffold KMP build and rtf-core foundation 2026-06-24 09:06:40 -07:00
.gitignore Add Dokka API docs and gh-pages deploy workflow 2026-06-28 18:12:00 -07:00
build.gradle.kts Add Dokka API docs and gh-pages deploy workflow 2026-06-28 18:12:00 -07:00
gradle.properties Set up Maven Central publishing (vanniktech, Central Portal) 2026-06-24 12:14:53 -07:00
gradlew Add Android target (AGP 9.0.0) and fix gradlew exec bit 2026-06-24 10:59:13 -07:00
gradlew.bat Scaffold KMP build and rtf-core foundation 2026-06-24 09:06:40 -07:00
LICENSE Scaffold KMP build and rtf-core foundation 2026-06-24 09:06:40 -07:00
NOTICE Scaffold KMP build and rtf-core foundation 2026-06-24 09:06:40 -07:00
PORTING-PLAN.md Scaffold KMP build and rtf-core foundation 2026-06-24 09:06:40 -07:00
PUBLISHING.md Add Dokka API docs and gh-pages deploy workflow 2026-06-28 18:12:00 -07:00
README.md Add picture support to the writer and reader 2026-10-29 20:42:02 -07:00
settings.gradle.kts Add rtf-compose: RTF -> Compose AnnotatedString 2026-06-24 12:08:20 -07:00

RtfParserKmp

Maven Central CI API Docs

An idiomatic Kotlin Multiplatform RTF reader and writer.

RtfParserKmp is a port of RTFParserKit by Jon Iles (Apache 2.0) — its proven, SAX-style Standard parser, re-homed into commonMain so it runs on the JVM, JS, WebAssembly, and every Kotlin/Native target with no platform code in the hot path. The parsing engine and the entire command dictionary are faithful translations of the original; the public API is reshaped to be idiomatic Kotlin (sealed event types, listener interfaces with default methods, a dependency-free byte source).

Acknowledgement. This library is a derivative work of RTFParserKit, © 2013 Jon Iles, used under the Apache License 2.0. See NOTICE. New code is © 2026 The RtfParserKmp contributors.

Modules

Each module publishes as its own Maven artifact under the group com.darkrockstudios, so you depend only on what you use.

Artifact Purpose Runtime deps
rtf-core Shared model: Command/CommandType, RtfEvent, RtfListener, RtfSource/ByteArrayRtfSource, styled-text model none
rtf-reader StandardRtfParser, parseRtf, charset decoding, RtfTextExtractor, RtfToMarkdown rtf-core
rtf-writer RtfWriter (styled model → RTF), MarkdownToRtf rtf-core
rtf-io-kotlinx RtfSource over a kotlinx-io Source rtf-core, kotlinx-io
rtf-io-okio RtfSource over an Okio BufferedSource rtf-core, Okio
rtf-compose RTF ↔ Compose AnnotatedString (rtfToAnnotatedString / annotatedStringToRtf) rtf-core, rtf-reader, rtf-writer, Compose ui-text

A non-published sample-cli (Kotlin/Native) demonstrates the stack end to end. rtf-compose targets the Compose-supported subset (JVM, Android, wasmJs, iOS, macOS); the other modules cover the full matrix.

dependencies {
    implementation("com.darkrockstudios:rtf-reader:0.1.0")
    implementation("com.darkrockstudios:rtf-writer:0.1.0")        // optional, for export
    implementation("com.darkrockstudios:rtf-io-kotlinx:0.1.0")    // optional, for streaming I/O
}

Supported targets

JVM · Android · JS (IR, browser + node) · wasmJs · wasmWasi · iOS (x64/arm64/simulatorArm64) · macOS (x64/arm64) · tvOS · watchOS · Linux (x64/arm64) · Windows (mingwX64).

Core parsing — including \uN Unicode, all single-byte codepages (Windows-125x, CP437/850/874, Mac Roman and friends), and UTF-8 — runs identically on every target with zero platform code.

Documentation

Full API reference and per-module recipe guides: darkrock-studios.github.io/RtfParserKmp.

Usage

Read

import com.darkrockstudios.libs.rtfparserkmp.parser.parseRtf
import com.darkrockstudios.libs.rtfparserkmp.converter.extractPlainText
import com.darkrockstudios.libs.rtfparserkmp.converter.convertToMarkdown

val bytes: ByteArray = /* your .rtf */
val text: String = extractPlainText(bytes)
val markdown: String = convertToMarkdown(bytes)

// Or drive the event stream yourself:
for (event in parseRtf(bytes)) { /* RtfEvent.Text, .Command, GroupStart, ... */ }

Implement RtfListener (every method has a no-op default) for streaming, or when over the sealed RtfEvent from parseRtf.

Write

import com.darkrockstudios.libs.rtfparserkmp.writer.convertMarkdownToRtf
import com.darkrockstudios.libs.rtfparserkmp.writer.writeRtf
import com.darkrockstudios.libs.rtfparserkmp.model.*

val rtf: String = convertMarkdownToRtf("This is **bold** and _italic_.")

// Or from the styled-text model directly:
val doc = StyledDocument(listOf(Paragraph(listOf(TextRun("Hello", CharacterStyle(bold = true))))))
val rtf2: String = writeRtf(doc)

The writer emits 7-bit ASCII (non-ASCII as \uN), so it has no charset dependency and round-trips cleanly with the reader.

For authoring rich documents (fonts, colors, headings, page breaks, hyperlinks, bookmarks, pictures, and paragraph layout), build an RtfDocument and serialize it with RtfDocumentWriter. See the Writing rich RTF guide.

Streaming I/O

import com.darkrockstudios.libs.rtfparserkmp.io.asRtfSource          // kotlinx-io or okio
import com.darkrockstudios.libs.rtfparserkmp.parser.StandardRtfParser

StandardRtfParser().parse(source.asRtfSource(), myListener)   // source: kotlinx-io Source / okio BufferedSource

Compose

import com.darkrockstudios.libs.rtfparserkmp.compose.rtfToAnnotatedString
import androidx.compose.foundation.text.BasicText

val styled: AnnotatedString = rtfToAnnotatedString(bytes)   // bold/italic/underline -> SpanStyle
// BasicText(styled)

val rtf: String = annotatedStringToRtf(styled)             // and back the other way

CLI

rtfcli <input> [--text | --markdown | --rtf] [-o <out>]

.rtf input → --text (default) or --markdown; .md input → --rtf.

Validation

The 22 real-world .rtf fixtures from upstream (Word, LibreOffice, Pages, encodings, \upr, negative Unicode, …) are embedded and compared byte-for-byte against their expected event dumps — green on JVM, and on every target for the single-byte/UTF-8 subset. CI runs the suite across Linux, macOS, and Windows runners.

Current limitations

  • Legacy multibyte CJK (codepages 932/936/949/950 via \'xx) decodes on JVM, Android, JS, wasmJs, and all Apple targets (web via TextDecoder, Apple via CFString). Only the remaining native targets — Linux, Windows (mingw), and wasmWasi — throw for those legacy bytes. \uN Unicode (what modern Word/LibreOffice/Pages emit) and all single-byte codepages work on every target.

License

Apache License 2.0 — see LICENSE and NOTICE.