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源码地址

软件运行截图

相关推荐
旋律翼22 小时前
Qt Bridges for C# 深度技术解析
开发语言·qt·c#
吴可可1232 小时前
判断多段线方向的鞋带公式法
c#
qq_454245033 小时前
BasicMethod.Map 设计框架:8 个并行基础模块的数学根基与实现
数据结构·算法·c#
Byron Loong4 小时前
【c#】Bitmap释放之 大象与蚊子
开发语言·c#
临风细雨5 小时前
从 Bun 的 Rust 重写,看 C# 如何重建 AI 基础设施层
人工智能·rust·c#
吴可可1235 小时前
C# CAD二次开发自定义实体实现
c#
不坑老师6 小时前
Excel总表如何按班级拆分并分别发送?
前端·数据库·人工智能·excel·ppt·office
z落落7 小时前
C# WinForm 线程池与事件等待+进度条暂停恢复实战案例
开发语言·c#
AC赳赳老秦8 小时前
Excel 动态仪表盘制作:用 OpenClaw 自动处理数据、生成交互式图表并实时更新仪表盘
大数据·服务器·后端·测试用例·excel·deepseek·openclaw
酷酷的身影8 小时前
Managers/APConfigManager.cs
开发语言·ui·c#