SelectPdf for .NET 10 - Convert PowerPoint to Pdf - C# / .NET 10 Sample

This sample shows how to use the SelectPdf PowerPoint to pdf converter to convert a PowerPoint presentation (pptx, pptm, potx, potm) to pdf - its slides, its notes pages or handouts - setting a few properties. The conversion runs in-process; no engine is launched.

The sample converts the following PowerPoint presentation, or a presentation you upload:
Test PowerPoint presentation

PowerPoint presentation (optional, pptx):

Page Setup:

(Document makes every page the size of a slide; Options scales the slides onto the page size, orientation and margins below.)

Pdf Page Size:


Pdf Page Orientation:


Margins:
pt

Add page numbers in the footer

Publish:


Slides per handout page:

(Notes pages and handouts are not produced as an accessible document; choose Slides with PDF/UA.)

Convert the hidden slides too

Pdf Standard:


Accessibility (PDF/UA):


Document Title (required by PDF/UA):


Document Language:


Sample code · C#
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class ConvertPowerPointToPdfModel : PageModel
    {
        public void OnGet()
        {
            DdlPageSetup = "Document";
            DdlPageSize = "A4";
            DdlPageOrientation = "Landscape";
            DdlPublish = "Slides";
            DdlSlidesPerHandout = "6";
            DdlPdfStandard = "Full";
            DdlAccessibility = "None";
            TxtTitle = "SelectPdf - PowerPoint presentation";
            TxtLanguage = "en-US";
            TxtMargins = "0";
        }

        // An uploaded presentation, or nothing - the shipped files/presentation.pptx is converted then.
        [BindProperty]
        public IFormFile FilePowerPoint { get; set; }

        [BindProperty]
        public string DdlPageSetup { get; set; }

        public List<SelectListItem> PageSetups { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Document", Text = "Document - pages the size of the slides" },
            new SelectListItem { Value = "Options", Text = "Options - scale onto the page set here" },
        };

        [BindProperty]
        public string DdlPageSize { get; set; }

        public List<SelectListItem> PageSizes { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "A3", Text = "A3" },
            new SelectListItem { Value = "A4", Text = "A4" },
            new SelectListItem { Value = "A5", Text = "A5" },
            new SelectListItem { Value = "Letter", Text = "Letter" },
            new SelectListItem { Value = "Legal", Text = "Legal" },
        };

        [BindProperty]
        public string DdlPageOrientation { get; set; }

        public List<SelectListItem> PageOrientations { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Portrait", Text = "Portrait" },
            new SelectListItem { Value = "Landscape", Text = "Landscape" },
        };

        [BindProperty]
        public string TxtMargins { get; set; }

        [BindProperty]
        public string DdlPublish { get; set; }

        public List<SelectListItem> PublishOptions { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Slides", Text = "Slides - one page per slide" },
            new SelectListItem { Value = "NotesPages", Text = "Notes pages - each slide with its notes" },
            new SelectListItem { Value = "Handouts", Text = "Handouts - several slides per page" },
        };

        [BindProperty]
        public string DdlSlidesPerHandout { get; set; }

        public List<SelectListItem> SlidesPerHandoutOptions { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "1", Text = "1" },
            new SelectListItem { Value = "2", Text = "2" },
            new SelectListItem { Value = "3", Text = "3" },
            new SelectListItem { Value = "4", Text = "4" },
            new SelectListItem { Value = "6", Text = "6" },
            new SelectListItem { Value = "9", Text = "9" },
        };

        [BindProperty]
        public bool ChkShowHiddenSlides { get; set; }

        [BindProperty]
        public string DdlPdfStandard { get; set; }

        public List<SelectListItem> PdfStandards { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Full", Text = "Full (no standard)" },
            new SelectListItem { Value = "PdfA", Text = "PDF/A-1b" },
            new SelectListItem { Value = "PdfA2B", Text = "PDF/A-2b" },
            new SelectListItem { Value = "PdfA3B", Text = "PDF/A-3b" },
            new SelectListItem { Value = "PdfA4", Text = "PDF/A-4" },
        };

        [BindProperty]
        public string DdlAccessibility { get; set; }

        public List<SelectListItem> Accessibility { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "None", Text = "None" },
            new SelectListItem { Value = "UA1", Text = "PDF/UA-1" },
            new SelectListItem { Value = "UA2", Text = "PDF/UA-2 (PDF 2.0)" },
        };

        [BindProperty]
        public string TxtTitle { get; set; }

        [BindProperty]
        public string TxtLanguage { get; set; }

        [BindProperty]
        public bool ChkPageNumbers { get; set; }

        public IActionResult OnPost()
        {
            // read parameters from the webpage
            PowerPointPageSetup pageSetup = SampleForm.ParseEnum(DdlPageSetup, PowerPointPageSetup.Document);
            PdfPageSize pageSize = SampleForm.ParseEnum(DdlPageSize, PdfPageSize.A4);
            PdfPageOrientation pdfOrientation = SampleForm.ParseEnum(DdlPageOrientation, PdfPageOrientation.Landscape);
            PowerPointPublish publish = SampleForm.ParseEnum(DdlPublish, PowerPointPublish.Slides);
            PdfStandard pdfStandard = SampleForm.ParseEnum(DdlPdfStandard, PdfStandard.Full);
            PdfUAStandard accessibility = SampleForm.ParseEnum(DdlAccessibility, PdfUAStandard.None);

            int margins = 0;
            try
            {
                margins = Convert.ToInt32(TxtMargins);
            }
            catch { }

            int slidesPerHandout = 6;
            try
            {
                slidesPerHandout = Convert.ToInt32(DdlSlidesPerHandout);
            }
            catch { }

            // instantiate a powerpoint to pdf converter object
            PowerPointToPdf converter = new PowerPointToPdf();

            // set converter options: the page setup policy decides whether the slides are scaled
            // onto the page size, orientation and margins below (Options) or every page is the
            // size of a slide (Document) - the margins and the bands apply either way
            converter.Options.PageSetup = pageSetup;
            converter.Options.PdfPageSize = pageSize;
            converter.Options.PdfPageOrientation = pdfOrientation;
            converter.Options.MarginLeft = margins;
            converter.Options.MarginRight = margins;
            converter.Options.MarginTop = margins;
            converter.Options.MarginBottom = margins;

            // what is published: the slides, the notes pages or handouts with several slides
            // on each page
            converter.Options.Publish = publish;
            converter.Options.SlidesPerHandout = slidesPerHandout;
            converter.Options.ShowHiddenSlides = ChkShowHiddenSlides;
            converter.Options.PdfStandard = pdfStandard;

            // accessibility: the slides' titles, text, tables, pictures and links become the
            // logical structure of the pdf; a declared standard needs a title and a language
            // (PDF/UA-2 is written as PDF 2.0 and combines with PDF/A-4 only)
            converter.Options.AccessibilityStandard = accessibility;
            converter.Options.Title = TxtTitle;
            converter.Options.Language = TxtLanguage;

            // a page-number footer; a PdfPowerPointSection could carry a presentation as the band
            // instead. A width of -1 spans the whole band, so the centered text stays centered.
            if (ChkPageNumbers)
            {
                converter.Options.DisplayFooter = true;
                converter.Footer.Height = 30;
                PdfTextSection pageNumbers = new PdfTextSection(0, 8, -1, "Page {page_number} of {total_pages}",
                    PdfFont.CreateStandardFont(PdfStandardFont.Helvetica, 9), new PdfColor(80, 80, 80));
                pageNumbers.HorizontalAlign = PdfTextHorizontalAlign.Center;
                converter.Footer.Add(pageNumbers);
            }

            // convert the uploaded presentation, or the sample one shipped with the application
            PdfDocument doc;
            if (FilePowerPoint != null && FilePowerPoint.Length > 0)
            {
                using (MemoryStream uploaded = new MemoryStream())
                {
                    FilePowerPoint.CopyTo(uploaded);
                    uploaded.Position = 0;
                    doc = converter.ConvertStream(uploaded);
                }
            }
            else
            {
                string powerPointFile = Path.Combine(Startup.ServerPath, "wwwroot", "files", "presentation.pptx");
                doc = converter.ConvertFile(powerPointFile);
            }

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }
    }
}