为什么要将 Word 转换为 RTF?
自 1990 年代以来,Microsoft 开始为其一系列专有的文字处理格式使用 .doc 扩展名。Doc 格式通常与 Microsoft Office 应用程序相关联。但是在 Doc 文件中存储文本并不总是很方便,因为它们与大多数第三方文本编辑器不兼容。为了使 Doc 信息更易于访问,最好将其存储为其他更常见的格式。用户通常选择 RTF 格式,因为它受到广泛的应用程序的支持。
如何使用 Spire.Doc 将 Word 转换为 RTF?
Spire.Doc for .NET 可以在您的系统上不安装 Microsoft Office 的情况下将 Word 转换为 RTF。使用 .NET Framework 2.0(或更高版本)下载Spire.Doc(或Spire.Office),然后按照以下简单步骤将 Word 转换为 RTF。
第 1 步:通过 Visual Studio 创建一个项目并添加 Spire.Doc DLL 作为参考。
第 2 步:加载我们需要转换为 RTF 格式的本地 Word doc 文件。使用下面的代码加载 Word 文件。
Document document = new Document();
document.LoadFromFile(@"D:\Sample.doc");
第 3 步:Spire.Doc 允许用户将 Word doc 文件转换为大多数流行的文件格式,包括 PDF、HTML、RTF、EPub、XML、Dot、Text 等。以下代码可以帮助我们将 Word 转换为 RTF。
document.SaveToFile("Sample.rtf", FileFormat.Rtf);
第 4 步:将下面的完整代码复制到项目中,然后按 F5 启动项目。
完整代码:
[C#]
using Spire.Doc;
using System;
namespace DOCRTF
{
class Program
{
static void Main(string[] args) { }
private void button1_Click(object sender, EventArgs e)
{
//Create word document
Document document = new Document();
document.LoadFromFile(@"D:\Sample.doc");
//Save doc file.
document.SaveToFile("Sample.rtf", FileFormat.Rtf);
//Launching the MS Word file.
WordDocViewer("Sample.rtf");
}
private void WordDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch { }
}
}
}
[VB.NET]
Imports Spire.Doc
Namespace DOCRTF
Class Program
Private Shared Sub Main(args As String())
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs)
'Create word document
Dim document As New Document()
document.LoadFromFile("D:\Sample.doc")
'Save doc file.
document.SaveToFile("Sample.rtf", FileFormat.Rtf)
'Launching the MS Word file.
WordDocViewer("Sample.rtf")
End Sub
Private Sub WordDocViewer(fileName As String)
Try
System.Diagnostics.Process.Start(fileName)
Catch
End Try
End Sub
End Class
End Namespace
有效截图:
欢迎下载|体验E-iceblue产品