How to convert PPTX to PDF in ASP.NET Core?
The ASP.NET Core PowerPoint library do not have direct support to convert PPTX to PDF. But, this can be achieved by converting the PowerPoint file to PDF document using a web service in ASP.NET Core application. The following code example explains how to create and use the web service in ASP.NET Core platform.
Web service code
/// <summary> /// Converts the PowerPoint presentation (PPTX) to PDF document /// </summary> [AcceptVerbs("Post")] public HttpResponseMessage ConvertToPdf() { HttpResponseMessage httpResponseMessage; using (Stream stream = Request.Content.ReadAsStreamAsync().Result) { try { //Opens the PowerPoint presentation (PPTX) from stream using (IPresentation presentation = Presentation.Open(stream)) { //Initializes the ChartToImageConverter for converting charts during PPTX to PDF conversion presentation.ChartToImageConverter = new ChartToImageConverter { ScalingMode = Syncfusion.OfficeChart.ScalingMode.Best }; //Creates an instance of the PresentationToPdfConverterSettings PresentationToPdfConverterSettings settings = new PresentationToPdfConverterSettings { ShowHiddenSlides = true }; //Converts PowerPoint presentation (PPTX) into PDF document PdfDocument pdfDocument = PresentationToPdfConverter.Convert(presentation, settings); //Adds watermark at top left corner of first page in the generated PDF document to denote that it is generated using demo web service //To remove this watermark, comment or delete the codes within below "if" statement if (pdfDocument.Pages.Count > 0) { PdfPage pdfPage = pdfDocument.Pages[0]; //Create PDF font and PDF font style using Font Font font = new Font("Times New Roman", 12f, FontStyle.Regular); PdfFont pdfFont = new PdfTrueTypeFont(font, false); //Create a new PDF brush to draw the rectangle PdfBrush pdfBrush = new PdfSolidBrush(Color.White); //Draw rectangle in the current page pdfPage.Graphics.DrawRectangle(pdfBrush, 0f, 0f, 228f, 20.65f); //Create a new brush to draw the text pdfBrush = new PdfSolidBrush(Color.Red); //Draw text in the current page pdfPage.Graphics.DrawString("Created by Syncfusion – Presentation library", pdfFont, pdfBrush, 6f, 4f); } //Saves the PDF document to response stream MemoryStream memoryStream = new MemoryStream(); pdfDocument.Save(memoryStream); memoryStream.Position = 0; httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(memoryStream) }; pdfDocument.Dispose(); } } catch { httpResponseMessage = new HttpResponseMessage(HttpStatusCode.ExpectationFailed); } } return httpResponseMessage; }
The following code snippet explains how to use the previously given web service to the ASP.NET Core applications to convert the PowerPoint file as a PDF document in ASP.NET Core application.
public async System.Threading.Tasks.Task<ActionResult> Index(string button) { if (button == null) return View(); //Gets file path for template document string rootPath = _hostingEnvironment.WebRootPath; FileStream fileStream = new FileStream(rootPath + "/PPTX/TemplateFile.pptx", FileMode.Open); //Open the input presentation file IPresentation presentation = Presentation.Open(fileStream); fileStream.Dispose(); //Saves a presentation into stream and close the presentation instance MemoryStream inputStream = new MemoryStream(); presentation.Save(inputStream); inputStream.Position = 0; presentation.Close(); #region Service approach to send presentation document as stream and gets resultant converted PDF from it. HttpClient client = new HttpClient(); //Gets Uri string requestUri = "http://js.syncfusion.com/demos/ioservices/api/powerpoint/converttopdf"; //Posts input presentation document to service and gets resultant PDF as content of HttpResponseMessage HttpResponseMessage response = null; try { response = await client.PostAsync(requestUri, new StreamContent(inputStream)); //Dispose the input stream and client instances inputStream.Dispose(); client.Dispose(); } catch (Exception ex) { //Gets exception here } MemoryStream outputStream = null; //Gets PDF from content stream if service got success if (response.IsSuccessStatusCode) { var responseHeaders = response.Headers; outputStream = new MemoryStream(await response.Content.ReadAsByteArrayAsync()); outputStream.Position = 0; //Dispose the response instance response.Dispose(); } #endregion return File(outputStream, "application/pdf", "Sample.pdf"); }
In this sample, the following things have been done:
- In ASP.NET Core sample, load the existing presentation document using Essential Presentation and send that document as stream to Web API (service) application.
- In ASP.NET Web API (service), the stream (input document) is converted into PDF in the service side and returned the response with resultant PDF.
- In ASP.NET Core side, received the PDF stream and saved the converted PDF file.
In provided ASP.NET Core sample, Syncfusion demo hosted web service (instead of local web service) have been used. By using this service, you can convert the Presentation to PDF document with “Essential Presentation” watermark. To avoid this watermark in the converted PDF document, run the below provided web service locally and replace the Syncfusion demo URL with your local host URL in the provided ASP.NET Core sample.
string requestUri = "http://js.syncfusion.com/demos/ioservices/api/powerpoint/converttopdf";
ASP.NET Core PDF conversion sample:
ASP.NET Web API (service) application:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebServices2123125391726663756
Steps to follow:
- Run the ASP.NET Web API service application (It is not required if you use Syncfusion demo hosted web service).
- Run the required ASP.NET Core application, which refers the service link on it.
Take a moment to peruse the documentation, where you will find more details with code examples. Refer here to explore the rich set of Syncfusion Essential PowerPoint features.
Conclusion
I hope you enjoyed learning about how to convert PPTX to PDF in ASP.NET Core.
You can refer to our .NET Core'sfeature 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 PDF example to understand how to create and manipulate data in the .NET Core.
For current customers, you can check out our Document processing libraries from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our .NET Core 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!