Home arrow static arrow Java Programming [Archive] - HTML to RTF conversion
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - HTML to RTF conversion
10 Duke Stars available
This topic has 2 replies on 1 page.

Posts:1
Registered: 7/23/04
HTML to RTF conversion  
Jul 23, 2004 4:02 AM



 
Hello. I am trying to convert a simple html string and produce a rtf file using the HTMLEditorKit and RTFEditorKit. I have already found some topics which cover the process and their authors warn that the output RTF will not contain images and tables. But what strikes me is that i can't even get new lines and paragraphs in the RTF-file. Here is the example code. The output RTF document contains just a single line of text. Thank you in advance!

import java.io.*;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;

import javax.swing.text.html.HTMLDocument;
import javax.swing.text.rtf.RTFEditorKit;
import javax.swing.text.html.HTMLEditorKit;

public class MainController {

private DefaultStyledDocument htmlDoc;
private HTMLEditorKit htmlKit;
private RTFEditorKit rtfKit;

public MainController() {
htmlDoc = new HTMLDocument();
htmlKit = new HTMLEditorKit();
rtfKit = new RTFEditorKit();
}

private void convert(String strText) {
StringReader reader = new StringReader(strText);
try {
htmlKit.read(reader, htmlDoc, 0);
FileOutputStream f = new FileOutputStream("rtfdoc.rtf");
rtfKit.write(
f,
htmlDoc,
0,
htmlDoc.getLength());
JEditorPane pane = new JEditorPane("text/html", strText);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(pane);
frame.setSize(300,300);
frame.show();
Thread.sleep(5000);
}
catch (IOException ie) {
}
catch (BadLocationException ble) {
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void main(String args[]) {
MainController conv = new MainController();
String strRTF =
"<html><head>

<span style=\"color: #000000\">Test </span><span style=\"color: #000000\">line </span><span style=\"color: #000000\">1 </span>

" +
"

<span style=\"color: #000000\">Test </span><span style=\"color: #000000\">line </span><span style=\"color: #000000\">2 </span>

</head></html>";
conv.convert(strRTF);
System.exit(0);
}
}
 

Posts:24,036
Registered: 2/3/03
Re: HTML to RTF conversion  
Jul 23, 2004 5:16 AM (reply 1 of 2)



 
When you post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read and prevents accidental markup from array indices like [i].
 

Posts:580
Registered: 8/21/02
Re: HTML to RTF conversion  
Jul 23, 2004 5:30 AM (reply 2 of 2)



 

But what strikes me is that i can't even get new lines and
paragraphs in the RTF-file.

It shouldn't. HTML support in java sucks. RTF support sucks bigtime.
 
This topic has 2 replies on 1 page.