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:\test.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     }

 

posted @ 2020-02-25 16:21  唐宋元明清2188  阅读(509)  评论(1编辑  收藏  举报