夫天地者,万物之逆旅;光阴者,百代之过客。而浮生若梦,为欢几何?
C# PPT分页转PDF

废话不说,上源码

开发环境需要安装office 2010以上版本,笔者本地安装的是office 2013  。

项目需要引入 Microsoft.Office.Interop.PowerPoint.dll 和 Office.dll

分页转换代码如下:

/// <summary>
        /// ppt 分页转 pdf
        /// </summary>
        /// <param name="sourcePath">ppt文件路径</param>
        /// <param name="saveDirectory">pdf分片文件保存路径</param>
        /// <param name="sliceSize">分片大小</param>
        /// <param name="totalPage">ppt总页码</param>
        /// <param name="sliceNum">分片数量</param>
        public void PPtToPdf(string sourcePath, string saveDirectory, int sliceSize, out int totalPage, out int sliceNum)
        {
            totalPage = 0;
            sliceNum = 0;
            object oMissing = System.Reflection.Missing.Value;
            //启动Word程序
            Microsoft.Office.Interop.PowerPoint.Application pptApp = null;
            try
            {
                //启动Word程序
                pptApp = new Microsoft.Office.Interop.PowerPoint.Application();
                
                var pptdoc = pptApp.Presentations.Open(sourcePath,
                   Microsoft.Office.Core.MsoTriState.msoTrue,
                                                                    Microsoft.Office.Core.MsoTriState.msoTrue,
                                                                    Microsoft.Office.Core.MsoTriState.msoFalse);
                totalPage = pptdoc.Slides.Count;//获取ppt文件总页码
                sliceNum = totalPage % sliceSize == 0 ? (totalPage / sliceSize) : (totalPage / sliceSize) + 1;//计算分片文件数量
                for (int i = 0; i < sliceNum; i++)
                {
                    //下面2行设置每次将 第 多少页到 第多少页转换成一个pdf小文件
                    var ranges = pptdoc.PrintOptions.Ranges;
                     var end = (i * sliceSize) + sliceSize;
                                        if (end > totalPage) end = totalPage;
                                        var range = ranges.Add((i * sliceSize) + 1, end);

                    pptdoc.ExportAsFixedFormat(string.Format("{0}\\{1}-{2}-{3}.pdf", saveDirectory, (i * sliceSize) + 1, totalPage, sliceNum),
                                                             Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF,
                                                             Microsoft.Office.Interop.PowerPoint.PpFixedFormatIntent.ppFixedFormatIntentPrint,
                                                             Microsoft.Office.Core.MsoTriState.msoFalse,
                                                             Microsoft.Office.Interop.PowerPoint.PpPrintHandoutOrder.ppPrintHandoutVerticalFirst,
                                                             Microsoft.Office.Interop.PowerPoint.PpPrintOutputType.ppPrintOutputSlides,
                                                             Microsoft.Office.Core.MsoTriState.msoFalse, range,
                                                             Microsoft.Office.Interop.PowerPoint.PpPrintRangeType.ppPrintSlideRange, string.Empty,
                                                             true, true, true, true, false, oMissing);
                }

                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (pptApp != null)
                {
                    pptApp.Quit();
                }
            }
        }


作者:暗夜余晖

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

0

支持

0

反对

posted @2018-1-21  拜读(1662)

评论列表

评论内容:



喜欢请打赏

支付宝 微信

请放心支付