Asynchronous Conversion

DynamicPDF Converter supports converting documents asynchronously to a PDF by either writing the output to a file or to a byte array.

Asynchronous Conversion to File

Converting asynchronously to a file is straightforward; replace the Convert method with ConvertAsync, as the following example illustrates.

static void Main(string [] args)
{
    ConversionOptions conversionOptions = new ConversionOptions(PageSize.Letter,
      PageOrientation.Portrait, 75);
     Converter.ConvertAsync("C:\\MyDocument.doc", "C:\\MyDocument.pdf", conversionOptions);
}

Asynchronous Conversion to Byte Array

You can also convert to a byte array asynchronously. The following example demonstrates converting multiple files to PDFs asynchronously. This example also illustrates using event handling for both failed and a successful conversions.

static void Main(string [] args)
{     
      var task1 = ConversionAsync();
      var task2 = ConversionHtmlStringAsync();

      task1.Wait();
      task2.Wait();
}

static async Task ConvertionAsync()
{
      ConversionOptions conversionOptions = new ConversionOptions(PageSize.Letter, PageOrientation.Portrait, 75);
      byte[] result = await Converter.ConvertAsync("C:\\MyDocument.doc", conversionOptions);
      if (result != null)
      {
          File.WriteAllBytes("C:/Output.pdf", result);
      }
}

static async Task ConversionHtmlStringAsync()
{
      string htmlString = "<html><head><title>ceTe software</title></head><body>DynamicPDF Converter</body></html>";

      await Converter.ConvertHtmlStringAsync(htmlString, @"C:\MyOutput.pdf");
}       
Sub Main()
      Dim task1 = ConvertionAsync()
	  Dim task2 = ConversionHtmlStringAsync()

      task1.Wait()  
      task2.Wait()
End Sub

Async Function ConvertionAsync() As Task
      Dim conversionOptions As ConversionOptions = New ConversionOptions(PageSize.Letter, PageOrientation.Portrait, 75)
      Dim result As Byte() = Await Converter.ConvertAsync("C:\MyDocument.doc", conversionOptions)

        If result IsNot Nothing Then
            File.WriteAllBytes("C:/Output.pdf", result)
        End If
End Function

Async Function ConversionHtmlStringAsync() As Task
        Dim htmlString As String = "<html><head><title>ceTe software</title></head><body>DynamicPDF Converter</body></html>"
        Await Converter.ConvertHtmlStringAsync(htmlString, "C:\MyOutput.pdf")
End Function

For more information, refer to the Converter.ConvertAsync documentation. There you will find more examples of the available options when converting a document to a PDF asynchronously.

GitHub Project

Refer to the converter-dotnet-core example project on GitHub for examples using C# and Visual Basic.

C# Visual Basic GitHub
ConverterAsyncExample.cs ConverterAsyncExample.vb https://github.com/DynamicPDF/converter-dotnet-core

In this topic