Java Swing HTML rtfToHtml(String rtf)

Here you can find the source of rtfToHtml(String rtf)

Description

rtf To Html

License

Open Source License

Declaration

public static String rtfToHtml(String rtf) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import javax.swing.text.BadLocationException;

import javax.swing.text.EditorKit;

import java.io.*;

public class Main {
    public static String rtfToHtml(String rtf) throws IOException {
        return rtfToHtml(new StringReader(rtf));
    }/*from  www .jav a  2  s.c  o  m*/

    /**
     * @see http 
     *      ://www.codeproject.com/Tips/136483/Java-How-to-convert-RTF-into-HTML
     * @param rtf
     * @return
     * @throws java.io.IOException
     */
    public static String rtfToHtml(Reader rtf) throws IOException {
        JEditorPane p = new JEditorPane();
        p.setContentType("text/rtf");
        EditorKit kitRtf = p.getEditorKitForContentType("text/rtf");
        try {
            kitRtf.read(rtf, p.getDocument(), 0);
            kitRtf = null;
            EditorKit kitHtml = p.getEditorKitForContentType("text/html");
            Writer writer = new StringWriter();
            kitHtml.write(writer, p.getDocument(), 0, p.getDocument().getLength());
            return writer.toString();
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. makeStyleSheet(String name)
  2. nameOf(Element element)
  3. plainToHtml(String plain)
  4. rtfToBody(String rtf)
  5. rtfToHtml(Reader rtf)
  6. stripHTML(String html)
  7. toBold(final T label)
  8. write(Writer w, HTML.Tag tag, AttributeSet attributes)