如何通过 C# 将 PPT 文档转换为 PDF 格式
1. 安装 .NET 库Spire.Presentation 是一款专门用于处理 PowerPoint 文档的 .NET 组件无需依赖 Microsoft Office 或 PowerPoint 客户端即可完成 PPT 文档的读取、编辑和格式转换。推荐通过 NuGet 包管理器安装步骤如下打开 Visual Studio创建任意 C# 项目如Console App右键项目→“管理NuGet程序包”搜索“Spire.Presentation”选择对应版本安装也可通过NuGet命令行安装Install-Package Spire.Presentation2. 基础示例单个 PowerPoint 文件转 PDF这是最常用的场景支持 PPT/PPTX 格式输入直接通过SaveToFile方法输出为 PDF 文件using System; using Spire.Presentation; namespace PptToPdfDemo { class Program { static void Main(string[] args) { try { // 1. 定义文件路径 string pptFilePath D:\Demo\source.pptx; // 输入PPT路径 string pdfFilePath D:\Demo\output.pdf; // 输出PDF路径 // 2. 加载PPT文档 Presentation presentation new Presentation(); presentation.LoadFromFile(pptFilePath); // 3. 转换为PDF并保存 // 可选参数PDF导出选项如压缩、权限等此处使用默认配置 presentation.SaveToFile(pdfFilePath, FileFormat.PDF); // 4. 释放资源关键避免内存泄漏 presentation.Dispose(); Console.WriteLine(PPT转PDF成功); } catch (Exception ex) { // 异常处理捕获文件不存在、格式不支持、权限不足等问题 Console.WriteLine($转换失败{ex.Message}); } } } }3. 批量转换转换多个 PowerPoint 文件为 PDF通过遍历文件夹实现批量转换适合处理大量 PPT 文件using System; using System.IO; using Spire.Presentation; namespace BatchPptToPdf { class Program { static void Main(string[] args) { // 源PPT文件夹路径 string pptFolderPath D:\Demo\PptFiles; // 输出PDF文件夹路径 string pdfFolderPath D:\Demo\PdfFiles; // 确保输出文件夹存在 if (!Directory.Exists(pdfFolderPath)) { Directory.CreateDirectory(pdfFolderPath); } // 遍历文件夹中的PPT/PPTX文件 string[] pptFiles Directory.GetFiles(pptFolderPath, *, SearchOption.TopDirectoryOnly) .Where(file file.EndsWith(.ppt, StringComparison.OrdinalIgnoreCase) || file.EndsWith(.pptx, StringComparison.OrdinalIgnoreCase)) .ToArray(); foreach (string pptFile in pptFiles) { try { // 获取文件名不含扩展名用于生成PDF文件名 string fileName Path.GetFileNameWithoutExtension(pptFile); string pdfFile Path.Combine(pdfFolderPath, ${fileName}.pdf); // 加载并转换 using (Presentation presentation new Presentation()) { presentation.LoadFromFile(pptFile); presentation.SaveToFile(pdfFile, FileFormat.PDF); } Console.WriteLine($已转换{pptFile} → {pdfFile}); } catch (Exception ex) { Console.WriteLine($转换失败 {pptFile}{ex.Message}); } } Console.WriteLine(批量转换完成); } } }4. 进阶示例将 PowerPoint 转换为加密的 PDF还可以在转换时直接加密保护 PDF 文件并为 PDF 设置权限using Spire.Presentation; using Spire.Presentation.External.Pdf; namespace ConvertToEncryptedPdf { class Program { static void Main(string[] args) { // 定义明确的文件路径建议替换为你的实际路径 string inputPptPath C:\Users\Administrator\Desktop\Input.pptx; string outputPdfPath C:\Users\Administrator\Desktop\ToEncryptedPdf.pdf; // 使用using语句自动释放Presentation资源优于手动Dispose try { using (Presentation presentation new Presentation()) { // 加载PPT文件 presentation.LoadFromFile(inputPptPath); // 获取PDF保存选项 SaveToPdfOption option presentation.SaveToPdfOption; // 设置PDF密码和权限 // 参数说明 // 1. 用户密码打开PDF需要输入的密码abc-123 // 2. 所有者密码用于修改PDF权限的密码owner-456可自定义 // 3. PDF权限允许打印 允许填写表单 // 4. 加密级别默认128位高安全性 option.PdfSecurity.Encrypt(abc-123, owner-456, PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields, PdfEncryptionKeySize.Key128Bit); // 保存为加密PDF presentation.SaveToFile(outputPdfPath, FileFormat.PDF, pdfOptions); Console.WriteLine(PPT已成功转换为加密PDF); Console.WriteLine($输出路径{outputPdfPath}); } } catch (Exception ex) { // 捕获所有可能的异常并提示 Console.WriteLine($转换失败{ex.Message}); } // 暂停控制台便于查看结果 Console.ReadLine(); } } }5. 关键注意事项格式兼容性支持输入格式PPT、PPTX、PPS、PPSX 等复杂PPT元素如3D图表、自定义动画、嵌入式视频转换后可能丢失或显示异常。资源释放必须通过Dispose()方法释放Presentation对象或使用using语句推荐否则易导致内存泄漏尤其批量转换时需注意。权限问题确保程序对输入/输出路径有读写权限否则会抛出UnauthorizedAccessException。6. 替代方案参考LibreOffice SDK免费开源需部署 LibreOffice 服务API 较复杂OpenXML SDK iTextSharp仅支持 PPTXOpenXML 格式需自行处理布局转换开发成本高GroupDocs.Conversion有免费额度云原生支持但依赖网络。