# C# 将word/ppt文档转换为Pdf的三种方法

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/11578.html | 发布：2022-10-24 | 更新：2022-10-24 | 分类：建站教程、投稿分享

利用office自带的COM类型库组件实现转换Pdf功能。只要安装了office的服务器上都可以调用，不需要额外的第三方组件，功能也更加丰富和强大，几乎可以不受限制的操作office所有类型文件。缺点是部署问题多，发布到客户服务器进行调试的话问题很多。禁忌：1，开发的时候调用,不同office版本的COM组件，比如Microsoft.Office.Interop.Word是v14，那ppt、excel等组件都要统一版本，不然问题很多；2，部署的服务器上只能安装一个版本的office，比如开发时调用的Office 2010，那部署的服务器就只能装 office 2010，建议不要混装各种版本来匹配组件型号，最终会导致哪个都不能用；3，常见的故障问题和解决方法附录在该节末尾。

 

（1）利用Microsoft.Office.Interop.Word实现word转换pdf.

 

首先安装office 2010或其他更高版本。

 

添加引用Microsoft.Office.Interop. Word：

 

C#代码中添加引用：

 

using System.Text;

  

using Microsoft.Office.Interop.Word;

 

using WdExportFormat = Microsoft.Office.Interop.Word.WdExportFormat;

  

创建WordToPdf方法：

 

/// <summary>

 

/// 把Word文件转换成pdf文件

 

/// </summary>

 

/// <param name="sourcePath">需要转换的文件路径和文件名称</param>

 

/// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>

 

/// <returns>成功返回true,失败返回false</returns>

 

public static bool WordToPdf(stringsourcePath, string targetPath)

 

{

  

bool result = false;

 

Microsoft.Office.Interop.Word.WdExportFormatwdExportFormatPDF = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;//转换格式1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式

 

object missing = Type.Missing;

 

Microsoft.Office.Interop.Word.ApplicationClassapplicationClass = null;

 

Document document = null;

 

try

 

{

 

applicationClass = newMicrosoft.Office.Interop.Word.ApplicationClass();

 

object inputfileName = sourcePath;//需要转格式的文件路径

 

string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称

 

WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式

 

bool openAfterExport = false;//转换完成后是否打开

 

WdExportOptimizeForwdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进行导出，质量较高，生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出，质量较差，生成的文件大小较小。

 

WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全部内容（枚举）

 

int from = 0;//起始页码

 

int to = 0;//结束页码

 

WdExportItemwdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.导出文件有标记

 

bool includeDocProps = true;//指定是否包含新导出的文件在文档属性

 

bool keepIRM = true;//

 

WdExportCreateBookmarkswdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导出文件中创建书签,2.wdExportCreateHeadingBookmarks：标题和文本框导出的文件中创建一个书签，3.wdExportCreateWordBookmarks每个字的书签，其中包括除包含页眉和页脚中的所有书签导出的文件中创建一个书签。

 

bool docStructureTags = true;

 

bool bitmapMissingFonts = true;

 

bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)

 

document = applicationClass.Documents.Open(refinputfileName, ref missing, ref missing, refmissing, ref missing, ref missing, ref missing, refmissing, ref missing, ref missing, ref missing, refmissing, ref missing, ref missing, ref missing, refmissing);

 

if (document != null)

 

{

 

document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing);

 

}

 

result = true;

 

}

 

catch

 

{

 

result = false;

 

}

 

finally

 

{

 

if (document != null)

 

{

 

document.Close(ref missing, refmissing, ref missing);

 

document = null;

 

}

 

if (applicationClass != null)

 

{

 

applicationClass.Quit(ref missing, ref missing, ref missing);

 

applicationClass = null;

 

}

 

}

 

return result;

 

}

 

调用该方法：

 

CommonCls.ConvertPdf.WordToPdf(sourcefilepath, targetfilepath);

 

（2）利用Microsoft.Office.Interop.PowerPoint实现ppt转换pdf.

 

首先安装office 2010或其他更高版本。

 

添加引用Microsoft.Office.Interop. PowerPoint：

       

（3）利用Microsoft.Office.Interop.*组件实现转换pdf的常见问题和解决方法。

 

未加载错误

 

引用错误

 

调试正常，发布到部署服务器时无法转换

  

![C# 将word/ppt文档转换为Pdf的三种方法](https://www.mfisp.com/wp-content/uploads/2022/10/11471-5iLLPQ.jpg)

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
