• PPT导出为图片


    使用Aspose组件导出

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

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

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

     1     internal class PptToImagesConverter
     2     {
     3         private const string ImageExtension = ".png";
     4         public bool ConvertToImages(string pptFile, string exportImagesFolder)
     5         {
     6             using (Presentation pres = new Presentation(pptFile))
     7             {
     8                 int desiredX = 1200;
     9                 int desiredY = 800;
    10 
    11                 float scaleX = (float)(1.0 / pres.SlideSize.Size.Width) * desiredX;
    12                 float scaleY = (float)(1.0 / pres.SlideSize.Size.Height) * desiredY;
    13 
    14                 foreach (ISlide sld in pres.Slides)
    15                 {
    16                     Bitmap bmp = sld.GetThumbnail(scaleX, scaleY);
    17 
    18                     string slidePath = Path.Combine(exportImagesFolder,$"Slide_{sld.SlideNumber}.{ImageExtension}");
    19                     bmp.Save(slidePath, System.Drawing.Imaging.ImageFormat.Png);
    20                 }
    21             }
    22 
    23             return true;
    24         }
    25     }

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

    使用Interop.PowerPoint导出

    也是在Nuget中搜索并安装:

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

     1     internal class PptToImagesConverter1
     2     {
     3         private const string PASSWORD_MARK = "::PASSWORD::";
     4         private const string ImageExtension = ".png";
     5         public bool ConvertToImages(string pptFile, string exportImagesFolder)
     6         {
     7             var tempPpt = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + Path.GetExtension(pptFile));
     8             File.Copy(pptFile, tempPpt);
     9             Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
    10             Microsoft.Office.Interop.PowerPoint.Presentation presentation = app.Presentations.Open(tempPpt + PASSWORD_MARK, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
    11             int desiredX = 1200;
    12             int desiredY = 800;
    13             var slides = ((Microsoft.Office.Interop.PowerPoint.Presentation)presentation).Slides.Cast<Microsoft.Office.Interop.PowerPoint.Slide>().ToList();
    14             Parallel.ForEach(slides, slide =>
    15             {
    16                 string slidePath = Path.Combine(exportImagesFolder, "Slide-" + slide.SlideIndex + ImageExtension);
    17                 slide.Export(slidePath, ImageExtension, desiredX, desiredY);
    18             });
    19             return true;
    20         }
    21     }
  • 相关阅读:
    hanlp在jdk11 maven8编译后在jdk8 报错
    Linux top命令输出到文件——持续输出某个进程的信息
    maven with depend
    解决mount时发生错误wrong fs type, bad option, bad superblock
    leetcode中 01背包问题相关汇总
    leetcode刷题总结901-950
    Xgboost如何处理缺失值/
    leetcode刷题总结851-900
    leetcode刷题总结801-850
    leetcode刷题总结751-800
  • 原文地址:https://www.cnblogs.com/kybs0/p/12362331.html
Copyright © 2020-2023  润新知