1. Dotnet9首页
  2. .NET
  3. .NET相关

PPT导出为图片

使用Aspose组件导出

Aspose有Aspose.Slides.dll,可以无需安装office,进行读写PPT文件。

Aspose可以通过Aspose.Slides.NET安装

PPT导出为图片

简单的导出图片demo,如下:

internal class PptToImagesConverter
    {
        private const string ImageExtension = ".png";
        public bool ConvertToImages(string pptFile, string exportImagesFolder)
        {
            using (Presentation pres = new Presentation(pptFile))
            {
                int desiredX = 1200;
                int desiredY = 800;

                float scaleX = (float)(1.0 / pres.SlideSize.Size.Width) * desiredX;
                float scaleY = (float)(1.0 / pres.SlideSize.Size.Height) * desiredY;

                foreach (ISlide sld in pres.Slides)
                {
                    Bitmap bmp = sld.GetThumbnail(scaleX, scaleY);

                    string slidePath = Path.Combine(exportImagesFolder,$"Slide_{sld.SlideNumber}.{ImageExtension}");
                    bmp.Save(slidePath, System.Drawing.Imaging.ImageFormat.Png);
                }
            }

            return true;
        }
    }

注:以上途径是没有购买过的dll,生成的图片会有水印。使用正版购买的,应该不会有问题。

PPT导出为图片

使用Interop.PowerPoint导出

也是在Nuget中搜索并安装:

PPT导出为图片

另,通过Presentations.Open(“c:\test.pptx::PASSWORD::”)可以解密PPT,从而导出图片。 

internal class PptToImagesConverter1
    {
        private const string PASSWORD_MARK = "::PASSWORD::";
        private const string ImageExtension = ".png";
        public bool ConvertToImages(string pptFile, string exportImagesFolder)
        {
            var tempPpt = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(pptFile));
            File.Copy(pptFile, tempPpt);
            Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
            Microsoft.Office.Interop.PowerPoint.Presentation presentation = app.Presentations.Open(tempPpt + PASSWORD_MARK, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
            int desiredX = 1200;
            int desiredY = 800;
            var slides = ((Microsoft.Office.Interop.PowerPoint.Presentation)presentation).Slides.Cast<Microsoft.Office.Interop.PowerPoint.Slide>().ToList();
            Parallel.ForEach(slides, slide =>
            {
                string slidePath = Path.Combine(exportImagesFolder, "Slide-" + slide.SlideIndex + ImageExtension);
                slide.Export(slidePath, ImageExtension, desiredX, desiredY);
            });
            return true;
        }
    }

原文出处:博客园【作者:唐宋元明清2188,http://www.cnblogs.com/kybs0/】

原文链接:https://www.cnblogs.com/kybs0/p/12362331.html

本文观点不代表Dotnet9立场,转载请联系原作者。

发表评论

登录后才能评论