在办公自动化、文档导出、报表生成等场景中,HTML 与RTF格式的转换是常见需求。RTF(Rich Text Format)作为跨平台的富文本格式,兼容Word、WPS等主流办公软件,而HTML则是网页内容的标准格式。Free Spire.Doc for .NET 是一款免费高效的 .NET 文档处理库,支持 HTML 到 RTF 的快速转换,且无需依赖 Office 或其他第三方软件。本文将详细讲解如何使用该免费库通过 C# 代码实现 HTML 到 RTF 的转换。
通过Nuget安装 Free Spire.Doc:
Install-Package FreeSpire.Doc
HTML→RTF核心转换原理
Free Spire.Doc for .NET 实现HTML到RTF转换的核心流程如下:
- HTML解析 :通过内置的HTML解析引擎,解析HTML字符串/文件中的标签(如
<p>、<font>、<table>)、属性(如style、src)和内容; - 样式映射 :将HTML的CSS样式(内联样式、
<style>标签样式)映射为RTF支持的富文本格式(字体、颜色、段落间距、表格边框等); - 文档构建 :创建
Document对象(Free Spire.Doc的核心文档模型),将解析后的HTML内容转化为文档的Section、Paragraph、Table等元素; - RTF导出 :调用
Document.SaveToFile()方法,指定保存格式为FileFormat.Rtf,生成RTF文件(或输出为流)。
实战示例(覆盖多场景)
以下示例代码均包含详细注释,可直接复制运行。需先添加命名空间:
csharp
using System;
using System.IO;
using Spire.Doc;
using Spire.Doc.Documents;
示例1:基础场景------HTML字符串转RTF文件
适用于简单HTML内容(无复杂样式、图片),快速实现转换。
csharp
class Program
{
static void Main(string[] args)
{
try
{
// 1. 定义HTML字符串(简单文本+基础样式)
string htmlContent = @"
<html>
<body>
<h1 style='color: #2E86AB; font-size: 24px;'>HTML转RTF基础示例</h1>
<p style='font-family: 微软雅黑; font-size: 14px; line-height: 1.5;'>
这是通过Free Spire.Doc转换的RTF文档,支持<b>粗体</b>、<i>斜体</i>、<u>下划线</u>等格式。
</p>
<ul style='color: #A23B72;'>
<li>列表项1</li>
<li>列表项2</li>
</ul>
</body>
</html>";
// 2. 创建Document对象(Free Spire.Doc的核心文档实例)
Document doc = new Document();
// 3. 添加一个文档节(Section)和段落(Paragraph)
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
// 4. 将HTML内容追加到段落中(关键方法:AppendHTML)
paragraph.AppendHTML(htmlContent);
// 5. 保存为RTF文件(指定格式为FileFormat.Rtf)
string outputPath = @"D:\Output\BasicHtmlToRtf.rtf";
doc.SaveToFile(outputPath, FileFormat.Rtf);
Console.WriteLine($"RTF文件生成成功!路径:{outputPath}");
}
catch (Exception ex)
{
Console.WriteLine($"转换失败:{ex.Message}");
}
}
}
运行效果 :生成的RTF文件可在Word中打开,完美保留标题颜色、字体、列表样式及文本格式。

示例2:进阶场景------含图片/表格的HTML转换
实际开发中,HTML常包含图片(本地图片、网络图片、Base64图片)和表格,以下示例覆盖这些场景。
2.1 含本地/网络图片的HTML转换
csharp
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
// HTML内容:包含本地图片(绝对路径)和网络图片
string htmlWithImage = @"
<html>
<body>
<h2>含图片的HTML转换</h2>
<p>本地图片:</p>
<img src='C:\Users\Administrator\Pictures\test.png' width='300' height='200' alt='本地图片'/>
<p>网络图片:</p>
<img src='https://picsum.photos/400/250' width='400' height='250' alt='网络图片'/>
</body>
</html>";
// 追加HTML时指定导入选项
paragraph.AppendHTML(htmlWithImage);
// 保存RTF
string outputPath = @"HtmlWithImage.rtf";
doc.SaveToFile(outputPath, FileFormat.Rtf);
Console.WriteLine($"含图片的RTF生成成功:{outputPath}");
}
}

2.2 含表格的HTML转换
csharp
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();
// HTML表格(带边框、合并单元格、样式)
string htmlWithTable = @"
<html>
<body>
<h2>含表格的HTML转换</h2>
<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 80%;'>
<tr style='background-color: #E8F4FD;'>
<th>姓名</th>
<th>年龄</th>
<th>部门</th>
</tr>
<tr>
<td>张三</td>
<td>28</td>
<td rowspan='2'>研发部</td> <!-- 合并单元格 -->
</tr>
<tr>
<td>李四</td>
<td>32</td>
</tr>
<tr>
<td colspan='3' style='text-align: center;'>员工信息表</td> <!-- 合并单元格 -->
</tr>
</table>
</body>
</html>";
paragraph.AppendHTML(htmlWithTable);
string outputPath = @"HtmlWithTable.rtf";
doc.SaveToFile(outputPath, FileFormat.Rtf);
Console.WriteLine($"含表格的RTF生成成功:{outputPath}");
}
}

注意:
- 本地图片需提供绝对路径(或相对路径,确保程序有权限访问);
- 网络图片需保证程序能访问外网,若无法访问可先下载到本地再引用;
- Free Spire.Doc 支持 JPG、PNG、GIF、BMP等常见图片格式,Base64 图片需确保编码正确。
示例3:批量转换------文件夹中所有HTML文件转RTF
适用于需要批量处理多个HTML文件的场景(如批量导出报表)。
csharp
class Program
{
static void Main()
{
// 仅需修改这两个路径(输入HTML文件夹、输出RTF文件夹)
string inputFolder = @"D:\HTML文件";
string outputFolder = @"D:\RTF输出";
try
{
// 检查输入文件夹,不存在则提示退出
if (!Directory.Exists(inputFolder))
{
Console.WriteLine($"输入文件夹不存在:{inputFolder}");
return;
}
// 自动创建输出文件夹(不存在时)
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputFolder);
Console.WriteLine($"已创建输出文件夹:{outputFolder}");
}
// 获取所有.html文件(不包含子文件夹)
string[] htmlFiles = Directory.GetFiles(inputFolder, "*.html");
if (htmlFiles.Length == 0)
{
Console.WriteLine("输入文件夹中未找到HTML文件!");
return;
}
Console.WriteLine($"找到{htmlFiles.Length}个HTML文件,开始转换...");
// 批量转换核心逻辑
foreach (string htmlFile in htmlFiles)
{
string fileName = Path.GetFileName(htmlFile);
try
{
// 每个文件创建独立Document实例(自动释放资源)
using (Document doc = new Document())
{
// 直接加载HTML(无额外配置)
doc.LoadFromFile(htmlFile, FileFormat.Html, XHTMLValidationType.None);
// 构建输出路径(原文件名+(.rtf)后缀)
string rtfPath = Path.Combine(outputFolder, Path.ChangeExtension(fileName, ".rtf"));
// 直接保存为RTF(无额外配置)
doc.SaveToFile(rtfPath, FileFormat.Rtf);
}
Console.WriteLine($"✅ 成功:{fileName}");
}
catch (Exception ex)
{
Console.WriteLine($"❌ 失败:{fileName} → 原因:{ex.Message}");
}
}
Console.WriteLine("\n转换完成!输出路径:" + outputFolder);
}
catch (Exception ex)
{
Console.WriteLine($"程序异常:{ex.Message}");
}
}
}
注意事项:免费版有页面限制,不适合大型文档。
总结
Free Spire.Doc for .NET 提供了简单、高效的HTML到RTF转换方案,无需依赖Office,支持文本、样式、图片、表格等核心元素的完整保留。通过本文的示例,开发者可快速实现基础转换和批量转换等场景需求。
整体而言,该组件是.NET平台中文档格式转换的优质选择,广泛适用于办公自动化、报表系统、文档导出等场景。更多示例可查看该中文教程合集。