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

软件运行截图

相关推荐
SunnyDays10116 小时前
使用 Python 提取 Word 文档中的表格数据
python·word
cjp5608 小时前
012.UG二次开发,UG管道服务器与WPF管道通讯,传输JSON数据
c#
jiushidt10 小时前
ArcGIS Pro Add‑in 打包流程详解
arcgis·c#·.net·arcgispro
weixin_4166600710 小时前
DeepSeek生成的数学公式怎么导出到Word并保持可编辑?
word·deepseek
Zzq_Fighting12 小时前
Excel批量匹配数据
excel
蝶恋舞者12 小时前
Word 实现「图 X」序号自动递增 和交叉引用
word
E_ICEBLUE12 小时前
Python 实现 Excel 转 Markdown,支持工作表、单元格区域和批量处理
python·excel·markdown·格式转换
拆房老料12 小时前
ONLYOFFICE 集群部署实战:9.3 多实例方案与 9.4 架构变化
开源·word·excel·开源软件·ppt
ms365copilot14 小时前
Excel Copilot挖掘数据洞察,不用精通函数也能做数据分析 (1)
数据分析·excel·copilot
小羊没烦恼!15 小时前
Office文件的奥秘——.NET平台下不借助Office实现Word、Powerpoint等文件的解析(完)
java·大数据·前端·网络·word·powerpoint·.net