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

      利用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/11578.html

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

文章版权:梦飞科技所发布的内容,部分为原创文章,转载请注明来源,网络转载文章如有侵权请联系我们!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
建站教程投稿分享

虚拟主机应用场景

2022-10-24 15:28:48

建站教程投稿分享

DNS分离解析技术练习

2022-10-25 16:44:30

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
客户经理
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索

梦飞科技 - 最新云主机促销服务器租用优惠