static async Task convertToPdf(ConversionUtil util) { Console.WriteLine("Starting conversion of sample.doc to PDF"); var bytes = File.ReadAllBytes("sample.doc"); var taskToken = await util.ConvertToPDF(new Common.Service.ConvertToPDFRequest { FileName = "sample.doc", FileData = bytes }); int retryCount = 0; while (retryCount < 600) { var status = await util.CheckConversionStatus(taskToken.TaskId); if (status.ConversionStatus == "Completed") { await downloadAndSaveFile(util, taskToken.TaskId, ".pdf"); Console.WriteLine("sample.doc successfully converted to PDF"); break; } else if (status.ConversionStatus == "Failed") { await deleteTaskCache(util, taskToken.TaskId); Console.WriteLine("sample.doc cannot converted to PDF"); break; } else await Task.Delay(1000);//Wait 1 second retryCount++; } Console.WriteLine(); }
static void Main(string[] args) { try { string apiKey = "Get your mashape api key from https://www.mashape.com/petadata/document-conversion-suite"; ConversionUtil util = new ConversionUtil(apiKey); convertHtml(util).Wait(); convertToPdf(util).Wait(); convertToTiff(util).Wait(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("All done."); } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex.Message); } Console.ReadLine(); }
static async Task deleteTaskCache(ConversionUtil util, string taskId) { await util.DeleteConversionTask(taskId); }
static async Task downloadAndSaveFile(ConversionUtil util, string taskId, string extension) { var bytes = await util.DownloadConvertedFile(taskId); var newFilePath = Path.Combine(Environment.CurrentDirectory, Guid.NewGuid().ToString() + extension); File.WriteAllBytes(newFilePath, bytes); deleteTaskCache(util, taskId); }
static async Task convertHtml(ConversionUtil util) { Console.WriteLine("Starting conversion of www.google.com to PDF"); var taskToken = await util.ConvertHTML(new Common.Service.ConvertHTMLRequest { Uri = "https://www.google.com", ConversionType = "PDF" //To convert image you must use "Image" }); int retryCount = 0; while (retryCount < 600) { var status = await util.CheckConversionStatus(taskToken.TaskId); if (status.ConversionStatus == "Completed") { await downloadAndSaveFile(util, taskToken.TaskId, ".pdf"); Console.WriteLine("https://www.google.com successfully converted to PDF"); break; } else if (status.ConversionStatus == "Failed") { await deleteTaskCache(util, taskToken.TaskId); Console.WriteLine("https://www.google.com cannot converted to PDF"); break; } else await Task.Delay(1000);//Wait 1 second retryCount++; } Console.WriteLine(); }