Convert HTML to Word in C#

HTML to Word in Java
Convert HTML to Word in Java

HTML is the older and one of the reliable formats for sharing information over the internet as most browsers support it as the default rendering format. An HMTL document includes forms, text, images, animations, links, etc. However, the HTML elements might be referenced from external/third-party websites that are loaded at runtime (when the document is loaded in the browser), so for long-term archival, we are going to convert HTML to Word using C# .NET. We get an option to convert HTML to MS Word (DOCX, DOC, DOCM, DOT, DOTX, DOTM), RTF or OpenOffice (ODT, OTT) formats.

HTML to Word Conversion API

Aspose.Words Cloud is a REST API offering the capabilities to create, update, and render word files to other supported formats. Now in order to convert HTML to DOCX using .NET code snippet, we need to install Aspose.Words Cloud SDK for .NET which is available on NuGet and GitHub. Please execute the following command in the terminal window to perform the installation for NuGet:

nuget install Aspose.Words-Cloud

The next step is to create a free account by visiting Aspose.Cloud dashboard. Sign up using GitHub or Google account, or click on the Create a new Account button. Obtain your Client Credentials as they will be needed in subsequent sections.

Convert HTML to Word in C#

Please follow the instructions given below to HTML to Word conversion using C# .NET.

  • Firstly, create an object of WordsApi where we pass Client ID and Client secret as arguments
  • Secondly, create an object of SaveOptionsData where we define the output format as DOCX and the resultant file name
  • Thirdly, create an object of SaveAsRequest where we pass input HTML and SaveOptionsData objects as arguments
  • Finally, call the method SaveAs(…) which takes SaveAsRequest to convert HTML to Word

The resultant file generated as output can be found over myResultant.docx.

HTML to Word using cURL Commands

The cURL commands are the easiest and simplest form to perform operations using REST APIs. Since our APIs are only accessible to authorized users, so please execute the following command to generate the JWT authentication token.

Obtaining JWT token

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=xxxxxxx-1c8e-4ea4-a948-3857547232fa&client_secret=xxxxxx" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accpet: application/json"

Once the JWT token is generated, please execute the following command to convert HTML to Word format.

curl -X PUT "https://api.aspose.cloud/v4.0/words/EmptyHtmlFile.html/saveAs" \
-H "accept: application/json" \
-H "Authorization: Bearer <JWT TOken>" \
-H "Content-Type: application/json" \
-d "{\"SaveFormat\":\"docx\",\"FileName\":\"myResultant.docx\",\"ZipOutput\":false,\"UpdateLastSavedTimeProperty\":true,\"UpdateSdtContent\":true,\"UpdateFields\":true,\"Dml3DEffectsRenderingMode\":\"Basic\",\"UpdateLastPrintedProperty\":true}"

Conclusion

We have discussed the details on how to convert HTML to Word using the C# .NET code snippet. Since our APIs are based on REST architecture, so we have also explored the options to save HTML to Word using cURL commands. In case you encounter any issues while using our API, please feel free to contact us via free support forums.

Related Articles

We highly recommend visiting the following links to learn more about