Convert Powerpoint Presentation into Image, HTML and PDF

| May 29, 2010 | 3 Comments

Sometimes we need to save our Office Presentation file(PPT or PPTX) into other format through programming. We will learn here how to convert powerpoint presentation into Image, HTML & PDF. So here is the solution for the same.

Convert powerpoint into image, html and pdf

Convert powerpoint into image, html and pdf

Pre-requisites for the Program:

(1) Microsoft Office Powerpoint software installed in system.
(2) .NET Framework 2.0 or more installed.
(3) Office 2007 add-in installed for converting documents into PDF or XPS. You can get one from here.
(4) Add Reference to Microsoft.Office.Interop.PowerPoint.dll version 12.0 from VS 2005 or greater.

Concepts Explained:

I am only dealing with concepts pertaining to PowerPoint Object Model Reference which has been used in the program. However for full reference click here
1. ApplicationClass: The Microsoft.Office.Interop.PowerPoint.ApplicationClass is used to open office applications with instance of this class.
2. Presentations Object: Represents a collection of presentation(s) that is(are) currently open in PowerPoint.
3. Presentation Object: Is a member of Presentations collection. For more details about methods of Presentation Object you can refer to link.

Code Snippet:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Core;
using Powerpoint = Microsoft.Office.Interop.PowerPoint;
namespace Convert_pptTo_imageHtml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnBrowsePpt_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtPptLoc.Text = openFileDialog1.FileName;
}
}

private void btnBrowseSaveLocation_Click(object sender, EventArgs e)
{
if (rbImage.Checked)
{
//Setting properties of saveFileDialog1
this.saveFileDialog1.DefaultExt = "(*.jpeg)";
this.saveFileDialog1.FileName = "ImageFile";
this.saveFileDialog1.Filter = "Types allowed (*.jpeg)|*.jpeg|All Files|*.jpg";

//Calling function to save accordingly
this.SavePpt();
}
else if (rbHtml.Checked)
{
//Setting properties of saveFileDialog1
this.saveFileDialog1.DefaultExt = "(*.html)";
this.saveFileDialog1.FileName = "HtmlFile";
this.saveFileDialog1.Filter = "Types allowed (*.html)|*.html|All Files|*.html";

//Calling function to save accordingly
this.SavePpt();
}
else if (rbPdf.Checked)
{
//Setting properties of saveFileDialog1
this.saveFileDialog1.DefaultExt = "(*.pdf)";
this.saveFileDialog1.FileName = "PdfFile";
this.saveFileDialog1.Filter = "Types allowed (*.pdf)|*.pdf";

//Calling function to save accordingly
this.SavePpt();
}
else
{
MessageBox.Show("Oops! You have not selected the option for saving.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

public void SavePpt()
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
txtSaveLoc.Text = saveFileDialog1.FileName;
}

try
{
//Declaration of variables
string savePath = txtSaveLoc.Text;
string pptPath = txtPptLoc.Text;
int iIndex = savePath.LastIndexOf(".");

//MsoTriState represents to show links or icons for opened application
Powerpoint.ApplicationClass pptApplication = new Powerpoint.ApplicationClass();
Powerpoint.Presentation pptPresentation = pptApplication.Presentations.Open(pptPath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);

#region Saving As Image
if (txtPptLoc.Text != string.Empty && rbImage.Checked)
{
for (int i = 1; i {
string strNewPath = savePath.Remove(iIndex);
savePath = strNewPath + i.ToString() + ".jpeg";

pptPresentation.Slides[i].Export(savePath, "jpg", 1024, 800);
}

lblStatus.Text = "STATUS: Image saved successfully!";
}
#endregion

#region Saving As HTML
if (txtPptLoc.Text != String.Empty && rbHtml.Checked)
{
pptPresentation.SaveAs(txtSaveLoc.Text, Powerpoint.PpSaveAsFileType.ppSaveAsHTML, MsoTriState.msoFalse);

lblStatus.Text = "STATUS: Html file saved successfully!";
}
#endregion

#region Saving As PDF
if (txtPptLoc.Text != String.Empty && rbPdf.Checked)
{
pptPresentation.ExportAsFixedFormat(txtSaveLoc.Text,
Powerpoint.PpFixedFormatType.ppFixedFormatTypePDF,
Powerpoint.PpFixedFormatIntent.ppFixedFormatIntentPrint,
MsoTriState.msoFalse,
Powerpoint.PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst,
Powerpoint.PpPrintOutputType.ppPrintOutputSlides,
MsoTriState.msoFalse,
null,
Powerpoint.PpPrintRangeType.ppPrintAll,
"",
false,
false,
false,
true,
true,
System.Reflection.Missing.Value);

lblStatus.Text = "STATUS: Pdf file saved successfully!";

}
#endregion

//Closing the opened presentation file
pptPresentation.Close();

//Claiming unused memory
GC.Collect();
}
catch (Exception ex)
{
lblStatus.Text = String.Empty;

MessageBox.Show("APPLICATION REPORTED ERROR. CONSULT VENDOR"
+ " FOR HELP!\n\nTechnical Report: " + ex.ToString(), "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}

Download Source Code

Tags: ,

Category: .NET

Comments (3)

Trackback URL | Comments RSS Feed

  1. I really like your blog.. very nice colors & theme. Did you make this website yourself or did
    you hire someone to do it for you? Plz respond as I’m looking to design my
    own blog and would like to find out where u got this from.
    thanks

  2. Leticia says:

    Howdy just wanted to give you a quick heads up. The text
    in your post seem to be running off the screen in
    Opera. I’m not sure if this is a formatting issue or something to do with web browser compatibility but I
    thought I’d post to let you know. The style and design look great though!

    Hope you get the problem solved soon. Kudos

  3. This is a good,common sense article.Very helpful to one who is just finding the resouces about this part.It will certainly help educate me.

Leave a Reply

%d bloggers like this: