All formats

RTFEditorKit (javax.swing.text.rtf)

LanguageJava (part of the java.desktop module)
LicenseGPL-2.0 with Classpath Exception (OpenJDK)
Repositorygithub.com/openjdk/jdk
DocumentationRTFEditorKit
Depends onbuilt in (java.desktop module)
Read / writeyes / yes; text, fonts, sizes, bold/italic/underline, colors, alignment; no tables or pictures

RTFEditorKit is the Swing editor kit that lets JEditorPane show RTF. You can use it without a GUI: read parses RTF into a StyledDocument (DefaultStyledDocument), where each run of text carries attributes like StyleConstants.Bold and StyleConstants.FontFamily; write serializes a styled document back to RTF.

Its RTF support is basic and dated. It ignores tables, images, lists, headers and footers, may mishandle some code pages and Unicode escapes (\uN), and writes RTF that is simple but valid. It runs headless (no display needed). For more robust text extraction use RTF Parser Kit.

Installation#

Built into the JDK. In a modular application add requires java.desktop;.

Basic usage: extract text from an RTF file#

Java
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.swing.text.Document;
import javax.swing.text.rtf.RTFEditorKit;

public class RtfToText {
    public static void main(String[] args) throws Exception {
        RTFEditorKit kit = new RTFEditorKit();
        Document doc = kit.createDefaultDocument();
        try (InputStream in = Files.newInputStream(Path.of("letter.rtf"))) {
            kit.read(in, doc, 0);
        }
        String text = doc.getText(0, doc.getLength());
        System.out.println(text);
    }
}

How to read formatting (bold runs, fonts)#

Walk the paragraph and character elements of the StyledDocument:

Java
import javax.swing.text.*;

StyledDocument sdoc = (StyledDocument) doc;
Element root = sdoc.getDefaultRootElement();
for (int p = 0; p < root.getElementCount(); p++) {
    Element para = root.getElement(p);
    for (int r = 0; r < para.getElementCount(); r++) {
        Element run = para.getElement(r);
        AttributeSet a = run.getAttributes();
        String s = sdoc.getText(run.getStartOffset(), run.getEndOffset() - run.getStartOffset());
        System.out.printf("%s%s [%s %dpt]%n",
            StyleConstants.isBold(a) ? "**" : "", s.strip(),
            StyleConstants.getFontFamily(a), StyleConstants.getFontSize(a));
    }
}

How to write an RTF file#

Java
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.swing.text.*;
import javax.swing.text.rtf.RTFEditorKit;

DefaultStyledDocument doc = new DefaultStyledDocument();
SimpleAttributeSet normal = new SimpleAttributeSet();
StyleConstants.setFontFamily(normal, "Arial");
StyleConstants.setFontSize(normal, 12);
SimpleAttributeSet bold = new SimpleAttributeSet(normal);
StyleConstants.setBold(bold, true);

doc.insertString(doc.getLength(), "Invoice ", bold);
doc.insertString(doc.getLength(), "No. 42\nThank you for your order.\n", normal);

try (OutputStream out = Files.newOutputStream(Path.of("invoice.rtf"))) {
    new RTFEditorKit().write(out, doc, 0, doc.getLength());
}

How to show RTF in a Swing window#

Java
JEditorPane pane = new JEditorPane();
pane.setEditorKit(new RTFEditorKit());
pane.read(Files.newInputStream(Path.of("letter.rtf")), null);

Tips#