Convert DOCX to PDF in .NET cloud application using Aspose.Words REST API

Hi,
I’m using a cloud application based on .Net.
I need to convert docx documents that are being created in the application to pdf.
I struggle to understand do I need to use the Aspose cloud/Aspose PDF or other product.

Looking forward to having your advice.

Thanks,
Raz

@dev1sheeta

Please note that Aspose.PDF does not offer DOCX to PDF conversion. Instead, Aspose.Words is the API specialized to deal with Word file as well as their conversion into PDF format. We are moving your inquiry in Aspose.Words category where you will be assisted accordingly.

@dev1sheeta,

With Aspose.Words Cloud APIs, you can first upload Word DOCX files to Aspose.Words Cloud Storage (or any other Cloud Storage of your choice) and then make calls to Aspose.Words web services (e.g. using .NET SDK) to perform Word DOCX document to PDF file conversion. In this case, Aspose will use its own servers to do all the cloud computing i.e. you don’t need to worry about the CPU, RAM and other hardware utilizations.

With Aspose.Words for .NET API, you can perform Word DOCX to PDF conversions inside your own computer. In this case, Aspose will not transmit any files from your machine to its server for conversions. However, if the DOCX file is not present on your machine, then you first need to download that DOCX file from file hosting server to your computer, convert the file locally and then upload converted PDF file to file server.

Thank you very much.

I’m using a cloud application so there is no way to download it to the user’s computer.
I would like to use the Cloud service.
Can you point me please to the exact library to download or api to use to do it?

I’ve created an example application and I’m interested in using Apose services.

Looking forward to having your assistance.

Thanks,
Raz

@dev1sheeta

You can convert DOCX to PDF using Aspose.Words Cloud SDK for .NET. First thing first, sign up with aspose.cloud and get the credentials. Either you can convert files from cloud storage or from the request body(streams). Please check the following sample code for details.

Convert Documents from Cloud Storage:

[GET ​/words​/{name}(Aspose.Words Cloud - API References) Converts a document in cloud storage to the specified format

C# code:

// Get Client Id and Client Secret from https://dashboard.aspose.cloud/
WordsApi wordsApi = new WordsApi(ClientId, ClientSecret);
string localFolder = "C:/Temp/";
string remoteFolder = "Temp";
string localName = "02_pages.docx";
string remoteName = "02_pages.docx";
string outPutName = "02_pages.pdf";

Aspose.Words.Cloud.Sdk.Model.Requests.UploadFileRequest uploadRequest = new Aspose.Words.Cloud.Sdk.Model.Requests.UploadFileRequest(System.IO.File.OpenRead(localFolder + localName), remoteFolder + "/" + remoteName);
wordsApi.UploadFile(uploadRequest);

var convertRequest = new GetDocumentWithFormatRequest(
    name: remoteName,
    format: "pdf",
    folder: remoteFolder,
    outPath: outPutName
);
var convertResponse = wordsApi.GetDocumentWithFormat(convertRequest);

var requestDownload = new Aspose.Words.Cloud.Sdk.Model.Requests.DownloadFileRequest(path: remoteFolder+"/"+outPutName);
var downloadResponse = wordsApi.DownloadFile(requestDownload);
var fileStream = System.IO.File.Create("C:/Temp/02_pages_out.pdf");
downloadResponse.CopyTo(fileStream);
fileStream.Close();

Convert Documents without Cloud Storage:

​PUT /words​/convert Converts a document on a local drive to the specified format

C# code:

// Get Client Id and Client Secret from https://dashboard.aspose.cloud/
WordsApi wordsApi = new WordsApi(ClientId, ClientSecret);

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

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

You can download the Aspose.Words Cloud SDK for .NET from GitHub or NuGet. And secondly please note we have used default internal storage(Aspose) so did not pass the cloud storage name in the API call explicitly. If you want to use one of the other supported Cloud storages then you need to pass the storage name in the API request as well.

Thank you very much
I eventually used this code:

WordsApi asposeCloudWordsApi = new WordsApi("appkey, "appsid");
ConvertDocumentRequest convertDocumentRequest = new ConvertDocumentRequest(streamfile, "pdf");
Stream result = asposeCloudWordsApi.ConvertDocument(convertDocumentRequest);
result.ReadAllBytes()

and it’s working.

what do you think?

Thanks,
Raz

@dev1sheeta

The ConvertDocument returns a Stream object and it does not support the ReadAllBytes method. Please try the above shared CovnertDocument sample code and amend it as per your requirement. Please feel free to ask the assistance if you face any issues.

1 Like

Hi,
I understand, this is that it’s working.
I’m getting the file converted to pdf.
Is there something I’m missing?

@dev1sheeta

I am afraid I could not get your point exactly. I will appreciate it if you please share some more details of your requirement/issue.