将HTML转换成RTF问题,怎么解决

html-css015

将HTML转换成RTF问题,怎么解决,第1张

使用JEditorPane浏览即可。 例如:new JEditorPane( "text/rtf",(要显示的String类型的字符串) )//具体参考JavaDococ文档 如果用工具的话,用Word自带的就可以了。 如果编程的话,用POI也是可以的.

1、纯文本格式就是没有任何文本修饰的,没有任何粗体,下划线,斜体,图形,符号或特殊字符及特殊打印格式的文本,只保存文本,不保存其格式设置,将所有的分节符、分页符、新行字符转换为段落标记。

常见的纯文本格式文件的扩展名:

TXT、HTM、ASP、BAT、C、BAS、PRG、CMD等。

2、超级文本标记语言是标准通用标记语言下的一个应用,也是一种规范,一种标准,它通过标记符号来标记要显示的网页中的各个部分。

通过在文本文件中添加标记符,可以告诉浏览器如何显示其中的内容(如:文字如何处理,画面如何安排,图片如何显示等)。

3、多信息文本格式 (RTF) 是一种方便于不同的设备、系统查看的文本和图形文档格式。

RTF 使用美国国内标准协会 (ANSI)、 PC-8、 Macintosh(mac苹果),或 IBM 的 PC 字符设置控制显示形式和打印形式。

扩展资料

下面分别介绍三种语言的特点。

一、纯文本文件特点:

纯文本文件的内容可以在任何一台计算机上显示(除了其中的控制代码),不需要特殊的“浏览器”之类的软件。

二、HTML特点

1、简易性:

超级文本标记语言版本升级采用超集方式,从而更加灵活方便。

2、可扩展性:

超级文本标记语言的广泛应用带来了加强功能,增加标识符等要求,超级文本标记语言采取子类元素的方式,为系统扩展带来保证。

3、平台无关性:

超级文本标记语言可以使用在广泛的平台上。

4、通用性:

HTML是网络的通用语言,一种简单、通用的全置标记语言。

它允许网页制作人建立文本与图片相结合的复杂页面,这些页面可以被网上任何其他人浏览到,无论使用的是什么类型的电脑或浏览器。

三、RTF特点

1、对普通用户而言,RTF格式是一个很好的文件格式转换工具,用于在不同应用程序之间进行格式化文本文档的传送。

2、通用兼容性应该是RTF的最大优点,但同时也就具有它的缺点,比如文件一般相对较大(可能因为嵌入了兼容各种应用程序的控制符号吧)、WORD等应用软件特有的格式可能无法正常保存等。

参考资料来源:百度百科-纯文本文件

参考资料来源:百度百科-HTML

参考资料来源:百度百科-RTF

将RTF格式的文件转成HTML并在网页中显示的代码 它是这样工作的,将一个RTF文件上传,然后转成HTML显示出来,代码有点复杂,teaman还要好好研究,好象中文版有点问题。 <html><body><? if(!($userfile)) { ?><form enctype="multipart/form-data" action="<?print($PHP_SELF)?>" method=post><input type=hidden name="max_file_size" value=2000>Send this file: <input name=userfile type=file><input type=submit value=Upload></form></body></html><? exit} function ProcessTags($tags, $line) { $html = ""global $colorglobal $sizeglobal $bullets// Remove spaces. $tags = trim($tags)// Found the beginning of the bulleted l // ist. if(ereg("\\\pnindent", $tags)) { $html .= "<ul><li>"$bullets += $line$tags = ereg_replace("\\\par", "", $tags)$tags = ereg_replace("\\\(tab)", "", $tags)} if($line - $bullets == 0) { $tags = ereg_replace("\\\par", "", $tags)} elseif($line - $bullets == 1) { if(ereg("\\\pntext", $tags)) { $html .= "<li>"$tags = ereg_replace("\\\par", "", $tags)$tags = ereg_replace("\\\(tab)", "", $tags)$bullets++} else { $html .= "</ul>"$bullets = 0} } // Convert Bold. if(ereg("\\\b0", $tags)){ $html .= "</b>"} elseif(ereg("\\\b", $tags)) { $html .= "<b>"} // Convert Italic. if(ereg("\\\i0", $tags)){ $html .= "</i>"} elseif(ereg("\\\i", $tags)) { $html .= "<i>"} // Convert Underline. if(ereg("\\\ulnone", $tags)){ $html .= "</u>"} elseif(ereg("\\\ul", $tags)){ $html .= "<u>"} // Convert Alignments. if(ereg("\\\pard\\\qc", $tags)) { $html .= "<div align=center>"} elseif(ereg("\\\pard\\\qr", $tags)) { $html .= "<div align=right>"} elseif(ereg("\\\pard", $tags)){ $html .= "<div align=left>"} // Remove \pard from the tags so it does // n't get confused with \par. $tags = ereg_replace("\\\pard", "", $tags)// Convert line breaks. if(ereg("\\\par", $tags)){ $html .= "<br>"} // Use the color table to capture the fo // nt color changes. if(ereg("\\\cf[0-9]", $tags)) { global $fcolor$numcolors = count($fcolor)for($i = 0$i <$numcolors$i++) { $test = "\\\cf" . ($i + 1)if(ereg($test, $tags)) { $color = $fcolor[$i]} } } // Capture font size changes. if(ereg("\\\fs[0-9][0-9]", $tags, $temp)) { $size = ereg_replace("\\\fs", "", $temp[0])$size /= 2if($size <= 10) { $size = 1} elseif($size <= 12) { $size = 2} elseif($size <= 14) { $size = 3} elseif($size <= 16) { $size = 4} elseif($size <= 18) { $size = 5} elseif($size <= 20) { $size = 6} elseif($size <= 22) { $size = 7} else{ $size = 8} } // If there was a font color or size cha // nge, change the font tag now. if(ereg("(\\\cf[0-9])||(\\\fs[0-9][0-9])", $tags)) { $html .= "</font><font size=$size color=$color>"} // Replace \tab with alternating spaces // and nonbreakingwhitespaces. if(ereg("\\\(tab)", $tags)) { $html .= ""} return $html} function ProcessWord($word) { // Replace \\ with \ $word = ereg_replace("[\\]{2,}", "\\", $word)// Replace \{ with { $word = ereg_replace("[\\][\{]", "\{", $word)// Replace \} with } $word = ereg_replace("[\\][\}]", "\}", $word)// Replace 2 spaces with one space. $word = ereg_replace(" ", " ", $word)return $word} $color = "000000"$size = 1$bullets = 0// Read the uploaded file into an array. // $rtfile = file($userfile)$fileLength = count($rtfile)// Loop through the rest of the array for($i = 1$i <$fileLength$i++) { /* ** If the line contains "\colortbl" then we found the color table. ** We'll have to split it up into each individual red, green, and blue ** Convert it to hex and then put the red, green, and blue back together. ** Then store each into an array called fcolor. */ if(ereg("^\{\\\colortbl", $rtfile[$i])) { // Split the line by the backslash. $colors = explode("\\", $rtfile[$i])$numOfColors = count($colors)for($k = 2$k <$numOfColors$k++) { // Find out how many different colors th // ere are. if(ereg("[0-9]+", $colors[$k], $matches)) { $match[] = $matches[0]} } // For each color, convert it to hex. $numOfColors = count($match)for($k = 0$k <$numOfColors$k += 3) { $red = dechex($match[$k])$red = $match[$k] <16 ? "0$red" : $red$green = dechex($match[$k + 1])$green = $match[$k +1] <16 ? "0$green" : $green$blue = dechex($match[$k + 2])$blue = $match[$k + 2] <16 ? "0$blue" : $blue$fcolor[] = "$red$green$blue"} $numOfColors = count($fcolor)} // Or else, we parse the line, pulling o // ff words and tags. else { $token = ""$start = 0$lineLength = strlen($rtfile[$i])for($k = 0$k <$lineLength$k++) { if($rtfile[$i][$start] == "\\" &&$rtfile[$i][$start + 1] != "\\") { // We are now dealing with a tag. $token .= $rtfile[$i][$k]if($rtfile[$i][$k] == " ") { $newFile[$i] .= ProcessTags($token, $i)$token = ""$start = $k + 1} elseif($rtfile[$i][$k] == "\n") { $newFile[$i] .= ProcessTags($token, $i)$token = ""} } elseif($rtfile[$i][$start] == "{") { // We are now dealing with a tag. $token .= $rtfile[$i][$k]if($rtfile[$i][$k] == "}") { $newFile[$i] .= ProcessTags($token, $i)$token = ""$start = $k + 1} } else { // We are now dealing with a word. if($rtfile[$i][$k] == "\\" &&$rtfile[$i][$k + 1] != "\\" &&$rtfile[$i][$k - 1] != "\\") { $newFile[$i] .= ProcessWord($token)$token = $rtfile[$i][$k]$start = $k} else { $token .= $rtfile[$i][$k]} } } } } $limit = sizeof($newFile)for($i = 0$i <$limit$i++) { print("$newFile[$i]\n")} ?></body></html>

不行的话试试这个

{

//创建一个word的实例

Word.Application newApp = new Word.Application()

// 指定源文件和目标文件

object Source="c:\\abc\\Source.doc"

object Target="c:\\abc\\Target.rtf"

object Unknown =Type.Missing

// 打开要转换的Word文件

newApp.Documents.Open(ref Source,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown )

// 指定文档的类型

object format = Word.WdSaveFormat.wdFormatRTF

//改变文档类型

newApp.ActiveDocument.SaveAs(ref Target,ref format,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown)

//关闭word实例

newApp.Quit(ref Unknown,ref Unknown,ref Unknown)

}

}

}

或是这种方法

在richtohtml函数中增加下面的代码就行了:

Dim strLastFontsize As Long

If strLastFontsize <>rtbRichTextBox.SelFontSize Then

strLastFontsize = rtbRichTextBox.SelFontSize

strHTML$ = strHTML$ + "<font-size=""" &strLastFontsize &""">"

End If