This tutorial guides how to convert DOCX to HTML with NET REST API. You may use Aspose.Words for .NET Cloud SDK to develop a DOCX to HTML converter with C# Low Code API. This sample code can be used in any platform supporting .NET Cloud SDK and assists in exploring the API with great details.
Prerequisite
- Create an account and get API credentials
- Download Aspose.Words Cloud SDK for Dotnet
- Setup C# project with the above SDK
Steps to Transform DOCX to DOC with C# Low Code API
- Create the Configuration object and set the client secret and ID
- Create the WordsApi object using the Configuration object
- Set the names of the input and output files
- Read the input DOCX file and initialize the position
- Instantiate the ConvertDocumentRequest() method using the above byte array and format
- Invoke the ConvertDocument method to Convert DOCX to HTML using REST API
- Save the output HTML file on the local disk
Follow these steps to convert DOCX file to HTML with C# REST API. Initialize the WordsApi class object with the client ID and secret followed by reading the source HTML file into a byte array. Instantiate the ConvertDocumentRequest and use it in the WordsApi.ConvertDocument() method for the transformation.
Code to Format DOCX to DOC with C# REST API
using Aspose.Words.Cloud.Sdk; | |
using Aspose.Words.Cloud.Sdk.Model.Requests; | |
using System; | |
using System.IO; | |
namespace WordsSample.Words | |
{ | |
public class DocxToHtml | |
{ | |
public void ConvertDocxToHtmlAsync() | |
{ | |
try | |
{ | |
var conf = new Configuration(); | |
conf.ClientSecret = "Client Secret"; | |
conf.ClientId = "Client ID"; | |
//Create SDK object | |
WordsApi api = new WordsApi(conf); | |
//string localPath = @""; | |
string input = "Test1.docx"; | |
string output = "DOCXToHTML"; | |
string format = "html"; | |
//Read input file to bytes array | |
var stream = File.Open(input, FileMode.Open); | |
stream.Position = 0; | |
var request = new ConvertDocumentRequest(stream, format, null, null, null, null, null, null, null); | |
var task = api.ConvertDocument(request); | |
task.Wait(); | |
var outputStream = task.Result; | |
outputStream.Position = 0; | |
using (var fileStream = File.Create(output + "." + format)) | |
{ | |
outputStream.Seek(0, SeekOrigin.Begin); | |
outputStream.CopyTo(fileStream); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} |
This code snippet provides the basis to convert DOCX to HTML programmatically with C# Low Code API. You may either read the source file into a byte array or load the stream from any other source. Remember to initialize the position to 0 to avoid exceptions in the code after reading the file.
You may also check out another similar feature on the following page: How to convert DOCX to DOC with NET REST API.