C# 实现PPT 每一页转成图片

zhuan https://www.cnblogs.com/ywtk/archive/2013/09/02/3295973.html

 版本 2019-09-30

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.XtraPrinting.Native;
using O2S.Components.PDFRender4NET;
using System.Drawing.Imaging;
using System.IO;

namespace WorkMonitor.CycleForm
{
    /// <summary>
    /// frmTest.xaml 的交互逻辑
    /// </summary>
    public partial class frmTest : Window
    {
        public frmTest()
        {
            InitializeComponent();


            pptToImg(@"C:UsersAdministratorDesktopAAA-FileCycle4.ppt", @"C:UsersAdministratorDesktopAAA-FileCycle", "testPPT", ImageFormat.Png);

            ConvertPDF2Image(@"C:UsersAdministratorDesktopAAA-FileCycle4.pdf", @"C:UsersAdministratorDesktopAAA-FileCycle", "testPDF", ImageFormat.Jpeg, 5);
 
        }

        /// <summary>
        /// 将PPT转换为图片
        /// </summary>
        /// <param name="pptPath"></param>
        /// <param name="imgPath"></param>
        private void pptToImg(string pptPath, string imgPath, string imageName, ImageFormat imageFormat)
        {
            var app = new Microsoft.Office.Interop.PowerPoint.Application();

            var ppt = app.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

            var index = 0;

            var fileName = pptPath;

            foreach (Microsoft.Office.Interop.PowerPoint.Slide slid in ppt.Slides)
            {
                ++index;
                //设置图片大小
                slid.Export(imgPath + string.Format("{0}{1}.{2}", imageName , index.ToString(),imageFormat.ToString()) ,imageFormat.ToString(), 1024, 768);
                //根据屏幕尺寸。设置图片大小
                //slid.Export(imgPath+string.Format("page{0}.jpg",index.ToString()), "jpg", Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            }

            //释放资源
            ppt.Close();
            app.Quit();
            GC.Collect();
        }

        /// <summary> 
        /// 将PDF文档转换为图片的方法 
        /// </summary> 
        /// <param name="pdfInputPath">PDF文件路径</param> 
        /// <param name="imageOutputPath">图片输出路径</param> 
        /// <param name="imageName">生成图片的名字</param>  
        /// <param name="imageFormat">设置所需图片格式</param> 
        /// <param name="definition">设置图片的清晰度,数字越大越清晰</param> 
        public   void ConvertPDF2Image(string pdfInputPath, string imageOutputPath,  string imageName,  ImageFormat imageFormat, int definition)
        {

            PDFFile pdfFile = PDFFile.Open(pdfInputPath);

            if (!Directory.Exists(imageOutputPath))
            {

                Directory.CreateDirectory(imageOutputPath);

            }

          
            
            // start to convert each page

            for (int i = 1; i <= pdfFile.PageCount; i++)
            {

                try
                {
                    System.Drawing.Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);

                    pageImage.Save(imageOutputPath + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat);

                    pageImage.Dispose();
                }
                catch (Exception)
                {

                }

            }

            pdfFile.Dispose();

        }

    }
}

  

版本 2019-09-29

要实现PPT转图片,首先需要引用两个DLL。

我这里用的这个这个版本

Microsoft.Office.Interop.PowerPoint 12.0

Microsoft Office 12.0 object Library

如下图:

代码如下:

复制代码
 private void pptToImg(string pptPath, string imgPath)
        {
            var app = new Microsoft.Office.Interop.PowerPoint.Application();

            var ppt = app.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

            var index = 0;

            var fileName = Path.GetFileNameWithoutExtension(pptPath);

            foreach (Microsoft.Office.Interop.PowerPoint.Slide slid in ppt.Slides) 
            {
                ++index;
                //设置图片大小
                slid.Export(imgPath+string.Format("page{0}.png",index.ToString()), "png", 1024, 768);
                //根据屏幕尺寸。设置图片大小
                //slid.Export(imgPath+string.Format("page{0}.jpg",index.ToString()), "jpg", Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            }

            //释放资源
            ppt.Close();
            app.Quit();
            GC.Collect();
        }
复制代码
 
 
 
原文地址:https://www.cnblogs.com/lhlong/p/11609384.html