|
| 1 | +--- |
| 2 | +title: Create a PowerPoint Presentation in ASP.NET Core Web API | Syncfusion |
| 3 | +description: Create PowerPoint document in ASP.NET Core Web API applications using .NET PowerPoint library without Microsoft PowerPoint or interop dependencies. |
| 4 | +platform: document-processing |
| 5 | +control: PowerPoint |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# Create a PowerPoint file using ASP.NET Core Web API |
| 10 | + |
| 11 | +Syncfusion<sup>®</sup> PowerPoint is a [.NET PowerPoint library](https://www.syncfusion.com/document-processing/powerpoint-framework/net) used to create, read, edit and convert PowerPoint documents programmatically without **Microsoft PowerPoint** or interop dependencies. Using this library, you can **create PowerPoint document in ASP.NET Core Web API**. |
| 12 | + |
| 13 | +## Steps to Create a PowerPoint file in ASP.NET Core Web API: |
| 14 | + |
| 15 | +The below steps illustrate creating a simple PowerPoint Presentation in ASP.NET Core Web API. |
| 16 | + |
| 17 | +Step 1: Create a new C# ASP.NET Core Web API project. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Step 2: Install the [Syncfusion.Presentation.Net.Core](https://www.nuget.org/packages/Syncfusion.Presentation.Net.Core) NuGet package as a reference to your project from [NuGet.org](https://www.nuget.org). |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +N> Starting with v16.2.0.x, if you reference Syncfusion<sup>®</sup> assemblies from trial setup or from the NuGet feed, you also have to add "Syncfusion.Licensing" assembly reference and include a license key in your projects. Please refer to this [link](https://help.syncfusion.com/common/essential-studio/licensing/overview) to know about registering Syncfusion<sup>®</sup> license key in your application to use our components. |
| 26 | + |
| 27 | +Step 3: Add a new API controller empty file in the project. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +Step 4: Include the following namespaces in the **ValuesController.cs** file. |
| 32 | + |
| 33 | +{% tabs %} |
| 34 | + |
| 35 | +{% highlight c# tabtitle="C#" %} |
| 36 | + |
| 37 | +using Microsoft.AspNetCore.Mvc; |
| 38 | +using Syncfusion.Presentation; |
| 39 | + |
| 40 | +{% endhighlight %} |
| 41 | + |
| 42 | +{% endtabs %} |
| 43 | + |
| 44 | +Step 5: Add a new action method CreatePresentation in **ValuesController.cs** and include the below code snippet to create an PowerPoint presentation and download it. |
| 45 | + |
| 46 | +{% tabs %} |
| 47 | + |
| 48 | +{% highlight c# tabtitle="C#" %} |
| 49 | + |
| 50 | + [HttpGet] |
| 51 | +[Route("api/PowerPoint")] |
| 52 | +public IActionResult CreatePresentation() |
| 53 | +{ |
| 54 | + try |
| 55 | + { |
| 56 | + var fileDownloadName = "Output.pptx"; |
| 57 | + const string contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; |
| 58 | + var stream = GeneratePresentation(); |
| 59 | + stream.Position = 0; |
| 60 | + return File(stream, contentType, fileDownloadName); |
| 61 | + } |
| 62 | + catch (Exception ex) |
| 63 | + { |
| 64 | + return BadRequest("Error occurred while creating PowerPoint file: " + ex.Message); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | + {% endhighlight %} |
| 69 | + |
| 70 | +{% endtabs %} |
| 71 | + |
| 72 | +Step 6: Implement the `GeneratePresentation` method in `ValuesController.cs`. |
| 73 | + |
| 74 | +{% tabs %} |
| 75 | + |
| 76 | +{% highlight c# tabtitle="C#" %} |
| 77 | + |
| 78 | + public static MemoryStream GeneratePresentation() |
| 79 | +{ |
| 80 | + // Create a new instance of PowerPoint Presentation file |
| 81 | + IPresentation pptxDoc = Presentation.Create(); |
| 82 | + // Add a new slide to file and apply background color |
| 83 | + ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.TitleOnly); |
| 84 | + // Specify the fill type and fill color for the slide background |
| 85 | + slide.Background.Fill.FillType = FillType.Solid; |
| 86 | + slide.Background.Fill.SolidFill.Color = ColorObject.FromArgb(232, 241, 229); |
| 87 | + // Add title content to the slide by accessing the title placeholder of the TitleOnly layout-slide |
| 88 | + IShape titleShape = slide.Shapes[0] as IShape; |
| 89 | + titleShape.TextBody.AddParagraph("Company History").HorizontalAlignment = HorizontalAlignmentType.Center; |
| 90 | + // Add description content to the slide by adding a new TextBox IShape |
| 91 | + IShape descriptionShape = slide.AddTextBox(53.22, 141.73, 874.19, 77.70); |
| 92 | + descriptionShape.TextBody.Text = "IMN Solutions PVT LTD is the software company, established in 1987, by George Milton. The company has been listed as the trusted partner for many high-profile organizations since 1988 and got awards for quality products from reputed organizations."; |
| 93 | + // Add bullet points to the slide |
| 94 | + IShape bulletPointsShape = slide.AddTextBox(53.22, 270, 437.90, 116.32); |
| 95 | + // Add a paragraph for a bullet point |
| 96 | + IParagraph firstPara = bulletPointsShape.TextBody.AddParagraph("The company acquired the MCY corporation for 20 billion dollars and became the top revenue maker for the year 2015."); |
| 97 | + // Format how the bullets should be displayed |
| 98 | + firstPara.ListFormat.Type = ListType.Bulleted; |
| 99 | + firstPara.LeftIndent = 35; |
| 100 | + firstPara.FirstLineIndent = -35; |
| 101 | + // Add another paragraph for the next bullet point |
| 102 | + IParagraph secondPara = bulletPointsShape.TextBody.AddParagraph("The company is participating in top open source projects in automation industry."); |
| 103 | + // Format how the bullets should be displayed |
| 104 | + secondPara.ListFormat.Type = ListType.Bulleted; |
| 105 | + secondPara.LeftIndent = 35; |
| 106 | + secondPara.FirstLineIndent = -35; |
| 107 | + // Add an auto-shape to the slide |
| 108 | + IShape stampShape = slide.Shapes.AddShape(AutoShapeType.Explosion1, 48.93, 430.71, 104.13, 80.54); |
| 109 | + // Format the auto-shape color by setting the fill type and text |
| 110 | + stampShape.Fill.FillType = FillType.None; |
| 111 | + stampShape.TextBody.AddParagraph("IMN").HorizontalAlignment = HorizontalAlignmentType.Center; |
| 112 | + // Save the PowerPoint Presentation as stream |
| 113 | + MemoryStream stream = new MemoryStream(); |
| 114 | + pptxDoc.Save(stream); |
| 115 | + pptxDoc.Close(); |
| 116 | + stream.Position = 0; |
| 117 | + return stream; |
| 118 | +} |
| 119 | + |
| 120 | +{% endhighlight %} |
| 121 | + |
| 122 | +{% endtabs %} |
| 123 | + |
| 124 | +Step 7: Build the project. |
| 125 | + |
| 126 | +Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project. |
| 127 | + |
| 128 | +Step 8: Run the project. |
| 129 | + |
| 130 | +Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app. |
| 131 | + |
| 132 | +A complete working sample is available on [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Getting-Started/ASP.NET-Core-Web-API/Create-Word-Document). |
| 133 | + |
| 134 | +## Steps for accessing the Web API using HTTP requests |
| 135 | + |
| 136 | +Step 1: Create a console application. |
| 137 | + |
| 138 | + |
| 139 | +N> Ensure your ASP.NET Core Web API is running on the specified port before running this client. Adjust the port number if your Web API runs on a different port (check the ASP.NET Core app's launch settings). |
| 140 | + |
| 141 | +Step 2: Add the below code snippet in the **Program.cs** file for accessing the Web API using HTTP requests. |
| 142 | + |
| 143 | +This method sends a GET request to the Web API endpoint to retrieve and save the generated Word document. |
| 144 | + |
| 145 | +{% tabs %} |
| 146 | + |
| 147 | +{% highlight c# tabtitle="C#" %} |
| 148 | + |
| 149 | + // Create an HttpClient instance |
| 150 | +using (HttpClient client = new HttpClient()) |
| 151 | +{ |
| 152 | + try |
| 153 | + { |
| 154 | + // Send a GET request to a URL |
| 155 | + HttpResponseMessage response = await client.GetAsync("https://localhost:7073/api/Values/api/PowerPoint"); |
| 156 | + |
| 157 | + // Check if the response is successful |
| 158 | + if (response.IsSuccessStatusCode) |
| 159 | + { |
| 160 | + // Read the content as a string |
| 161 | + Stream responseBody = await response.Content.ReadAsStreamAsync(); |
| 162 | + FileStream fileStream = File.Create("../../../Output/Output.pptx"); |
| 163 | + responseBody.CopyTo(fileStream); |
| 164 | + fileStream.Close(); |
| 165 | + } |
| 166 | + else |
| 167 | + { |
| 168 | + Console.WriteLine("HTTP error status code: " + response.StatusCode); |
| 169 | + } |
| 170 | + } |
| 171 | + catch (HttpRequestException e) |
| 172 | + { |
| 173 | + Console.WriteLine("Request exception: " + e.Message); |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +{% endhighlight %} |
| 178 | + |
| 179 | +{% endtabs %} |
| 180 | + |
| 181 | +Step 3: Build the project. |
| 182 | + |
| 183 | +Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project. |
| 184 | + |
| 185 | +Step 4: Run the project. |
| 186 | + |
| 187 | +Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app. |
| 188 | + |
| 189 | +A complete working sample is available on [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Getting-Started/ASP.NET-Core-Web-API/Client%20Application). |
| 190 | + |
| 191 | +Upon executing the program, the **PowerPoint Presentation** will be generated as follows. |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | +Click [here](https://www.syncfusion.com/document-processing/word-framework/net) to explore the rich set of Syncfusion<sup>®</sup> Word library (DocIO) features. |
| 196 | + |
| 197 | +An online sample link to [create a Word document](https://document.syncfusion.com/demos/word/helloworld#/tailwind) in ASP.NET Core. |
0 commit comments