Get possible conversions

There are multiple target formats available when converting documents with GroupDocs.Conversion and you can always refer to supported file formats documentation for more details.

But what about getting possible conversions programmatically? For example, it could allow end-users to select the target format for a specific document or to display the complete list of supported formats. Fortunately GroupDocs.Conversion API provides several ways to achieve this, so please check the available options below.

Get possible conversions for a specific document

When you need to know possible conversions for a provided source document you can do this by following the below steps:

  1. Create an instance of the Converter class by passing the source document path as the constructor’s parameter.
  2. Call the GetPossibleConversions method of the Converter object.

The method will return the PossibleConversions collection with a complete list of possible conversions for the source document type.

The following code snippet shows how to get possible conversions of the source document:

using (Converter converter = new Converter("sample.docx"))
{
    PossibleConversions conversions = converter.GetPossibleConversions();
    Console.WriteLine("The source document is of type {0} and could be converted to:", conversions.Source.Extension);

    foreach (var conversion in conversions.All)
    {
        Console.WriteLine("\t {0} as {1} conversion.",
            conversion.Format,
            conversion.IsPrimary ? "primary" : "secondary");
    }    
}

Or you can use a fluent syntax

PossibleConversions conversions = FluentConverter.Load("sample.docx").GetPossibleConversions();
Console.WriteLine("The source document is of type {0} and could be converted to:", conversions.Source.Extension);

foreach (var conversion in conversions.All)
{
     Console.WriteLine("\t {0} as {1} conversion.",
        conversion.Format,
        conversion.IsPrimary ? "primary" : "secondary");
}    
Warning
Fluent syntax is introduced in v22.1

Get all available conversions 

If it is required to programmatically obtain a collection of all supported conversions it is as easy as calling a static GetAllPossibleConversions method of the Converter class.

The following code snippet shows how to get all possible conversions:

var allPossibleConversions = Converter.GetAllPossibleConversions();
foreach (var possibleConversions in allPossibleConversions)
{
    Console.WriteLine($"Source format: {possibleConversions.Source.Description}");
    foreach (var conversion in possibleConversions.All)
    {
        Console.WriteLine("\t...can be converted to {0} format as {1} conversion.",
            conversion.Format,
            conversion.IsPrimary ? "primary" : "secondary");        
    }
}