public static String getContent(HTMLDocument doc) { try { StringWriter writer = new StringWriter(); HTMLWriter htmlWriter = new HtmlBodyWriter(writer, doc); htmlWriter.write(); return writer.toString(); } catch (IOException | BadLocationException e) { } return ""; }
private String getSelectionAsHTML() { StringWriter buf = new StringWriter(); HTMLWriter writer = new HTMLWriter(buf, (HTMLDocument)jTextPane.getDocument(), jTextPane.getSelectionStart(), jTextPane.getSelectionEnd()); try { writer.write(); } catch (BadLocationException | IOException ex) { ex.printStackTrace(); } return buf.toString(); }
public final void string2html(String htmlText, String outputFilename) throws IOException, BadLocationException { HTMLEditorKit kit = new HTMLEditorKit(); HTMLDocument htmlDoc = (HTMLDocument) kit.createDefaultDocument(); kit.read(new StringReader(htmlText), htmlDoc, 0); StyleSheet styleSheet = htmlDoc.getStyleSheet(); styleSheet.addRule("p {margin-top:5px;margin-bottom:5px;text-indent:25px}"); styleSheet.addRule("h1 {font-size:150%;font-weight:bold;}"); styleSheet.addRule("h2 {font-size:100%;font-weight:bold;}"); styleSheet.addRule(".T {color:blue; font-weight:bold;font-size:150%}"); styleSheet.addRule(".A {color:green; font-weight:bold;font-size:150%}"); styleSheet .addRule(".A1A2 {color:#DF01D7; font-weight:bold;font-size:150%}"); styleSheet.addRule(".eventid {font-weight:bold;}"); styleSheet.addRule(".table_event_type {width:200px;}"); styleSheet.addRule(".table_event_trigger {width:200px;}"); styleSheet.addRule(".table_primary {width:200px;}"); styleSheet.addRule(".table_secondary {width:200px;}"); styleSheet .addRule(".title {margin-top:10px;font-weight:bold; font-size:150%}"); styleSheet .addRule("table {border-collapse:collapse; border:1px solid black;}"); styleSheet.addRule("th,td {border:1px solid black;}"); // print PrintStream out = new PrintStream(new FileOutputStream(outputFilename)); StringWriter stringWriter = new StringWriter(); HTMLWriter writer = new HTMLWriter(stringWriter, htmlDoc); writer.write(); stringWriter.flush(); out.println(stringWriter); out.close(); }