Articles in this section
Category / Section

How to convert multiple PowerPoint Presentations to multiple PDFs and zip the PDFs in C#

4 mins read

Syncfusion PowerPoint Presentation is a .NET Core PowerPoint library used to create, read, and edit PowerPoint presentations in any .NET application without any Microsoft Office dependency. Using this library, you can convert multiple PowerPoint files to PDF in C#.

You can zip the converted PDF files using Syncfusion Compression library which is one of the dependent packages of Presentation library.

Steps to convert multiple PowerPoint Presentations to PDFs and zip the PDFs in C#

  1. Create new C# .NET Core console application.

Project template in Visual Studio

  1. Install the Syncfusion.PresentationRenderer.Net.Core NuGet package as a reference to your .NET Core application from NuGet.org.

Note:

  • The Syncfusion.Compression library is a dependent package of PresentationRenderer NuGet package. Once PresentationRenderer gets installed, the Compression library will automatically be installed as its dependent package.

  • Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

Install PresentationRenderer NuGet package in ASP.NET Core

  1. Include the following namespace in the Program.cs file.
using Syncfusion.Presentation;
using Syncfusion.PresentationRenderer;
using Syncfusion.Pdf;
  1. Use the following code snippet to convert multiple PowerPoint Presentations to PDFs and zip the pdfs.
//Creating new zip archive.
Syncfusion.Compression.Zip.ZipArchive zipArchive = new Syncfusion.Compression.Zip.ZipArchive();
//You can use CompressionLevel to reduce the size of the file.
zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best;
//Get the input Presentation from the folder.
string folderName = @"../../../InputDocuments";
string[] inputFiles = Directory.GetFiles(folderName);
DirectoryInfo directoryInfo = new DirectoryInfo(folderName);
List<string> files = new List<string>();
FileInfo[] fileInfo = directoryInfo.GetFiles();
foreach (FileInfo fi in fileInfo)
{
   string name = Path.GetFileNameWithoutExtension(fi.Name);
   files.Add(name);
}
//Converts each PowerPoint Presentations to PDF documents.
for (int i = 0; i < inputFiles.Length; i++)
{
   //Output PDF file name.
   string outputFileName = files[i] + ".pdf";
   //Loads the PowerPoint presentation into stream.
   using (FileStream fileStreamInput = new FileStream(inputFiles[i], FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
   {
       //Convert Presentation to PDF.
       MemoryStream outputStream = ConvertPresentationToPDF(fileStreamInput);
       //Add the converted PDF file to zip.
       zipArchive.AddItem(outputFileName, outputStream, true, Syncfusion.Compression.FileAttributes.Normal);
   }
}
//Zip file name and location.
FileStream zipStream = new FileStream(@"../../../OutputPDFs.zip", FileMode.OpenOrCreate, FileAccess.ReadWrite);
zipArchive.Save(zipStream, true);
zipArchive.Close();

Helper method to convert PowerPoint Presentation to PDF.

/// <summary>
/// Convert PowerPoint Presentation to PDF.
/// </summary>
/// <param name="inputStream" =""="">Input Presentation stream
static MemoryStream ConvertPresentationToPDF(FileStream fileStreamInput)
{
   //Open the existing PowerPoint presentation with loaded stream.
   using (IPresentation pptxDoc = Presentation.Open(fileStreamInput))
   {
       //Create the MemoryStream to save the converted PDF.
       using (MemoryStream pdfStream = new MemoryStream())
       {
           //Convert the PowerPoint document to PDF document.
           using (PdfDocument pdfDocument = PresentationToPdfConverter.Convert(pptxDoc))
           {
               //Save the converted PDF document to MemoryStream.
               pdfDocument.Save(pdfStream);
               pdfStream.Position = 0;
           }
           //Create the output PDF file stream.
           MemoryStream fileStreamOutput = new MemoryStream();
           //Copy the converted PDF stream into created output PDF stream.
           pdfStream.CopyTo(fileStreamOutput);
           return fileStreamOutput;
       }
   }
} 

A complete working example of how to convert multiple PowerPoint Presentations to PDFs and zip the PDFs can be downloaded from GitHub.

By executing the example program, you will get the zip folder containing the output pdf as like below.

Output of multiple Presentation documents to PDFs and zip the PDFs in C#

Take a moment to peruse the documentation, where you can find other file format conversions with code examples.
Explore more about the rich set of Syncfusion PowerPoint Framework features.
An online example to convert Presentation document to PDF in ASP.NET Core web application.

See Also

How to zip files using the Syncfusion.Compression.Zip namespace?
How to zip all the files in subfolders using Syncfusion’s Compression?

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied