Convert PowerPoint (PPT/PPTX) to PDF with C#
Source: Dev.to
Converting PowerPoint (PPT/PPTX) to PDF in C# with Spire.Presentation
In daily development and office workflows, converting PowerPoint files to PDF is a high‑priority requirement. PDF’s cross‑platform compatibility, fixed layout, and ease of distribution make it the go‑to format for sharing and long‑term storage.
Spire.Presentation is a lightweight, high‑performance .NET component for PowerPoint processing. Its main advantage is zero dependency on Microsoft Office or PowerPoint installations, allowing you to read, edit, and convert PPT files on both server and desktop environments.
1. Install the NuGet Package
Via Visual Studio UI
- Open Visual Studio and create a C# project (Console App, Windows Forms, etc.).
- Right‑click the project in Solution Explorer → Manage NuGet Packages.
- In the Browse tab, search for
Spire.Presentationand install the latest stable version.
Via Package Manager Console
Install-Package Spire.Presentation
2. Simple PPT → PDF Conversion
The following example demonstrates a single‑file conversion using the SaveToFile method, which natively supports PDF output.
using System;
using Spire.Presentation;
namespace PptToPdfDemo
{
class Program
{
static void Main(string[] args)
{
try
{
// Input & output paths
string pptFilePath = @"D:\Demo\source.pptx";
string pdfFilePath = @"D:\Demo\output.pdf";
// Load the PowerPoint file
Presentation presentation = new Presentation();
presentation.LoadFromFile(pptFilePath);
// Convert to PDF (default settings)
presentation.SaveToFile(pdfFilePath, FileFormat.PDF);
// Release resources
presentation.Dispose();
Console.WriteLine("PPT converted to PDF successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Conversion failed: {ex.Message}");
}
}
}
}
3. Batch Conversion (Folder → Folder)
When you need to process many presentations, iterate through a directory and convert each file.
using System;
using System.IO;
using System.Linq;
using Spire.Presentation;
namespace BatchPptToPdf
{
class Program
{
static void Main(string[] args)
{
// Source & destination folders
string pptFolderPath = @"D:\Demo\PptFiles";
string pdfFolderPath = @"D:\Demo\PdfFiles";
// Ensure the output folder exists
Directory.CreateDirectory(pdfFolderPath);
// Get all .ppt and .pptx files (case‑insensitive)
string[] pptFiles = Directory.GetFiles(pptFolderPath, "*.*", SearchOption.TopDirectoryOnly)
.Where(f => f.EndsWith(".ppt", StringComparison.OrdinalIgnoreCase) ||
f.EndsWith(".pptx", StringComparison.OrdinalIgnoreCase))
.ToArray();
foreach (string pptFile in pptFiles)
{
try
{
string fileName = Path.GetFileNameWithoutExtension(pptFile);
string pdfFile = Path.Combine(pdfFolderPath, $"{fileName}.pdf");
// Automatic disposal with 'using'
using (Presentation presentation = new Presentation())
{
presentation.LoadFromFile(pptFile);
presentation.SaveToFile(pdfFile, FileFormat.PDF);
}
Console.WriteLine($"Converted: {pptFile} → {pdfFile}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to convert {pptFile}: {ex.Message}");
}
}
Console.WriteLine("Batch conversion completed!");
}
}
}
4. Encrypting the PDF During Conversion
Spire.Presentation also lets you protect the generated PDF with a password and set permissions.
using System;
using Spire.Presentation;
using Spire.Presentation.External.Pdf;
namespace ConvertToEncryptedPdf
{
class Program
{
static void Main(string[] args)
{
// Paths (replace with your own)
string inputPptPath = @"C:\Users\Administrator\Desktop\Input.pptx";
string outputPdfPath = @"C:\Users\Administrator\Desktop\EncryptedOutput.pdf";
try
{
using (Presentation presentation = new Presentation())
{
// Load the PowerPoint file
presentation.LoadFromFile(inputPptPath);
// Configure PDF encryption options
SaveToPdfOption pdfOption = new SaveToPdfOption
{
// Set a password required to open the PDF
EncryptionPassword = "YourStrongPassword",
// Optional: restrict printing, copying, etc.
// Permissions = PdfPermissions.Print | PdfPermissions.Copy,
// Set the encryption level (e.g., 128‑bit AES)
EncryptionLevel = PdfEncryptionLevel.AES128
};
// Save the encrypted PDF
presentation.SaveToFile(outputPdfPath, FileFormat.PDF, pdfOption);
}
Console.WriteLine("Encrypted PDF created successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Encryption failed: {ex.Message}");
}
}
}
}
Summary
- Spire.Presentation works without Microsoft Office installed.
- Install via NuGet (
Install-Package Spire.Presentation). - Use
Presentation.LoadFromFile+SaveToFile(..., FileFormat.PDF)for single‑file conversion. - Implement folder traversal for batch processing.
- Apply
SaveToPdfOptionto encrypt PDFs and control permissions.
These snippets are ready to copy, paste, and run in any .NET project. Happy coding!
PPT‑to‑PDF Conversion with Encryption (C#)
// Get PDF options from the presentation
var pdfOptions = presentation.SaveToPdfOption;
// -------------------------------------------------------------------
// Configure PDF encryption and permissions
// -------------------------------------------------------------------
// 1. User Password : Required to open the PDF file (abc‑123)
// 2. Owner Password : Required to modify PDF permissions (owner‑456, customizable)
// 3. Permissions : Allow printing and form filling
// 4. Encryption Level: 128‑bit AES (industry‑standard high security)
pdfOptions.PdfSecurity.Encrypt(
userPassword: "abc-123",
ownerPassword: "owner-456",
permissions: PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields,
keySize: PdfEncryptionKeySize.Key128Bit
);
// Save the encrypted PDF file
presentation.SaveToFile(outputPdfPath, FileFormat.PDF, pdfOptions);
Console.WriteLine("PPT converted to encrypted PDF successfully!");
Console.WriteLine($"Output Path: {outputPdfPath}");
// ---------------------------------------------------------------
// Main program (excerpt)
try
{
// ... (load presentation, set outputPdfPath, etc.)
// (code from above)
}
catch (Exception ex)
{
Console.WriteLine($"Conversion failed: {ex.Message}");
}
// Pause the console to view results (press Enter to exit)
Console.ReadLine();
Supported Input Formats
- PPT, PPTX, PPS, PPSX, etc.
Limitations
- Complex PowerPoint elements (e.g., 3D charts, custom animations, embedded videos) may not render perfectly in PDF because they rely on PowerPoint’s proprietary rendering engine.
Resource Release
- Always release
Presentationobjects usingDispose()or wrap them in ausingstatement.
This prevents memory leaks, especially in long‑running batch conversion tasks.
Permissions Checklist
- Ensure the application has read permission for the input folder.
- Ensure it has write permission for the output folder.
- Missing permissions will trigger an
UnauthorizedAccessException.
Solution Comparison
| Solution | Pros | Cons |
|---|---|---|
| LibreOffice SDK | Free and open‑source | Requires LibreOffice service deployment; complex API; poor stability in server environments |
| OpenXML SDK + iTextSharp | No external dependencies | Only supports PPTX (OpenXML) format; manual layout handling required; high development cost |
| GroupDocs.Conversion | Cloud‑native support; free quota available | Relies on network connectivity; unsuitable for offline scenarios; costly for large‑scale use |
| Spire.Presentation (this article) | Zero Office dependency; simple API; flexible output customization; ideal for server‑side batch processing, desktop apps, and enterprise document management | (Not listed – see article for details) |
Why Choose Spire.Presentation?
- Zero Office dependency – no need to install Microsoft Office on the server.
- Simple API – a few lines of code handle loading, converting, and encrypting.
- Flexible output – customize PDF security, layout, and rendering options.
- Production‑ready – reliable for batch processing, desktop utilities, and enterprise‑level document management systems.