Sunday, November 2, 2014

Convert PowerPoint slides to Images in C#

One of the challenges I have come across is to have a component that could batch convert PPT and PPTX files into images.

I know there are really good components out there like Aspose, but IT budget is always a constraint.
As we already had Office 2010 installed on our machine, I came up with a quick console application t perform the function.

Obviously, you need Office 2010 installed on the machine where you need to run the tool. I compiled the project for the .NET 2.0 Framework.

Just add some references to your Visual Studio 2010 solution like below:
Microsoft.Office.Interop.PowerPoint (from .NET, v14.0.0.0)
Microsoft.Office.Core (from COM, v14.0.0.0)
Others like System.Configuration (as I am using app.config)

After creating a basic C# Console application, add the references mentioned above.



Then, create a new configuration file and give it the following settings:
 <?xml version="1.0" encoding="utf-8" ?>  
 <configuration>  
  <appSettings>  
   <add key="WidthResolution" value="1600" />  
   <add key="HeightResolution" value="1200" />  
   <add key="ImageType" value="jpg" />  
   <add key="ImageExt" value=".jpg" />  
   <!-- Test -->  
   <add key="SourcePath" value="D:\Temp\" />  
   <add key="DestinationPath" value="D:\Temp\" />  
  </appSettings>  
 </configuration>  
The sourcePath and DestinationPath is predefined so that I only need to pass the file name to the program.
In the destination directory, a new folder will be created with the same name of the file, and the images created inside.

Finally in Program.cs, add the following lines for referencing:



using System.Configuration;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;

And in Main(), add the following code:
       string sourcePath = ConfigurationManager.AppSettings.Get("SourcePath");  
       string destinationPath = ConfigurationManager.AppSettings.Get("DestinationPath");  
       int width = int.Parse(ConfigurationManager.AppSettings.Get("WidthResolution"));  
       int height = int.Parse(ConfigurationManager.AppSettings.Get("HeightResolution"));  
       // The type as found in windows registry  
       string imageType = ConfigurationManager.AppSettings.Get("ImageType");  
       string imageExt = ConfigurationManager.AppSettings.Get("ImageExt");  
       string filePath = sourcePath + args[0];  
       Application pptApplication = new Application();  
       Presentation pptPresentation = pptApplication.Presentations  
         .Open(filePath, MsoTriState.msoFalse, MsoTriState.msoFalse  
         , MsoTriState.msoFalse);  
       string fileName = System.IO.Path.GetFileNameWithoutExtension(filePath);  
       int i = 1;  
       string imageName;  
       // Create directory if it does not exist  
       if (System.IO.Directory.Exists(destinationPath + fileName))  
       {  
         try  
         {  
           System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(destinationPath + fileName);  
           foreach (System.IO.FileInfo file in directory.GetFiles()) file.Delete();  
           foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);  
         }  
         catch (Exception e)  
         {  
           //Errors might happen especially if the file is being accessed by another user in thumbnail view, e.g. Thumbs.db  
           Console.WriteLine(e.ToString());  
         }  
       }  
       else  
       {  
         System.IO.Directory.CreateDirectory(destinationPath + fileName);  
       }  
       foreach (Slide slide in pptPresentation.Slides)  
       {  
         imageName = fileName + "_" + i.ToString("D4") + imageExt;  
         slide.Export(destinationPath + fileName + "\\" + imageName, imageType, width, height);  
         i += 1;  
       }  

To invoke the Program, just type in the command line:
YourProgramName.exe Test.pptx

No comments:

Related Posts Plugin for WordPress, Blogger...