找回密码
 立即注册
首页 业界区 业界 使用 C# 实现 RTF 文档转 PDF 格式

使用 C# 实现 RTF 文档转 PDF 格式

赖娅闺 昨天 11:55
RTF(Rich Text Format)作为跨平台富文本格式,常用于文档编辑与数据交换,而 PDF 因格式稳定、跨设备兼容性强,更适合文档分发和归档。在 .NET 开发中,实现 RTF 到 PDF 的转换是常见需求,本文将介绍如何使用免费库 Free Spire.Doc for .NET 实现该转换过程。
安装: Free Spire.Doc 是一款支持 RTF、Word 等文档的格式转换的免费 .NET 库 (有篇幅限制),可直接通过 NuGet 包管理器安装:
  1. Install-Package FreeSpire.Doc
复制代码
RTF 转 PDF  核心实现代码

场景1:单个RTF文件转换为PDF(基础版)

核心逻辑为“加载RTF文件 → 保存为PDF格式”,代码简洁易实现:
  1. using System;
  2. using Spire.Doc;
  3. namespace RtfToPdfConverter
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             try
  10.             {
  11.                 // 初始化Document对象
  12.                 Document document = new Document();
  13.                 // 加载本地RTF文件(替换为实际文件路径)
  14.                 string rtfFilePath = @"C:\Files\test.rtf";
  15.                 document.LoadFromFile(rtfFilePath, FileFormat.Rtf);
  16.                 // 保存为PDF文件(替换为输出路径)
  17.                 string pdfFilePath = @"C:\Files\test.pdf";
  18.                 document.SaveToFile(pdfFilePath, FileFormat.Pdf);
  19.                 // 释放资源
  20.                 document.Close();
  21.                 Console.WriteLine("RTF转PDF成功!输出路径:" + pdfFilePath);
  22.             }
  23.             catch (Exception ex)
  24.             {
  25.                 Console.WriteLine("转换失败:" + ex.Message);
  26.             }
  27.         }
  28.     }
  29. }
复制代码
场景2:批量转换RTF文件(进阶版)

针对多文件转换场景,可遍历指定目录下的RTF文件批量处理:
  1. using System;
  2. using System.IO;
  3. using Spire.Doc;
  4. namespace BatchRtfToPdfConverter
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             // 源RTF文件目录、PDF输出目录(替换为实际路径)
  11.             string sourceDir = @"C:\Files\RTF_Source";
  12.             string outputDir = @"C:\Files\PDF_Output";
  13.             // 检查并创建输出目录
  14.             if (!Directory.Exists(outputDir))
  15.             {
  16.                 Directory.CreateDirectory(outputDir);
  17.             }
  18.             try
  19.             {
  20.                 // 获取目录下所有RTF文件
  21.                 string[] rtfFiles = Directory.GetFiles(sourceDir, "*.rtf");
  22.                 if (rtfFiles.Length == 0)
  23.                 {
  24.                     Console.WriteLine("源目录下未找到RTF文件!");
  25.                     return;
  26.                 }
  27.                 // 批量转换
  28.                 int successCount = 0;
  29.                 foreach (string rtfFile in rtfFiles)
  30.                 {
  31.                     try
  32.                     {
  33.                         Document document = new Document();
  34.                         document.LoadFromFile(rtfFile, FileFormat.Rtf);
  35.                         // 生成同名PDF文件
  36.                         string fileName = Path.GetFileNameWithoutExtension(rtfFile);
  37.                         string pdfFile = Path.Combine(outputDir, $"{fileName}.pdf");
  38.                         document.SaveToFile(pdfFile, FileFormat.Pdf);
  39.                         document.Close();
  40.                         successCount++;
  41.                         Console.WriteLine($"成功转换:{rtfFile} → {pdfFile}");
  42.                     }
  43.                     catch (Exception ex)
  44.                     {
  45.                         Console.WriteLine($"转换失败 {rtfFile}:{ex.Message}");
  46.                     }
  47.                 }
  48.                 Console.WriteLine($"\n批量转换完成!成功:{successCount} 个,失败:{rtfFiles.Length - successCount} 个");
  49.             }
  50.             catch (Exception ex)
  51.             {
  52.                 Console.WriteLine("批量转换异常:" + ex.Message);
  53.             }
  54.         }
  55.     }
  56. }
复制代码
常见问题与解决方案

问题1:加载 RTF 文件时报错


  • 可能原因:文件路径错误/文件损坏
  • 解决方案:检查路径正确性,验证 RTF 文件可正常打开
转换后 PDF 格式错乱


  • 可能原因:RTF 含特殊格式/字体
  • 解决方案:确保运行环境安装了 RTF 中使用的字体
Free Spire.Doc for .NET 为 RTF 到 PDF 的转换提供了可行的免费解决方案,适合文档规模较小、基础转换场景。

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册