How to Update Email Load Options and Convert to PDF Using Conversion for .NET API v24.10+

Not sure how to modify the below code to use the new Conversion APIs from 24.10 onwards?

It says:
“warning CS0618: ‘Converter.Converter(string, Func)’ is obsolete: ‘The constructor is obsolete from v24.10. Will be removed in v25.1. Use the constructor with Func<LoadContext, LoadOptions> parameter’”

                GroupDocs.Conversion.FileTypes.FileType fileType = GroupDocs.Conversion.FileTypes.FileType.FromExtension(System.IO.Path.GetExtension(sourceFileName));

                if (!(fileType is GroupDocs.Conversion.FileTypes.CompressionFileType))  // Ignore zip and 7z
                {
                    GroupDocs.Conversion.Options.Load.LoadOptions loadOptions()
                    {
                        // Force CSV files to be processed as text format instead of spreadsheet for performance reasons
                        if (fileType == GroupDocs.Conversion.FileTypes.SpreadsheetFileType.Csv)
                        {
                            return new GroupDocs.Conversion.Options.Load.TxtLoadOptions();
                        }

                        // Only convert top-level attachments (i.e. embedded images) for emails
                        if (fileType is GroupDocs.Conversion.FileTypes.EmailFileType type)
                        {
                            return new GroupDocs.Conversion.Options.Load.EmailLoadOptions
                            {
                                ConvertOwner = true,
                                ConvertOwned = false,
                                Format = type
                            };
                        }
                        return null;
                    }

                    using (GroupDocs.Conversion.Converter converter = new GroupDocs.Conversion.Converter(sourceFileName, loadOptions))
                    {
                        GroupDocs.Conversion.Options.Convert.PdfConvertOptions options = new GroupDocs.Conversion.Options.Convert.PdfConvertOptions
                        {
                            PdfOptions = new GroupDocs.Conversion.Options.Convert.PdfOptions
                            {
                                PdfFormat = GroupDocs.Conversion.Options.Convert.PdfFormats.PdfA_2U
                            }
                        };

                        converter.Convert(destFileName, options);
                    }
                }

@jarrodwee
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CONVERSIONNET-7432

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@jarrodwee

Starting from version 25.1, the API will pass a LoadContext to the delegates, requiring modifications to support methods like loadOptions, convertOptions, etc. Specifically, the loadOptions method signature needs to be updated from:

GroupDocs.Conversion.Options.Load.LoadOptions loadOptions()

to:

GroupDocs.Conversion.Options.Load.LoadOptions loadOptions(LoadContext loadContext)

Here’s the updated snippet with the required change applied:

GroupDocs.Conversion.FileTypes.FileType fileType = GroupDocs.Conversion.FileTypes.FileType.FromExtension(System.IO.Path.GetExtension(sourceFileName));

if (!(fileType is GroupDocs.Conversion.FileTypes.CompressionFileType)) // Exclude zip and 7z files
{
    GroupDocs.Conversion.Options.Load.LoadOptions loadOptions(LoadContext loadContext)
    {
        // Process CSV files as text format for improved performance
        if (fileType == GroupDocs.Conversion.FileTypes.SpreadsheetFileType.Csv)
        {
            return new GroupDocs.Conversion.Options.Load.TxtLoadOptions();
        }

        // For emails, only convert top-level attachments (e.g., embedded images)
        if (fileType is GroupDocs.Conversion.FileTypes.EmailFileType type)
        {
            return new GroupDocs.Conversion.Options.Load.EmailLoadOptions
            {
                ConvertOwner = true,
                ConvertOwned = false,
                Format = type
            };
        }
        return null;
    }

    using (GroupDocs.Conversion.Converter converter = new GroupDocs.Conversion.Converter(sourceFileName, loadOptions))
    {
        GroupDocs.Conversion.Options.Convert.PdfConvertOptions options = new GroupDocs.Conversion.Options.Convert.PdfConvertOptions
        {
            PdfOptions = new GroupDocs.Conversion.Options.Convert.PdfOptions
            {
                PdfFormat = GroupDocs.Conversion.Options.Convert.PdfFormats.PdfA_2U
            }
        };

        converter.Convert(destFileName, options);
    }
}

This adjustment ensures compatibility with the updated API requirements by incorporating the LoadContext parameter in the loadOptions delegate.

Thanks, how about for loadOptions such as the below?
How should it be changed?

GroupDocs.Conversion.Options.Load.LoadOptions loadOptionsProvider(string nativeFileName, GroupDocs.Conversion.FileTypes.FileType nativeFileType, Stream nativeStream)
{
 ...
}

@jarrodwee

The delegate needs to be modified to the following:

GroupDocs.Conversion.Options.Load.LoadOptions loadOptionsProvider(LoadContext loadContext)
{
    string nativeFileName = loadContext.SourceFileName;
    GroupDocs.Conversion.FileTypes.FileType nativeFileType = loadContext.SourceFormat;
    // The nativeStream is not required
    ...
}

But I need the nativeStream to copy out the original constituent files from file types which are recursively converted, e.g. MSG, ZIP, etc.

Is this capability missing in 24.10 onwards?

1 Like

@jarrodwee

You’re correct that the source stream during recursive conversion may originate from a container document. We’ll be adding a new property to LoadContext to provide the source stream in the loadOptions delegate. This update will ensure that the loadContext is available to the delegate. This change is scheduled to be included in version 25.1, which is expected to be released this week.

Noted that GroupDocs.Conversion 25.1 was released almost 3 weeks ago.
Is there any estimate on when GroupDocs.Total.NETFramework 25.1 will be released?

@jarrodwee

GroupDocs.Total for .NET 25.1 is now available to download.

Thanks for the update!

1 Like