Simple Steps to View a PowerPoint Presentation in an ASP.NET Core Application

Jollen Moyani - May 17 '23 - - Dev Community

In this blog, we will walk through how to create simple PowerPoint viewer functionality in an ASP.NET Core application using Syncfusion components. The interface will have the following basic options:

  • Browse and open PowerPoint presentation (PPTX) files.
  • Thumbnail view.
  • Print the entire presentation or a specific slide from the browser.
  • Download the PowerPoint presentation as a PDF.

PowerPoint Viewer in ASP.NET Core

PowerPoint Viewer in ASP.NET Core

By integrating PowerPoint viewing capability into your ASP.NET Core application, users can view PowerPoint presentation files without leaving the application or needing any external application. It results in improved security for your PowerPoint files.

We will use Syncfusion’s .NET PowerPoint Library and PDF Viewer control to achieve this functionality. We need to convert a PowerPoint file into a PDF with the former, and then view the resultant PDF using the PDF Viewer:

  • .NET PowerPoint Library: Used to create, read, edit, and convert PPTX files without using Microsoft Office or PowerPoint.
  • PDF Viewer: Used to view, print, and annotate PDF files.

Design PowerPoint viewer in ASP.NET Core application

Step 1: First, clone the getting started with the PDF Viewer application from the GitHub repository.

Step 2: As we are designing a PowerPoint viewer using the Syncfusion .NET PowerPoint Library and ASP.NET Core PDF Viewer, we must install these NuGet packages in the application:

Note: If you plan to deploy your application to Linux or macOS, install Syncfusion.EJ2.PdfViewer.AspNet.Core.Linux or Syncfusion.EJ2.PdfViewer.AspNet.Core.OSX, respectively, instead of Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows.

Step 3: Open Index.cshtml and set the accept value of the input type as .pptx to accept uploading the PowerPoint presentation files.

<input type=”file” id=”fileUpload” accept=”.pptx” style=”display:block;visibility:hidden;width:0;height:0;”>
Enter fullscreen mode Exit fullscreen mode

Step 4: Modify the onload function script in Index.cshtml like in the following code to load the PowerPoint presentation template as the default instead of the PDF template.

window.onload = function () {
    disableSpinner();
    var pdfViewer = document.getElementById('pdfviewer').ej2_instances[0];
    pdfViewer.serviceUrl = 'api/PowerPointViewer';
    document.getElementById('fileUpload').addEventListener('change', readFile, false);
}
Enter fullscreen mode Exit fullscreen mode

Step 5: In the Controller class, include the following namespaces.

using Syncfusion.Pdf;

using Syncfusion.Presentation;

using Syncfusion.PresentationRenderer;
Enter fullscreen mode Exit fullscreen mode

Step 6: Then, replace the existing code with the following Load() method in the Controller class to convert the PowerPoint presentation to PDF using the .NET PowerPoint Library and send the converted PDF to the PDF Viewer.

public IActionResult Load([FromBody] Dictionary<string, string> jsonObject)
{
     PdfRenderer pdfviewer = new PdfRenderer(_cache);
     MemoryStream stream = new MemoryStream();

     object jsonResult = new object();

     if (jsonObject != null && jsonObject.ContainsKey("document"))
     {
        if (bool.Parse(jsonObject["isFileName"]))
        {
           string documentPath = GetDocumentPath(jsonObject["document"]);
           if (!string.IsNullOrEmpty(documentPath))

           {
              byte[] bytes = System.IO.File.ReadAllBytes(documentPath);
              stream = new MemoryStream(bytes);
           }
           else
           {
              return this.Content(jsonObject["document"] + " is not found");                 
           }
        }
        else
        {
            byte[] bytes = Convert.FromBase64String(jsonObject["document"]);
            stream = new MemoryStream(bytes);
        }
    }

    try
    {
        //Open the PowerPoint Presentation using the Syncfusion Presentation library
        using (IPresentation presentation = Presentation.Open(stream))
        {
            stream.Dispose();
            //Convert a PowerPoint Presentation as PDF to view the generated PDF file in our Syncfusion PdfViewer
           using (PdfDocument pdfDocument = PresentationToPdfConverter.Convert(presentation))
           {
               //Save the converted Pdf document to get a physical DOM of PDF document.
               stream = new MemoryStream();
               pdfDocument.Save(stream);
               //Reset the pdf stream position.
               stream.Position = 0;
            }
          }
     }
     catch
     {
     }
     jsonResult = pdfviewer.Load(stream, jsonObject);
     return Content(JsonConvert.SerializeObject(jsonResult));
}
Enter fullscreen mode Exit fullscreen mode

Step 7: Now, the application is ready. Launch it and upload the PowerPoint presentation file to display the slides in the PowerPoint viewer.

GitHub reference

You can find the example of this code in the GitHub repository.

Convert PowerPoint presentations into images

To make the PowerPoint presentation accessible on virtually any platform (Windows, Linux, macOS, mobile, Docker, cloud), you might occasionally want to share or show each slide as an image. Furthermore, it is a handy format to guard against content alteration. Our Syncfusion .NET Core PowerPoint Library provides the APIs to convert an entire PowerPoint presentation or just specific slides to images with just a few lines of code in C# without Microsoft PowerPoint.

Conclusion

Thank you for reading this blog. I hope this information helps you create a PowerPoint viewer using the Syncfusion .NET PowerPoint Library. Take a moment to peruse its documentation, where you’ll find other options and features, all with accompanying code examples.

Our Syncfusion .NET PowerPoint Library has the following significant functionalities:

To learn about all the features supported, please check our product page. If you are new to our PowerPoint Library, it is highly recommended that you follow our Getting Started guide.

Are you already a Syncfusion user? You can download the product setup here. If you’re not a Syncfusion user, you can download a free 30-day trial here.

If you have questions, contact us through our support forum, support portal, or feedback portal. We are happy to assist you!

Related blogs

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .