Microsoft® PowerPoint Presentation Conversion via C#

C# Source Codes for different conversion cases to convert files to images, PDF, HTML and other formats.

 

It is easy for the developers to convert Microsoft® PowerPoint Presentations with speed and accuracy. Get the results within no time for automating the business processes. We are discussing here few cases to read or load any input supported PowerPoint formats and write or save to any supported output format.

Inter Conversion Of Microsoft PowerPoint Files

Whenever there is need to automate the inter conversion of Microsoft® PowerPoint formats. C# PowerPoint library provides classes to achieve this target. Load the file using Presentation class to load or read the desired format and calling the Save method of same class by specifying the output file and SaveFormat .OutputFormat.

C# Converter Code for Microsoft PowerPoint Presentations
// Load the Source File
var pptToPptx = new Presentation("sourceFile.ppt");
// Save into the desired format
pptToPptx.Save("powerpoiont-inter-conversion.pptx", SaveFormat.Pptx);   
 

C# PowerPoint to PDF Conversion

For converting PowerPoint slides to PDF accurately, Programmers can load the document using Presentation class and use PdfOptions class for all specific and custom options like text compression level, Jpeg quality, the behavior of metafiles, converting hidden slides as well as selecting specific slides and more. Even there is option to protect the converted PDF file with password.

C# PowerPoint to PDF Converter Code
// Load PowerPoint file
Presentation pptxtopdf = new Presentation("sourceFile.pptx");

// Create PdfOptions class object for specific settings
PdfOptions pptPDFOptions = new PdfOptions();

// Set Jpeg quality
pptPDFOptions.JpegQuality = 90;

// Set behavior for metafiles
pptPDFOptions.SaveMetafilesAsPng = true;

// Set text compression level
pptPDFOptions.TextCompression = PdfTextCompression.Flate;

// Define the PDF 15 standard
pptPDFOptions.Compliance = PdfCompliance.Pdf15;

// Include hidden slides
pptPDFOptions.ShowHiddenSlides = true;

// Setting PDF password and access permissions
pptPDFOptions.Password = "password";
pptPDFOptions.AccessPermissions = PdfAccessPermissions.PrintDocument | PdfAccessPermissions.HighQualityPrint;

// Save the presentation as PDF
pptxtopdf.Save("csharp-PowerPoint-to.pdf", SaveFormat.Pdf, pptPDFOptions);
 

Mircrosoft PowerPoint to HTML Conversion

When ever there is need to embed presentations within webpages, then there is need to convert slides to HTML. API provides HtmlOptions class , Use it after loading the files for special settings like hidden slides, as by default, these will not be included during conversion process. Pass the finalized options to Save method for conversion.

C# Code for PowerPoint to HTML Conversion

// Load source presentation 
Presentation powerpoiontohtml = new Presentation("sourceFile.pptx");

// Create HTML options
HtmlOptions PowerPointhtmlOpt = new HtmlOptions();

// Show hidden slides
PowerPointhtmlOpt.ShowHiddenSlides = true;

// Save the PPTX as HTML
powerpoiontohtml.Save("presentation-to.html", SaveFormat.Html, PowerPointhtmlOpt); 
 

Convert PowerPoint Slides to Image Formats

Converting Microsoft® PowerPoint formats to images JPEG, PNG, TIFF etc is another commom use case mostly used for creating slides thumbnails. Coding process is simple. After loading the document, Use ISlide interface to iterate through each slide. During each iteration, use (Bitmap Object)[https://docs.microsoft.com/en-us/dotnet/api/system.drawing.bitmap?view=netframework-4.8] along with its GetThumbnail mehtod having customized image dimensions. Finally save the image in the required format.

C# PowerPoint to Image Converter Code
using (Presentation powerpointtoimage = new Presentation("source-file.ppt")){
foreach (ISlide sld in powerpointtoimage.Slides){

// Create a full scale image
Bitmap bmp = sld.GetThumbnail(1f, 1f);
// or use some customized dimensions as sld.GetThumbnail(x, y)

// Save the image
bmp.Save(string.Format("Slide_{0}.jpg", sld.SlideNumber), System.Drawing.Imaging.ImageFormat.Jpeg);
}
}