Save PowerPoint Sections to PDF

Did you ever want to save all sections of a PowerPoint presentation to seperate PDF files? Yes, you can manually select the slides of a section and then use the option “Save Selection as PDF“. But what I wanted is to have some tool that helps me with a lot of presentations, so that all the sections of the selected presentations are stored to individual PDF files.

So I created a method in C#. You can use my example code in you own Visual Studio C# application at your own risk!

Within your Visual Studio project, add a all nescesarry references and add using statements to the namespaces. These are the minimal uses defined:

using System;
using System.IO;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
using System.Text.RegularExpressions;

See PowerPoint Object Model for more information.

public static void PresentationSectionsToPDF(string _inputPPTX)
{
    Presentation presentation = null;
    Microsoft.Office.Interop.PowerPoint.Application application = null;

    if (!File.Exists(_inputPPTX))
    {
        throw new FileNotFoundException("Please, specify a presentation file!");
    }
    try
    {
        // Target folder
        string targetFolder = Path.GetDirectoryName(_inputPPTX);
        // PowerPoint application
        application = new Microsoft.Office.Interop.PowerPoint.Application();
        application.Visible = MsoTriState.msoTrue;
        // Load the presentation
        presentation = application.Presentations.Open(_inputPPTX, MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoTrue);
        // Fetch all sections
        SectionProperties oSections = presentation.SectionProperties;
        if (oSections.Count > 0)
        {
            // Loop through sections
            for (int iSection = oSections.Count; iSection > 0; iSection--)
            {
                // When the section contains slides
                if (presentation.SectionProperties.SlidesCount(iSection) > 0)
                {
                    string sectionName = ValidFileName(presentation.SectionProperties.Name(iSection));
                    string targetFile = $"{targetFolder}\\{sectionName}.pdf";

                    // Create new target presentation
                    Presentation targetPPTX = application.Presentations.Add(MsoTriState.msoFalse);

                    // Copy current design
                    for (int designCount = 1; designCount < presentation.Designs.Count; designCount++)
                    {
                        targetPPTX.Designs.Clone(presentation.Designs[designCount]);
                    }

                    // Determine all slides in section
                    int firstSlide = presentation.SectionProperties.FirstSlide(iSection);
                    int lastSlide = presentation.SectionProperties.SlidesCount(iSection);

                    // Clone the slides from the section
                    targetPPTX.Slides.InsertFromFile(_inputPPTX, 0, firstSlide, (lastSlide + firstSlide - 1));

                    // Copy slide style
                    for (int slideCount = 1; slideCount <= lastSlide; slideCount++)
                    {
                        targetPPTX.Slides[slideCount].Design = presentation.Slides[firstSlide + slideCount - 1].Design;
                    }

                    // Target presentation settings
                    targetPPTX.HandoutMaster.HeadersFooters.Header.Text = "";
                    targetPPTX.HandoutMaster.HeadersFooters.Header.Visible = MsoTriState.msoFalse;
                    targetPPTX.HandoutMaster.HeadersFooters.DateAndTime.Text = "";
                    targetPPTX.HandoutMaster.HeadersFooters.DateAndTime.Visible = MsoTriState.msoFalse;
                    targetPPTX.SlideMaster.HeadersFooters.SlideNumber.Visible = MsoTriState.msoFalse;
                    targetPPTX.HandoutMaster.HeadersFooters.Footer.Visible = MsoTriState.msoTrue;
                    targetPPTX.HandoutMaster.HeadersFooters.Footer.Text = $"Copyright © {DateTime.Now.Year.ToString()} Byteway's BLog";

                    // Then export the selection as PDF file
                    targetPPTX.ExportAsFixedFormat(targetFile, PpFixedFormatType.ppFixedFormatTypePDF, PpFixedFormatIntent.ppFixedFormatIntentPrint, MsoTriState.msoFalse, PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, PpPrintOutputType.ppPrintOutputTwoSlideHandouts);
                    targetPPTX.Close();
                }
            }
        }
    }
    finally
    {
        if (presentation != null)
        {
            presentation.Close();
        }
        if (application != null)
        {
            application.Quit();
        }
    }
}
public static string ValidFileName(string _fileName)
{
    string ret = "";

    string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
    Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
    ret = r.Replace(_fileName, "");

    return ret;
}