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

软件运行截图

相关推荐
A_nanda1 小时前
c# MOdbus rto读写串口,如何不相互影响
算法·c#·多线程
码云数智-园园3 小时前
使用 C# 将 PowerPoint 演示文稿高效转换为 PDF 格式
c#
PfCoder4 小时前
WinForm真入门(23)---PictureBox 控件详细用法
开发语言·windows·c#·winform
gc_22996 小时前
C#学习调用OpenMcdf模块解析ole数据的基本用法(1)
c#·ole·openmcdf
开开心心就好8 小时前
键盘改键工具免安装,自定义键位屏蔽误触
java·网络·windows·随机森林·计算机外设·电脑·excel
MM_MS11 小时前
Halcon图像点运算、获取直方图、直方图均衡化
图像处理·人工智能·算法·目标检测·计算机视觉·c#·视觉检测
老骥伏枥~12 小时前
C# 控制台:Console.ReadLine / WriteLine
开发语言·c#
fqbqrr13 小时前
2601Mfc,自动化excel
自动化·excel·mfc
tlwlmy13 小时前
python excel图片批量导出
开发语言·python·excel
PfCoder1 天前
C#中定时器之System.Timers.Timer
c#·.net·visual studio·winform