C# ppt导出pdf

static public bool ConverterToPdf(string _lstrInputFile, string _lstrOutFile)
        {
            Microsoft.Office.Interop.PowerPoint.Application lobjPowerPointApp = null;
            Microsoft.Office.Interop.PowerPoint.Presentation lobjppt = null;
            object lobjMissing = System.Reflection.Missing.Value;
            object lobjSaveChanges = null;
            try
            {
                lobjPowerPointApp = new Microsoft.Office.Interop.PowerPoint.Application();
                lobjppt = lobjPowerPointApp.Presentations.Open(_lstrInputFile, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                lobjppt.SaveAs(_lstrOutFile, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoCTrue);
            }
            catch (Exception ex)
            {
                //其他日志操作;
                return false;
            }
            finally
            {
                if (lobjppt != null)
                {
                    lobjppt.Close();
                    Marshal.ReleaseComObject(lobjppt);
                    lobjppt = null;
                }
                if (lobjPowerPointApp != null)
                {
                    lobjPowerPointApp.Quit();
                    Marshal.ReleaseComObject(lobjPowerPointApp);
                    lobjPowerPointApp = null;
                }
                //主动激活垃圾回收器,主要是避免超大批量转文档时,内存占用过多,而垃圾回收器并不是时刻都在运行!
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return true;
        }

 bool a= ConverterToPdf(@"D:\1.pptx", @"D:\2.pdf");

 

要引入Microsoft.Office.Interop.PowerPoint; 版本12.0.0.0;

CS1752 无法嵌入互操作类型“XXXXClass“。请改适用的接口

在打开.Net项目时 应用其他库有的提示这个问题,这个原因是由于引入的属性中,有一个“嵌入操作类型”设置  设置为false即可。

posted on 2022-12-20 13:37  漫思  阅读(49)  评论(0编辑  收藏  举报

导航