如何使用 GroupDocs.Editor 编辑 DOCX 并转换格式

在您的 .NET 环境中轻松管理和转换 Word 文档,使用 GroupDocs.Editor .NET。在本教程中,您将了解 如何编辑 docx 文件,将其转换为 RTF、DOCM 和纯文本等格式,并自动化文档工作流以实现最高生产力。

快速答案

  • 需要的库是什么? GroupDocs.Editor for .NET (20.10+).
  • 我可以将 DOCX 转换为 RTF 吗? 是的 – 使用 WordProcessingSaveOptionsWordProcessingFormats.Rtf.
  • 支持保存为 TXT 吗? 当然,可以通过 TextSaveOptions.
  • 我需要许可证吗? 试用版可用于开发;许可证可解锁所有功能。
  • 如何处理大量文件? 将代码与 async/await 或 Parallel.ForEach 结合使用以进行批处理。

使用 GroupDocs.Editor “如何编辑 docx” 是什么?

GroupDocs.Editor 加载 DOCX,将其内容以可编辑的 HTML 形式呈现,允许您以编程方式修改该 HTML,然后将结果保存为任何受支持的格式。这种方法消除了对 Office 互操作的需求,并可在任何服务器端 .NET 平台上运行。

为什么在文档工作流自动化中使用 GroupDocs.Editor?

  • 仅服务器端 – 无需安装 Office。
  • 多种输出格式 – RTF、DOCM、TXT、HTML、PDF 等。
  • 高性能 – 为批处理场景设计的轻量级 API。
  • 完全控制 – 在保存前编辑、替换或注入 HTML 片段。

前提条件

  • GroupDocs.Editor 库(版本 20.10 或更高)
  • .NET Framework 4.7.2 + 或 .NET 5/6
  • Visual Studio(任何近期版本)
  • 基本的 C# 知识以及对文件 I/O 的了解

安装 GroupDocs.Editor

您可以通过 .NET CLI、Package Manager Console 或 NuGet UI 添加此包。

dotnet add package GroupDocs.Editor
Install-Package GroupDocs.Editor

获取许可证

先使用免费试用版或请求临时许可证密钥。生产环境请购买许可证以解锁无限制处理。

如何加载并编辑 DOCX 文档

首先,将编辑器指向源文件,然后获取可编辑的 HTML,进行所需的更改,最后使用修改后的标记创建新的 EditableDocument

using (Editor editor = new Editor("YOUR_DOCUMENT_DIRECTORY/sample.docx", new Options.WordProcessingLoadOptions()))
{
    // Your document manipulation code here
}

步骤代码演练

  1. 定义输入文件路径
string inputFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
  1. 创建 Editor 实例
using GroupDocs.Editor;
using GroupDocs.Editor.Options;

Editor editor = new Editor(inputFilePath, new Options.WordProcessingLoadOptions());
  1. 编辑文档的 HTML
EditableDocument defaultWordProcessingDoc = editor.Edit();
string allEmbeddedInsideString = defaultWordProcessingDoc.GetEmbeddedHtml();
string allEmbeddedInsideStringEdited = allEmbeddedInsideString.Replace("Subtitle", "Edited subtitle");
  1. 从编辑后的 HTML 创建新的 EditableDocument
EditableDocument editedDoc = EditableDocument.FromMarkup(allEmbeddedInsideStringEdited, null);
  1. 释放临时对象
editedDoc.Dispose();
defaultWordProcessingDoc.Dispose();
editor.Dispose();

如何将 Word 转换为 RTF

使用相应的保存选项将编辑后的文档保存为 RTF 非常简单。

string outputRtfPath = Path.Combine("YOUR_OUTPUT_DIRECTORY", "editedDoc.rtf");
WordProcessingSaveOptions rtfSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Rtf);
using (Editor editor = new Editor(inputFilePath, new Options.WordProcessingLoadOptions()))
{
    editor.Save(editedDoc, outputRtfPath, rtfSaveOptions);
}
editor.Dispose();

如何使用流将 DOCX 保存为 DOCM

当您需要宏启用的 DOCM 格式时,可以直接写入 FileStream

string outputDocmPath = Path.Combine("YOUR_OUTPUT_DIRECTORY", "editedDoc.docm");
WordProcessingSaveOptions docmSaveOptions = new WordProcessingSaveOptions(WordProcessingFormats.Docm);
using (Editor editor = new Editor(inputFilePath, new Options.WordProcessingLoadOptions()))
{
    using (FileStream outputStream = File.Create(outputDocmPath))
    {
        editor.Save(editedDoc, outputStream, docmSaveOptions);
    }
}
editor.Dispose();

如何将 DOCX 转换为 TXT(纯文本)

纯文本提取对于索引、搜索或电子邮件通知非常有用。

string outputTxtPath = Path.Combine("YOUR_OUTPUT_DIRECTORY", "editedDoc.txt");
TextSaveOptions textSaveOptions = new TextSaveOptions()
{
    Encoding = Encoding.UTF8,
    PreserveTableLayout = true
};
using (Editor editor = new Editor(inputFilePath, new Options.WordProcessingLoadOptions()))
{
    editor.Save(editedDoc, outputTxtPath, textSaveOptions);
}
editor.Dispose();

常见使用场景与真实案例

  • 自动化报告生成: 将分析性的 Word 报告转换为 TXT 以进行电子邮件分发。
  • 协作编辑平台: 让用户编辑 HTML 片段并将结果存储为 DOCM 或 RTF。
  • 文档归档: 将旧版 DOCX 文件迁移到 DOCM 以支持宏,同时保留内容。
  • Web 服务: 实时流式转换 DOCX → PDF/RTF,无需临时文件。

批量处理 Word 文档的性能技巧

  • 及时释放: 始终对 Editor 和文档对象调用 Dispose()
  • 明智加载: 选择最具体的 LoadOptions 以避免不必要的解析。
  • 安全并行化: 在每个线程中使用单独的 Editor 实例配合 Parallel.ForEach
  • 避免大型内存字符串: 处理超大文档时,使用流而不是完整的 HTML 字符串。

常见问题

Q: 我可以编辑受密码保护的 DOCX 吗?
A: 可以。在创建 Editor 时通过 WordProcessingLoadOptions.Password 提供密码。

Q: 是否可以一次性转换多个文件?
A: 当然。将单文件逻辑包装在循环中或使用 Parallel.ForEach 进行并发处理。

Q: GroupDocs.Editor 支持 .NET Core 吗?
A: 该库兼容 .NET 5、.NET 6、.NET Core 3.1 以及完整的 .NET Framework。

Q: 当我保存为 DOCM 时宏会怎样?
A: 使用 Docm 保存格式时宏会被保留;而在 RTF/TXT 中会被剥离。

Q: 在大批量作业期间如何排查 “OutOfMemoryException”?
A: 将文件分成更小的批次处理,立即释放对象,并考虑增加应用程序的内存限制。

结论

您现在拥有一个完整的、可投入生产的工作流,可使用 GroupDocs.Editor .NET 编辑 docx 文件并将其转换为 RTF、DOCM 或纯文本。通过遵循上述步骤,您可以自动化文档工作流、处理批量任务,并将无缝的格式转换集成到任何 .NET 应用程序中。


最后更新: 2026-04-07
测试环境: GroupDocs.Editor 20.10(撰写时的最新版本)
作者: GroupDocs