How to convert Word documents in C# using REST API Client with Aspose.Words Cloud?

Please share the sample C# code to get token from client ID and secret and request response code

@TrialUserSuyati

Please find the sample C# code to convert a DOCX file to PDF from the local drive using Aspose.Words Cloud SDK for .NET. The SDK. The SDK takes care of a lot of low-level details of authentication, requests and handling responses and lets you focus on writing code specific to your particular project. Hopefully, it will help you to accomplish the task. Please feel free to contact us for any further assistance.

// Get Client ID and Secret from https://dashboard.aspose.cloud/
WordsApi wordsApi = new WordsApi("xxxx-xxxx-xxxxxx-xxxxx-xxxx", "xxxxxxxxxxxxxxx");

var request = new ConvertDocumentRequest(
    document: File.OpenRead("C:/Temp/Test.docx"),
    format: "pdf"
);

var response = wordsApi.ConvertDocument(request);
var fileStream = System.IO.File.Create("C:/Temp/Test.pdf");
response.CopyTo(fileStream);

fileStream.Close();

Thanks there, But I need to call API directly with client ID and secret, I can’t use external Dll in my project. Can you share code for token and api request response.

@TrialUserSuyati

Thanks for your feedback. We are looking into your requirement and will provide you sample code soon.

@TrialUserSuyati

Please find sample code to get the JWT token and convert a Word document to PDF using Aspose.Words Cloud API and RestSharp Client. Hopefully, it will help you get started with Aspose.Words REST API via REST Client.

var client = new RestClient("https://api.aspose.cloud");

// OAuth request... 
RestRequest requestOAuth = new RestRequest("/connect/token", Method.POST);
requestOAuth.AddHeader("Authorization", "Basic " + client);
requestOAuth.AddHeader("Content-Type", "application/x-www-form-urlencoded");
requestOAuth.AddParameter("grant_type", "client_credentials");
// Get Client ID and Client Secret from https://dashboard.aspose.cloud/
requestOAuth.AddParameter("client_id", "xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx");
requestOAuth.AddParameter("client_secret", "xxxxxxxxxxxxxxxxxxxxxxxxxxx");
IRestResponse responseOAuth = client.Execute(requestOAuth);

// Convert the result to JSON
JObject result = JObject.Parse(responseOAuth.Content);


// Convert Word to PDF using Aspose.Words Cloud REST API

//Get the input file stream
FileInfo fileInfo = new FileInfo("C:/Temp/02_pages.docx");
byte[] bytes = null;
FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = fileInfo.Length;
bytes = br.ReadBytes((int)numBytes);
var request = new RestRequest("/v4.0/words/convert", Method.PUT);

request.AddHeader("Authorization", "Bearer " + result.GetValue("access_token").ToString());
request.AddHeader("Content-Type", "multipart/form-data");
request.AddHeader("Accept", "application/octet-stream");
//output file format
request.AddQueryParameter("format", "pdf");
//storage output path
request.AddQueryParameter("outPath","test_rest.pdf");

request.AddFileBytes("Document",bytes,"filename");

IRestResponse response = client.Execute(request);

Console.WriteLine("Completed...");
Console.ReadKey();

Thanks Tilal!, one more question on this, we are looking for the on Premise deployment on aspose API in our environment and then use it in our Dynamics CRM code, we need to call it as an API from our environment, so is On Premise API deployment is possible? how ? Also is there one time price possible for this ?

@TrialUserSuyati

Yes you can self host Aspose.Words Cloud Docker container.

As per my knowledge, we charge the usage monthly. But please post your query in Aspose.Purchase for exact details. My sales colleague will guide you accordingly.