|
| 1 | +--- |
| 2 | +title: Convert Word to PDF in ASP.NET Core Web API | Syncfusion |
| 3 | +description: Convert Word to PDF in ASP.NET Core Web API using .NET Core Word (DocIO) library without Microsoft Word or interop dependencies. |
| 4 | +platform: document-processing |
| 5 | +control: DocIO |
| 6 | +documentation: UG |
| 7 | +--- |
| 8 | + |
| 9 | +# Convert Word document to PDF in ASP.NET Core Web API |
| 10 | + |
| 11 | +Syncfusion<sup>®</sup> DocIO is a [.NET Core Word library](https://www.syncfusion.com/document-processing/word-framework/net-core/word-library) used to create, read, edit, and **convert Word documents** programmatically without **Microsoft Word** or interop dependencies. Using this library, you can **convert a Word document to PDF in ASP.NET Core Web API**. |
| 12 | + |
| 13 | +## Steps to convert Word document to PDF in programmatically: |
| 14 | + |
| 15 | +The below steps illustrate convert a simple Word document to PDF 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.DocIORenderer.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIORenderer.Net.Core) NuGet package as a reference to your project from [NuGet.org](https://www.nuget.org). |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +N> 1. If you're deploying the application in a Linux environment, refer to the [documentation](https://help.syncfusion.com/document-processing/word/conversions/word-to-image/net/nuget-packages-required-word-to-image#additional-nuget-packages-required-for-linux) for the required additional NuGet packages. |
| 26 | +N> 2. 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. |
| 27 | + |
| 28 | +Step 3: Add a new API controller empty file in the project. |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +Step 4: Include the following namespaces in the **ValuesController.cs** file. |
| 33 | + |
| 34 | +{% tabs %} |
| 35 | + |
| 36 | +{% highlight c# tabtitle="C#" %} |
| 37 | + |
| 38 | +using Microsoft.AspNetCore.Mvc; |
| 39 | +using Syncfusion.DocIO; |
| 40 | +using Syncfusion.DocIO.DLS; |
| 41 | +using Syncfusion.DocIORenderer; |
| 42 | +using Syncfusion.Pdf; |
| 43 | + |
| 44 | +{% endhighlight %} |
| 45 | + |
| 46 | +{% endtabs %} |
| 47 | + |
| 48 | +Step 5: Add a new action method ConvertWordToPdf in **ValuesController.cs** and include the below code snippet to Convert Word document to PDF and download it. |
| 49 | + |
| 50 | +{% tabs %} |
| 51 | + |
| 52 | +{% highlight c# tabtitle="C#" %} |
| 53 | + |
| 54 | +[HttpGet] |
| 55 | +[Route("api/ConvertWordToPdf")] |
| 56 | +public IActionResult ConvertWordToPdf() |
| 57 | +{ |
| 58 | + try |
| 59 | + { |
| 60 | + var fileDownloadName = "Output.pdf"; |
| 61 | + const string contentType = "application/pdf"; |
| 62 | + var stream = ConvertWordDocumentToPdf(); |
| 63 | + stream.Position = 0; |
| 64 | + return File(stream, contentType, fileDownloadName); |
| 65 | + } |
| 66 | + catch (Exception ex) |
| 67 | + { |
| 68 | + return BadRequest("Error occurred while converting Word to PDF: " + ex.Message); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +{% endhighlight %} |
| 73 | + |
| 74 | +{% endtabs %} |
| 75 | + |
| 76 | +Step 6: Implement the `ConvertWordDocumentToPdf` method in `ValuesController.cs`. |
| 77 | + |
| 78 | +{% tabs %} |
| 79 | + |
| 80 | +{% highlight c# tabtitle="C#" %} |
| 81 | + |
| 82 | +public static MemoryStream ConvertWordDocumentToPdf() |
| 83 | +{ |
| 84 | + //Open the existing PowerPoint presentation with loaded stream. |
| 85 | + using (WordDocument wordDocument = new WordDocument(Path.GetFullPath("Data/Template.docx"))) |
| 86 | + { |
| 87 | + using (DocIORenderer render = new DocIORenderer()) |
| 88 | + { |
| 89 | + PdfDocument pdfDocument = render.ConvertToPDF(wordDocument); |
| 90 | + //Create the MemoryStream to save the converted PDF. |
| 91 | + MemoryStream pdfStream = new MemoryStream(); |
| 92 | + //Save the converted PDF document to MemoryStream. |
| 93 | + pdfDocument.Save(pdfStream); |
| 94 | + pdfStream.Position = 0; |
| 95 | + //Download PDF document in the browser. |
| 96 | + return pdfStream; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +{% endhighlight %} |
| 102 | + |
| 103 | +{% endtabs %} |
| 104 | + |
| 105 | +Step 7: Build the project. |
| 106 | + |
| 107 | +Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project. |
| 108 | + |
| 109 | +Step 8: Run the project. |
| 110 | + |
| 111 | +Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app. |
| 112 | + |
| 113 | +A complete working sample is available on [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/ASP.NET-Core-Web-API/Convert-Word-Document-to-PDF). |
| 114 | + |
| 115 | +## Steps for accessing the Web API using HTTP requests |
| 116 | + |
| 117 | +Step 1: Create a console application. |
| 118 | + |
| 119 | + |
| 120 | +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). |
| 121 | + |
| 122 | +Step 2: Add the below code snippet in the **Program.cs** file for accessing the Web API using HTTP requests. |
| 123 | + |
| 124 | +This method sends a GET request to the Web API endpoint to retrieve and save the generated PDF document. |
| 125 | + |
| 126 | +{% tabs %} |
| 127 | + |
| 128 | +{% highlight c# tabtitle="C#" %} |
| 129 | + |
| 130 | +// Create an HttpClient instance |
| 131 | +using (HttpClient client = new HttpClient()) |
| 132 | +{ |
| 133 | + try |
| 134 | + { |
| 135 | + // Send a GET request to a URL |
| 136 | + HttpResponseMessage response = await client.GetAsync("https://localhost:7240/api/Values/api/ConvertWordToPdf"); |
| 137 | + |
| 138 | + // Check if the response is successful |
| 139 | + if (response.IsSuccessStatusCode) |
| 140 | + { |
| 141 | + // Read the content as a string |
| 142 | + Stream responseBody = await response.Content.ReadAsStreamAsync(); |
| 143 | + FileStream fileStream = File.Create("../../../Output/Output.pdf"); |
| 144 | + responseBody.CopyTo(fileStream); |
| 145 | + fileStream.Close(); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + Console.WriteLine("HTTP error status code: " + response.StatusCode); |
| 150 | + } |
| 151 | + } |
| 152 | + catch (HttpRequestException e) |
| 153 | + { |
| 154 | + Console.WriteLine("Request exception: " + e.Message); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +{% endhighlight %} |
| 159 | + |
| 160 | +{% endtabs %} |
| 161 | + |
| 162 | +Step 3: Build the project. |
| 163 | + |
| 164 | +Click on Build → Build Solution or press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>B</kbd> to build the project. |
| 165 | + |
| 166 | +Step 4: Run the project. |
| 167 | + |
| 168 | +Click the Start button (green arrow) or press <kbd>F5</kbd> to run the app. |
| 169 | + |
| 170 | +A complete working sample is available on [GitHub](https://github.com/SyncfusionExamples/DocIO-Examples/tree/main/Word-to-PDF-Conversion/Convert-Word-document-to-PDF/ASP.NET-Core-Web-API/Client-Application). |
| 171 | + |
| 172 | +Upon executing the program, the **PDF** will be generated as follows. |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | +Click [here](https://www.syncfusion.com/document-processing/word-framework/net) to explore the rich set of Syncfusion<sup>®</sup> Word library (DocIO) features. |
| 177 | + |
| 178 | +An online sample link to [convert Word document to PDF](https://document.syncfusion.com/demos/word/wordtopdf#/tailwind) in ASP.NET Core. |
0 commit comments