Document Conversion Suite

FREEMIUM
By petadata | Updated לפני חודש | Data
Popularity

8.3 / 10

Latency

461ms

Service Level

100%

Health Check

N/A

Back to All Tutorials (6)

Convert documents to PDF by using C#

In this tutorial we will cover how to implement asynchronous approach of Document Conversion Suite. You only need to send source document to SubmitTIFFConversionTask or SubmitPDFConversionTask API methods to receive task identifier as response. There is also SubmitDOCXConversionTask method to convert PDF documents to editable Microsoft Word document.

After receiving task identifier you need to check task status by calling GetConversionTaskStatus method. You need to call same method after a few seconds delay as long as task status equals to “Waiting”. Once you receive “Completed” from GetConversionTaskStatus method you can call DownloadResult method to download final document file. You can use the C# implementation below.

async Task<string> SubmitPdfConversionTask(string filePathToConvert, string rapidApiKey)
{
    using (HttpClient httpClient = new HttpClient())
    {
        using (MultipartFormDataContent form = new MultipartFormDataContent())
        {
            form.Headers.Add("X-RapidAPI-Key", rapidApiKey);
            form.Add(new StringContent("Sebastian"), "authorName");
            form.Add(new StringContent("Final report"), "title");
            form.Add(new StringContent("Reports, Final"), "keywords");
            form.Add(new StringContent("Quarterly Final Report"), "name");
            var fileBytes = await File.ReadAllBytesAsync(filePathToConvert);
            var fileName = Path.GetFileName(filePathToConvert);
            form.Add(new ByteArrayContent(fileBytes, 0, fileBytes.Length), "file", fileName);

            using (var response = await httpClient.PostAsync("https://petadata-document-conversion-suite.p.rapidapi.com/SubmitPDFConversionTask", form))
            {
                response.EnsureSuccessStatusCode();
                return await response.Content.ReadAsStringAsync();
            }
        }
    }
}

async Task<string> GetConversionTaskStatus(string taskId, string rapidApiKey)
{
    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("X-RapidAPI-Key", rapidApiKey);

        using (var response = await httpClient.GetAsync($"https://petadata-document-conversion-suite.p.rapidapi.com/GetConversionTaskStatus?taskId={taskId}"))
        {
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsStringAsync();
        }
    }
}

async Task<byte[]> DownloadResult(string taskId, string rapidApiKey)
{
    using (HttpClient httpClient = new HttpClient())
    {
        httpClient.DefaultRequestHeaders.Add("X-RapidAPI-Key", rapidApiKey);

        using (var response = await httpClient.GetAsync($"https://petadata-document-conversion-suite.p.rapidapi.com/DownloadResult?taskId={taskId}"))
        {
            response.EnsureSuccessStatusCode();
            return await response.Content.ReadAsByteArrayAsync();
        }
    }
}

var rapidApiKey = "<YOUR RAPIDAPI KEY HERE>";
var filePathToConvert = "<FILE PATH TO CONVERT>";
var taskId = await SubmitPdfConversionTask(filePathToConvert, rapidApiKey);
var retryCount = 0;
while (retryCount < 100)
{
    retryCount++;
    await Task.Delay(5000);
    var status = await GetConversionTaskStatus(taskId, rapidApiKey);
    if (status == "Completed")
    {
        var fileBytes = await DownloadResult(taskId, rapidApiKey);
        await File.WriteAllBytesAsync("final_report.pdf", fileBytes);
        break;
    }
    else if (status == "Waiting")
    {
        continue;
    }
    else if (status == "Failed")
    {
        throw new Exception("Cannot convert file");
    }
    else
    {
        throw new Exception("Invalid status");
    }
}