2014-03-13 2 views
1

JEditorPane을 사용하여 rtf 텍스트를 구문 분석하고이를 html로 변환 할 수 있습니다. 그러나 html 출력에는 일부 형식, 즉이 경우 경고 코드가 누락되어 있습니다. 결과에서 볼 수 있듯이 밑줄 텍스트는 <u> 안에 올바르게 래핑되었지만 취소 선이 없습니다. 어떤 생각?rtf를 Java 형식의 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> 

답변