Tips for Exporting slides from Powerpoint in C#

August 31, 2005 | Uncategorized | 7 Comments

I recently needed to export slides from powerpoint as images to use in
another application. I ran into two issues along the way that I thought
I’d share in hopes that it will speed someone else’s development time.

Office Versions
Some of the end users had Office XP (2002) and some had Office 2003.
The Office API is different for for 2002 and 2003 so I had to use the
provider pattern for the two versions. The export slide functionality
looks like this:

Office 2002

using Microsoft.Office.Core;
using PowerPoint;
ApplicationClass pptApplication = new ApplicationClass();
Presentation pptPresentation = pptApplication.Presentations.Open("myfile.ppt", MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
pptPresentation.Slides.Item(1).Export("slide.jpg", "jpg", 320, 240);

Office 2003

using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
ApplicationClass pptApplication = new ApplicationClass();
Presentation pptPresentation = pptApplication.Presentations.Open("myfile.ppt", MsoTriState.msoFalse,
MsoTriState.msoFalse, MsoTriState.msoFalse);

pptPresentation.Slides.Item[1].Export("slide.jpg", "jpg", 320, 240);

The main differences are (1) the namespaces have changed and (2) the
Slides collection is accessed as a method in 2002 and as an indexed
property in 2003. Once the code differences are determined, I created
an abstract PowerpointExporter class and a class for each office
version I had to support.

Image Output Quality
I found that the above code produced fairly low quality JPG images.
There was high degree of artifacting around text, the text wasn’t
antialiased and the images our artists used as background didn’t look
very good. Since there is no way to tell Powerpoint what JPG
compression to use, I changed the code to output as lossless PNG files
and I changed the output to the size of the background images in the
original Powerpoint. For Office 2003, the code looked like this:

pptPresentation.Slides.Item[1].Export("slide.png", "PNG", 1024, 768);

After the extraction, I resized and compressed the PNGs into the JPGs I need and the slides look great.

7 responses to “Tips for Exporting slides from Powerpoint in C#”

  1. lamia says:

    Hell,
    I’d like to know how can I export slide from PowerPoint to .Tiff file with Microsoft office 2007.
    Think you

  2. Rohith says:

    Hii thanks for this wonderful resource can you tell me the source for this information in icrosoft website ??

  3. Julian says:

    Hi, thank you very much for this tips. I am implementing a prototype to export PPT to static images using your snippet. It is awesome.

  4. Mr.hack says:

    is it possible to convert a pptx slides to images using java code. Please give some guide line. Thanks in advanced.

  5. Neel Kamal says:

    Can we do this without Ms-Office ?
    This code is asking MS-Office to be installed to the server .

    Or can we have any other way to use the same code without installing MS-Office on the server.
    Please help me,
    Regards
    Neel

  6. Vikas says:

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

    FileInfo fileInfo = new FileInfo(@”E:\VIKAS\TrainingModuleEnhancements.pptx”);
    Application pptApplication = new Application();
    Presentation pptPresentation = pptApplication.Presentations.Open2007(fileInfo.FullName, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);

    for (int i =1; i <= pptPresentation.Slides.Count; i++)
    {
    pptPresentation.Slides[i].Export("slide" + i + ".png", "png", 1280, 786);

    pptPresentation.SaveCopyAs(@"E:\Test\.png", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPNG, MsoTriState.msoTrue);

    }

  7. nirmal says:

    can u suggest any other method without using interop? or spire ?

Leave a Reply

Hi, I'm John Dyer. In my day job, I build websites and create online seminary software for a seminary in Dallas. I also like to release open source tools including a pretty popular HTML5 video player and build tools that help people find best bible commentaries and do bible study. And just for fun, I also wrote a book on the theology of technology and media.

Fork me on GitHub

Social Widgets powered by AB-WebLog.com.