將 Word 轉換為 RTF
為什么要將 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
有效截圖:
	 
 

 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網安備
            50010702500608號
渝公網安備
            50010702500608號
             
            
 客服熱線
客服熱線