/// <summary> /// Example of saving the same Word docx to PDF and to PNG without uploading the same Word file two times. /// https://www.convertapi.com/docx-to-pdf /// https://www.convertapi.com/docx-to-png /// </summary> static async Task Main(string[] args) { //Get your secret at https://www.convertapi.com/a var convertApi = new ConvertApi("your api secret"); const string sourceFile = @"..\..\..\TestFiles\test.docx"; var fileParam = new ConvertApiFileParam(sourceFile); var convertToPdf = await convertApi.ConvertAsync("docx", "pdf", fileParam); var outputFileName = convertToPdf.Files[0]; var fileInfo = await outputFileName.SaveFileAsync(Path.Combine(Path.GetTempPath(), outputFileName.FileName)); Console.WriteLine("The PDF saved to " + fileInfo); var convertToPng = await convertApi.ConvertAsync("docx", "png", fileParam); foreach (var processedFile in convertToPng.Files) { fileInfo = await processedFile.SaveFileAsync(Path.Combine(Path.GetTempPath(), processedFile.FileName)); Console.WriteLine("The PNG saved to " + fileInfo); } Console.ReadLine(); }
private static List <ConvertApiFileParam> FilesToParams(List <FileInfo> readyFiles) { return(readyFiles.OrderBy(f => f.Length).Select(f => { _concSem.Wait(); var fp = new ConvertApiFileParam(f); // Delete uploaded file from local file system fp.GetValueAsync().ContinueWith(fm => { _concSem.Release(); try { var dir = f.Directory; f.Delete(); if (!dir.GetFileSystemInfos().Any()) { dir.Delete(); } } catch (Exception e) { Console.Error.WriteLine(e.Message); } }); return fp; }).ToList()); }
/// <summary> /// The example shows how to merge Word and Powerpoint documents to one PDF by converting them to PDFs and then merging. /// https://www.convertapi.com/docx-to-pdf /// https://www.convertapi.com/pdf-to-merge /// </summary> static async Task Main(string[] args) { try { Console.WriteLine("The example shows how to merge Word and Powerpoint documents to one PDF by converting them to PDFs and then merging. Please wait..."); var sw = new Stopwatch(); sw.Start(); //Get your secret at https://www.convertapi.com/a var convertApi = new ConvertApi("your api secret"); var destinationFileName = Path.Combine(Path.GetTempPath(), $"test-merged-{Guid.NewGuid()}.pdf"); var convertApiFileParam = new ConvertApiFileParam(@"..\..\..\..\TestFiles\test.docx"); var doc1 = convertApi.ConvertAsync("docx", "pdf", convertApiFileParam); Console.WriteLine("Word sent for conversion..."); var doc2 = convertApi.ConvertAsync("pptx", "pdf", new ConvertApiFileParam(@"..\..\..\..\TestFiles\test.pptx")); Console.WriteLine("PowerPoint sent for conversion..."); var mergeTask = await convertApi.ConvertAsync("pdf", "merge", new ConvertApiFileParam(await doc1), new ConvertApiFileParam(await doc2)); var saveFiles = await mergeTask.Files.First().SaveFileAsync(destinationFileName); Console.WriteLine("The PDF saved to " + saveFiles); sw.Stop(); Console.WriteLine("Elapsed " + sw.Elapsed); } //Catch exceptions from asynchronous methods catch (ConvertApiException e) { Console.WriteLine("Status Code: " + e.StatusCode); Console.WriteLine("Response: " + e.Response); } Console.ReadLine(); }
private static Task Convert(List <ConvertApiFileParam> fileParams, ConvConfig cfg, List <BlockingCollection <ConvertApiFileParam> > outQueues) { var fileNamesStr = string.Join(", ", fileParams.Select(f => f.GetValueAsync().Result.FileName).ToList()); // Console.WriteLine($"Converting: {fileNamesStr} -> {cfg.DestinationFormat}"); var orderedFileParams = fileParams.OrderBy(fp => fp.GetValueAsync().Result.FileName).ToList(); var srcFormat = orderedFileParams.First().GetValueAsync().Result.FileExt; var convertParams = orderedFileParams.Cast <ConvertApiBaseParam>().Concat(cfg.Params).ToList(); Cde?.AddCount(); _concSem.Wait(); return(_convertApi.ConvertAsync(srcFormat, cfg.DestinationFormat, convertParams) .ContinueWith(tr => { _concSem.Release(); if (tr.IsCompletedSuccessfully) { try { tr.Result.Files.ToList().ForEach(resFile => { if (outQueues.Any()) { var fp = new ConvertApiFileParam(resFile.Url); outQueues.ForEach(action: q => q.Add(fp)); Cde?.Signal(); } if (!outQueues.Any() || cfg.SaveIntermediate) { resFile.SaveFileAsync(Path.Join(cfg.Directory.FullName, resFile.FileName)) .ContinueWith(tfi => { Console.WriteLine(tfi.Result.FullName); Cde?.Signal(); if (Cde?.CurrentCount == 1) { Cde?.Signal(); // Removing initial count to unblock wait } }); } }); } catch (ConvertApiException e) { Console.Error.WriteLine($"Unable to convert: {fileNamesStr} -> {cfg.DestinationFormat}\n{e.Message}\n{e.Response}"); } catch (Exception e) { Console.Error.WriteLine($"Unable to convert: {fileNamesStr} -> {cfg.DestinationFormat}\n{e.Message}"); } } else { Console.Error.WriteLine($"Unable to convert: {fileNamesStr} -> {cfg.DestinationFormat}\n{tr.Exception?.Flatten().Message}"); Cde?.Signal(); if (Cde?.CurrentCount == 1) { Cde?.Signal(); // Removing initial count to unblock wait } } })); }