Word、Excel、PPT文件转PDF文件(C#)

一、添加依赖

为wpf项目引用Microsoft.Office.Interop.Excel、Microsoft.Office.Interop.PowerPoint、Microsoft.Office.Interop.Word、Office,依赖文件已经打到源代码包里了。

二、先定义一些命名空间

cs 复制代码
    using Word = Microsoft.Office.Interop.Word;
    using Excel = Microsoft.Office.Interop.Excel;
    using PPT = Microsoft.Office.Interop.PowerPoint;

三、Word转PDF

cs 复制代码
        /// <summary>
        /// Word转PDF
        /// </summary>
        /// <param name="strWordPath">待转化的Word文件</param>
        /// <param name="strPdfPath">转换后的pdf文件路径</param>
        /// <returns></returns>
        public static bool ConverterWordToPdf(string strWordPath, string strPdfPath)
        {
            Word.Application appWord = null;
            Word.Document    docWord = null;

            try
            {
                appWord = new Word.Application();
                docWord = appWord.Documents.Open(strWordPath);
                docWord.ExportAsFixedFormat(strPdfPath, Word.WdExportFormat.wdExportFormatPDF);

                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                if(docWord != null)
                {
                    docWord.Close(false);
                }
                if(appWord != null)
                {
                    appWord.NormalTemplate.Saved = true;
                    appWord.Quit(false);
                }
            }
        }

四、Excel转PDF

cs 复制代码
public static bool ConverterExcelToPdf(string strExcelPath, string strPdfPath)
        {
            Excel.Application appExcel = null;
            Excel.Workbook    docExcel = null;

            try
            {
                appExcel = new Excel.Application();
                docExcel = appExcel.Workbooks.Open(strExcelPath, System.Reflection.Missing.Value, true);
                docExcel.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, strPdfPath);

                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                if (docExcel != null)
                {
                    docExcel.Close(false);
                }
                if (appExcel != null)
                {
                    appExcel.Quit();
                }
            }
        }

五、PPT转PDF

cs 复制代码
public static bool ConverterPPTToPdf(string strPPTPath, string strPdfPath)
        {
            PPT.Application appPPT = null;
            PPT.Presentation pptPresentation = null;

            try
            {
                appPPT = new PPT.Application();
                pptPresentation = appPPT.Presentations.Open(strPPTPath, 
                    Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
                pptPresentation.SaveAs(strPdfPath, PPT.PpSaveAsFileType.ppSaveAsPDF);

                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                if (pptPresentation != null)
                {
                    pptPresentation.Close();
                }
                if (appPPT != null)
                {
                    appPPT.Quit();
                }
            }
        }

六、我写了一个简单的转换代码,源码下载地址如下

word、excel、ppt转pdf源码地址

软件运行截图

相关推荐
CallZhang2101 小时前
Vision Master的C#脚本与opencv联合编程
opencv·计算机视觉·c#·视觉检测
AI视觉网奇1 小时前
kafka 冲突解决 kafka安装
c#·linq
hqwest2 小时前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
萘柰奈2 小时前
Unity进阶--C#补充知识点--【Unity跨平台的原理】Mono与IL2CPP
unity·c#·游戏引擎
程序设计实验室3 小时前
StarBlog v1.3.0 新版本,一大波更新以及迁移服务器部署
c#·aspnetcore·starblog番外
淡海水3 小时前
【原理】Struct 和 Class 辨析
开发语言·c++·c#·struct·class
淡海水4 小时前
【原理】Unity GC 对比 C# GC
unity·c#·gc·垃圾回收
张人玉4 小时前
C#读取文件, IO 类属性及使用示例
microsoft·c#
咕白m6259 小时前
通过 C# 高效提取 PDF 文本的完整指南
后端·c#
hqwest12 小时前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计