这个清晰的主题包含了使用 NET REST API 将 Word 转换为 RTF 的过程。我们将利用 Aspose.Words for .NET Cloud SDK 将 Word DOC 文件导出为 RTF 文件。如果您打算使用 C# 低代码 API 开发 Word DOC 到 RTF 转换器,请遵循本文中共享的指南,并使用工作示例代码尝试该功能。
先决条件
- 创建免费帐户并获取 API 凭据
- 下载 Aspose.Words Cloud SDK for .NET
- 使用上述 SDK 设置用于 Word 到 RTF 转换的 C# 项目
使用 C# 低代码 API 将 Word DOC 转换为 RTF 的步骤
- 设置 API 的客户端 ID 和客户端密钥以将 Word Doc 转换为 RTF
- 使用您的客户端凭据创建 WordsAPI 类的实例
- 设置输入Word和输出RTF文件名
- 读取源 Word DOC 文件并将其转换为文件流
- 使用 ConvertDocument 方法使用 REST API 将 DOCX 转换为 RTF
- 将生成的RTF文件保存到本地磁盘
上述步骤确保使用 REST API* 简化 *DOCX 到 RTF 的转换,该过程是通过使用 Aspose.Words for .NET REST SDK 启动的,并通过提供创建的云应用程序标识符和秘密值来创建 WordsApi 类的对象。订阅。将通过文件流从磁盘访问源 Word 文件,并使用 ConvertDocument() 方法,在云端完成 Word 到 RTF 的转换过程,并返回可保存在磁盘上的 RTF 文件流。
使用 NET REST API 将 DOC 转换为 RTF 的代码
using Aspose.Words.Cloud.Sdk; | |
using Aspose.Words.Cloud.Sdk.Model.Requests; | |
using System; | |
using System.IO; | |
namespace WordsSample.Words | |
{ | |
public class WordsToRtf | |
{ | |
public void ConvertWordToRtfAsync() | |
{ | |
try | |
{ | |
var apiClient = new Configuration(); | |
apiClient.ClientSecret = ""; | |
apiClient.ClientId = ""; | |
//Create SDK object | |
WordsApi wordsApi = new WordsApi(apiClient); | |
string localPath = @"C:\Words\"; | |
string inputFile = "Test1.docx"; | |
string outputFile = "DOCXToRtf.rtf"; | |
string outputFormat = "rtf"; | |
//Read input file to bytes array | |
var inpuFileStream = File.Open(localPath + '/' + inputFile, FileMode.Open); | |
inpuFileStream.Position = 0; | |
//var uploadDocRequest = new UploadFileRequest(inpuFileStream, inputFile); | |
//var task = wordsApi.UploadFile(uploadDocRequest); | |
//task.Wait(); | |
//var result = task.Result; | |
//create conversion request object with input and output files | |
ConvertDocumentRequest convertDocumentRequest = new ConvertDocumentRequest(inpuFileStream, outputFormat, null, null, null, null, null, null, null); | |
var conversionTask = wordsApi.ConvertDocument(convertDocumentRequest); | |
conversionTask.Wait(); | |
var outputFileStream = conversionTask.Result; | |
outputFileStream.Position = 0; | |
using (var fileStream = File.Create(localPath + outputFile+"."+ outputFormat)) | |
{ | |
outputFileStream.Seek(0, SeekOrigin.Begin); | |
outputFileStream.CopyTo(fileStream); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} |
此示例代码演示了使用 C# REST API* 开发 *Word 文档到 RTF 转换器的基础。您可以在转换过程中配置其他参数,包括输出文档的路径、行距和输出文件名。 DOC 到 RTF 转换 的此功能可以与支持 .NET Framework 的任何操作系统上的任何无代码或低代码应用程序集成。
在本主题中,我们了解了使用 NET REST API 将Word 转换为RTF。如果您对 Word 到 PDF 转换感兴趣,请参阅有关如何使用 NET REST API 将 DOCX 转换为 PDF的文章。