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

软件运行截图

相关推荐
故事不长丨4 小时前
C#定时器与延时操作的使用
开发语言·c#·.net·线程·定时器·winform
阿桂有点桂4 小时前
C#使用VS软件打包msi安装包
windows·vscode·c#
c#上位机5 小时前
halcon图像增强之分段灰度拉伸2
c#·上位机·halcon·机器视觉
yue0085 小时前
C# Directory的用法介绍
开发语言·c#
❀͜͡傀儡师7 小时前
docker 部署OnlyOffice实现在线编辑Word文档
docker·容器·word
c#上位机7 小时前
halcon图像增强之自动灰度拉伸
图像处理·算法·c#·halcon·图像增强
白雪公主的后妈10 小时前
Auto CAD二次开发——Ribbon界面(1)
ribbon·c#·cad二次开发
Byron Loong12 小时前
【C#】 RSA 密钥生成工具
c#
yngsqq13 小时前
二维异形排版、二维装箱(NPF碰撞检测)——CAD c#二次开发
开发语言·javascript·c#
切糕师学AI14 小时前
.NET 如何引用两个不同版本的dll?
c#·.net