Java Swing Text Style unRTF(String s)

Here you can find the source of unRTF(String s)

Description

Extract text from RTF

License

Open Source License

Declaration

static String unRTF(String s) 

Method Source Code


//package com.java2s;
import java.io.StringReader;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;

public class Main {
    /**/* w ww  .j  ava 2 s.  c om*/
     * Extract text from RTF
     */
    static String unRTF(String s) {
        if (s == null)
            return null;

        DefaultStyledDocument doc = new DefaultStyledDocument();
        RTFEditorKit kit = new RTFEditorKit();

        try {
            kit.read(new StringReader(s), doc, 0);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

        try {
            s = doc.getText(0, doc.getLength());
        } catch (BadLocationException e) {
            throw new RuntimeException(e);
        }

        return s;
    }
}

Related

  1. hasDifferentCharacterAttributes(AttributeSet style, AttributeSet base)
  2. isCentered(AttributeSet attributes)
  3. isLTR(final int level)
  4. overrideStyle(Style originalStyle, Style newStyle)
  5. resolveAttributes(AttributeSet style)