How to create a PowerPoint document using Azure function in ASP.NET MVC?
You can create, edit, and convert the PowerPoint files without Microsoft Office dependencies in .NET Azure functions using the Syncfusion .NET PowerPoint library.
Steps to modify the template PowerPoint document in Azure functions using Syncfusion Presentation library:
- Create a new Azure function project.
- Select framework Azure Functions v1 (.NET Framework) and select HTTP trigger as follows.
- Install the Syncfusion.Presentation.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.
- Include the following namespaces in Function1.cs file.
// [C# Code] using System.IO; using Syncfusion.Presentation; using System.Net.Http.Headers;
- Add the following code snippet in Run method of Function1 class to modify the template PowerPoint document in Azure functions and return the modified document to client end.
// [C# Code] //Gets the input PowerPoint stream from client request Stream stream = req.Content.ReadAsStreamAsync().Result; //Open the existing PowerPoint file using (IPresentation pptxDoc = Presentation.Open(stream)) { //Gets the first slide from the PowerPoint Presentation ISlide slide = pptxDoc.Slides[0]; //Gets the first shape of the slide IShape shape = (IShape)slide.Shapes[0]; //Instance to hold paragraphs in textframe IParagraphs paragraphs = shape.TextBody.Paragraphs; //Adds paragraph to the textbody of shape IParagraph paragraph1 = paragraphs.Add(); //Adds a TextPart to the paragraph ITextPart textpart1 = paragraph1.AddTextPart(); //Adds text to the TextPart textpart1.Text = "Q&A SEGMENT"; //Sets the color of the text as white textpart1.Font.Color = ColorObject.White; //Sets text as bold textpart1.Font.Bold = true; //Sets the font name of the text as Calibri textpart1.Font.FontName = "Calibri (Body)"; //Sets the font size of the text textpart1.Font.FontSize = 25; //Adds paragraph to the textbody of shape paragraphs.Add(); //Add a paragraph into paragraphs collection AddParagraph(paragraphs, "Q&A segment will be at the end of the webinar."); //Add a paragraph into paragraphs collection AddParagraph(paragraphs, "Please enter your questions in the Questions window."); //Add a paragraph into paragraphs collection AddParagraph(paragraphs, "A recording of the webinar will be available within a week."); //Create memory stream to save the output PowerPoint file MemoryStream memoryStream = new MemoryStream(); //Save the PowerPoint into memory stream pptxDoc.Save(memoryStream); //Reset the memory stream position memoryStream.Position = 0; //Create the response to return HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); //Set the PowerPoint saved stream as content of response response.Content = new ByteArrayContent(memoryStream.ToArray()); //Set the contentDisposition as attachment response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Sample.pptx" }; //Set the content type as PPTX format mime type response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.presentationml.presentation"); //Return the response with output PowerPoint stream return response; }
- Add the following code snippet in Function1 class to add a paragraph within the PowerPoint shape
// [C# Code] private static void AddParagraph(IParagraphs paragraphs, string text) { //Adds paragraph to the textbody of shape IParagraph paragraph = paragraphs.Add(); //Sets the list type of the paragraph as bulleted paragraph.ListFormat.Type = ListType.Bulleted; //Sets the list level as 1 paragraph.IndentLevelNumber = 1; // Sets the hanging value paragraph.FirstLineIndent = -20; //Adds a TextPart to the paragraph ITextPart textpart = paragraph.AddTextPart(); //Adds text to the TextPart textpart.Text = text; //Sets the color of the text as white textpart.Font.Color = ColorObject.White; //Sets the font name of the text as Calibri textpart.Font.FontName = "Calibri (Body)"; //Sets the font size of the text textpart.Font.FontSize = 20; //Adds paragraph to the textbody of shape paragraphs.Add(); }
- Right-click the project and select Publish. Then, create a new profile in the Publish Window.
- Create an App service using Azure subscription and select a hosting plan.
The Syncfusion PowerPoint library works from basic hosting plan (B1). So, select the required hosting plan.
- After creating the profile, click the Publish button.
- Now, go to Azure portal and select the App Services. After running the service, click Get function URL by copying it. Then, paste it in the below client sample (which will request the Azure Function, to modify the PowerPoint document with input template document). You will get the modified PowerPoint document as follows.
A complete Azure function sample can be downloaded from Create PowerPoint file using Azure Function.zip.
Steps to post the request to Azure function with template PowerPoint document:
- Create a simple console application to request the Azure function API.
- Add the following code snippet into Main method to request the Azure function with template PowerPoint document and get the modified PowerPoint document.
// [C# Code] //Open the required template PowerPoint file Stream fileStream = File.Open(@"../../Data\Template.pptx", FileMode.Open); //Create memory stream to save the template PowerPoint MemoryStream inputStream = new MemoryStream(); //Copy the file stream into memory stream fileStream.CopyTo(inputStream); //Dispose the file stream fileStream.Dispose(); try { //Create HttpWebRequest with hosted azure function URL HttpWebRequest req = (HttpWebRequest)WebRequest.Create("Your Azure Function URL"); //Set request method as POST req.Method = "POST"; //Get the request stream to strore the PowerPoint stream Stream stream = req.GetRequestStream(); //Write the PowerPoint stream into request stream stream.Write(inputStream.ToArray(), 0, inputStream.ToArray().Length); //Gets the responce from the Azure Function request. HttpWebResponse res = (HttpWebResponse)req.GetResponse(); //Create file stream to save the output PowerPoint file FileStream outStream = File.Create("Sample.pptx"); //Copy the responce stream into file stream res.GetResponseStream().CopyTo(outStream); //Dispose the input stream inputStream.Dispose(); //Dispose the file stream outStream.Dispose(); } catch (Exception ex) { throw; }
A console application can be downloaded from ClientApp.zip.
Take a moment to peruse the documentation, where you will find other options like create and edit PowerPoint tables, create and edit PowerPoint charts, convert PowerPoint slides to images, and convert PowerPoint file to PDF with code examples.
See Also:
Create a PowerPoint file in Windows Forms
Create a PowerPoint file in WPF
Create a PowerPoint file in UWP
Create a PowerPoint file in Xamarin
Create a PowerPoint file in ASP.NET Core
Conclusion
I hope you enjoyed learning about how to create a PowerPoint document using Azure function in ASP.NET MVC.
You can refer to our ASP.NET MVC PowerPoint feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC PowerPoint example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!