2014-03-13 55 views
1

我可以使用JEditorPane來解析rtf文本並將其轉換爲html。但是html輸出缺少一些格式,即在這種情況下的透視標記。正如您在輸出中看到的那樣,下劃線文本被正確包裝在<或>中,但沒有任何透明包裝。任何想法?使用Java格式將rtf轉換爲html

public void testRtfToHtml() 
{ 
    JEditorPane pane = new JEditorPane(); 
    pane.setContentType("text/rtf"); 

    StyledEditorKit kitRtf = (StyledEditorKit) pane.getEditorKitForContentType("text/rtf"); 

    try 
    { 
     kitRtf.read(
      new StringReader(
       "{\\rtf1\\ansi \\deflang1033\\deff0{\\fonttbl {\\f0\\froman \\fcharset0 \\fprq2 Times New Roman;}}{\\colortbl;\\red0\\green0\\blue0;} {\\stylesheet{\\fs20 \\snext0 Normal;}} {\\plain \\fs26 \\strike\\fs26 This is supposed to be strike-through.}{\\plain \\fs26 \\fs26 } {\\plain \\fs26 \\ul\\fs26 Underline text here} {\\plain \\fs26 \\fs26 .{\\u698\\'20}}"), 
      pane.getDocument(), 0); 
     kitRtf = null; 

     StyledEditorKit kitHtml = 
      (StyledEditorKit) pane.getEditorKitForContentType("text/html"); 

     Writer writer = new StringWriter(); 
     kitHtml.write(writer, pane.getDocument(), 0, pane.getDocument().getLength()); 
     System.out.println(writer.toString()); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 

輸出:

<html> 
    <head> 
    <style> 
     <!-- 
     p.Normal { 
      RightIndent:0.0; 
      FirstLineIndent:0.0; 
      LeftIndent:0.0; 
     } 
     --> 
    </style> 
    </head> 
    <body> 
    <p class=default> 
       <span style="color: #000000; font-size: 13pt; font-family: Times New Roman"> 
This is supposed to be strike-through. 
     </span> 
     <span style="color: #000000; font-size: 13pt; font-family: Times New Roman"> 

     </span> 
     <span style="color: #000000; font-size: 13pt; font-family: Times New Roman"> 
<u>Underline text here</u> 
     </span> 
     <span style="color: #000000; font-size: 13pt; font-family: Times New Roman"> 
.? 
     </span> 

    </p> 
    </body> 
</html> 

回答