Class RichTextDocuments

java.lang.Object
com.codename1.wordprocessing.RichTextDocuments

public final class RichTextDocuments extends Object

Moves content between a RichTextArea and a Document, so the light editor and the paged one can work on the same text.

final DocumentModel model = new DocumentModel(new Document());
model.insertBlock(0, new Paragraph(model.newId('p'), RichText.of("Edit me")));
RichTextArea editor = new RichTextArea();
RichTextDocuments.show(model.getDocument(), editor);
// later, once the user has edited the text
RichTextDocuments.read(editor, new SuccessCallback<Document>() {
    public void onSucess(Document d) {
        model.setDocument(d);
    }
});

Content goes through HTML, so what HTML cannot say -- page size, tracked changes, comments -- is not carried. Embedded components are: the editor shows each through the image source EmbeddedComponents registers, and reading back restores its type, size and state.

  • Method Details

    • show

      public static void show(Document d, RichTextArea editor)

      Shows a document in a rich text editor.

      Parameters
      • d: the document

      • editor: the editor

    • toHtml

      public static String toHtml(Document d)

      The HTML a rich text editor is given for a document.

      Parameters
      • d: the document
      Returns

      the HTML, embedded components named by cn1-component: sources

    • fromHtml

      public static Document fromHtml(String html)

      A document made from HTML a rich text editor gave back.

      Parameters
      • html: the HTML
      Returns

      the document

    • readMarkdown

      public static Document readMarkdown(String markdown) throws IOException

      Reads Markdown. Unlike Document.read, this reaches no other format's code, so an application that only exchanges Markdown with a rich text editor carries no DOCX, ODT or PDF support.

      Parameters
      • markdown: the source
      Returns

      the document

      Throws
      • IOException: when the source nests too deeply for the thread's stack
      Throws:
      IOException
    • writeMarkdown

      public static String writeMarkdown(Document d)

      Writes Markdown.

      Parameters
      • d: the document
      Returns

      the Markdown

    • readRtf

      public static Document readRtf(String rtf) throws IOException

      Reads RTF from a string, as a clipboard gives it. Characters above U+00FF, which RTF spells with escapes, are kept: each becomes the backslash-u control word that says it.

      Parameters
      • rtf: the source
      Returns

      the document

      Throws
      • IOException: when the source is not RTF or nests too deeply for the thread's stack
      Throws:
      IOException
    • writeRtf

      public static String writeRtf(Document d)

      Writes RTF.

      Parameters
      • d: the document
      Returns

      the RTF, which is 7-bit

    • read

      public static void read(RichTextArea editor, SuccessCallback<Document> callback)

      Reads a rich text editor's content as a document. The editor answers asynchronously.

      Parameters
      • editor: the editor

      • callback: given the document