Convert PowerPoint (PPT/PPTX) to PDF in C#
Converting PowerPoint presentations to PDF format is a common task that enhances accessibility, sharing, and printing. PDF files preserve the layout, fonts, and images of the original PowerPoint presentation across all devices and operating systems, making them an ideal format for distributing information. In this guide, we’ll explore how to programmatically convert PowerPoint to PDF in C#.
- C# Convert a PowerPoint Presentation to PDF
- C# Convert PowerPoint to Password-Protected PDF
- C# Convert a Specified Slide to PDF
.NET Library for PowerPoint to PDF Conversion
To use C# to convert PowerPoint to PDF, we need to install the Spire.Presentation for .NET library. The library provides a simple and efficient API that supports a wide range of presentation-related tasks, including adding slides, customizing slide layouts, working with animations, adding multimedia, and converting presentations to other formats like PDF and images.
To install it, download its product package via this link and then manually import the dlls to your project. Or you can install it directly via Nuget.
PM> Install-Package Spire.Presentation
Convert a PowerPoint Presentation to PDF in C#
To convert PowerPoint to PDF, simply use the LoadFromFile() method to load a PPT or PPTX file, and then save it as a PDF file using the SaveToFile() method.
C# code:
using Spire.Presentation;
namespace ConvertPPTToPdf
{
class Program
{
static void Main(string[] args)
{
// Load a PowerPoint file
Presentation presentation = new Presentation();
presentation.LoadFromFile("Review.pptx");
//Save it to a PDF file
presentation.SaveToFile("ToPdf.pdf", FileFormat.PDF);
presentation.Dispose();
}
}
}
Convert PowerPoint to Password-Protected PDF in C#
The .NET PowerPoint library provides the SaveToPdfOption class for setting the PowerPoint to PDF conversion options. To password protected the generated PDF document, use its PdfSecurity.Encrypt() method to set the permission password and set document processing permissions.
C# code:
using Spire.Presentation;
using Spire.Presentation.External.Pdf;
namespace ConvertPPTToProtectedPdf
{
class Program
{
static void Main(string[] args)
{
// Load a PowerPoint file
Presentation presentation = new Presentation();
presentation.LoadFromFile("Review.pptx");
// Get the SaveToPdfOption object
SaveToPdfOption option = presentation.SaveToPdfOption;
// Encrypt the generated document with a password
option.PdfSecurity.Encrypt("123", PdfPermissionsFlags.CopyContent | PdfPermissionsFlags.Print);
// Save to a PDF file
presentation.SaveToFile("ToEncryptedPdf.pdf", FileFormat.PDF);
presentation.Dispose();
}
}
}
Convert a Specified Slide to PDF in C#
You can also get a specified slide through the Presentation.Slides[index] property, and then convert it to a PDF file using the ISlide.SaveToFile() method.
C# code:
using Spire.Presentation;
namespace ConvertSlideToPdf
{
class Program
{
static void Main(string[] args)
{
// Load a PowerPoint file
Presentation presentation = new Presentation();
presentation.LoadFromFile("Review.pptx");
// Get a specific slide
ISlide slide = presentation.Slides[1];
// Save the slide to a PDF file
slide.SaveToFile("SlideToPdf.pdf", FileFormat.PDF);
presentation.Dispose();
}
}
}
To remove the evaluation warning, you can request a trail license here:
The library also provides a free version, but with certain page limitations. Explore the Free Spire.Presentation for .NET library at: