Error while updating Conversion API from 19.6 to 20.8

I am using groupdocs in my application the version is 19.6

I updated the version to 20.8, groupdocs.annotations, groupdocs.signature works fine
now I updated conversion from 19.6 to 20.8, but I am facing lots of issues in my custom codes for ex
code for conversion

        public static GroupDocs.Conversion.Handler.ConversionHandler GetConversionHandler(bool isUseCache)
        {
            GroupDocs.Conversion.Config.ConversionConfig conversionConfig = new GroupDocs.Conversion.Config.ConversionConfig { StoragePath = StoragePath, CachePath = CachePath, OutputPath = OutputPath };
            // Set to use cache or not
            conversionConfig.UseCache = isUseCache;
            // Creating new ConversionHandler class object with ConversionConfig object
            GroupDocs.Conversion.Handler.ConversionHandler conversionHandler = new GroupDocs.Conversion.Handler.ConversionHandler(conversionConfig);
            // Returns the ConversionHandler static object
            return conversionHandler;
        }
        public static byte[] ConvertToPdfFromStreamToStream(byte[] inputFileBytes)
        {
            GroupDocs.Conversion.Handler.ConversionHandler conversionHandler = GetConversionHandler(false);
            using (MemoryStream inputStream = new MemoryStream(inputFileBytes))
            {
                var convertedDocumentStream = conversionHandler.Convert(inputStream, new GroupDocs.Conversion.Options.Save.PdfSaveOptions());
                using (MemoryStream targetStream = new MemoryStream())
                {
                    convertedDocumentStream.Save(targetStream);
                    return targetStream.ToArray();
                }
            }
        }
        public static byte[] ConvertToImageFromStreamToStream(byte[] inputFileBytes, GroupDocs.Conversion.Options.Save.ImageSaveOptions.ImageFileType outputFileType = GroupDocs.Conversion.Options.Save.ImageSaveOptions.ImageFileType.Tif, List<int> convertPages = null)
        {
            GroupDocs.Conversion.Handler.ConversionHandler conversionHandler = GetConversionHandler(false);

            var saveOptions = new GroupDocs.Conversion.Options.Save.ImageSaveOptions
            {
                ConvertFileType = outputFileType,
                TiffOptions = { Compression = GroupDocs.Conversion.Options.Save.TiffOptions.TiffCompression.Lzw },
                HorizontalResolution = 200,
                VerticalResolution = 200,
                PageNumber = 1,
                NumPagesToConvert = 1
            };
            using (MemoryStream inputStream = new MemoryStream(inputFileBytes))
            {
                var convertedDocumentStream = conversionHandler.Convert(inputStream, saveOptions);
                using (MemoryStream targetStream = new MemoryStream())
                {
                    convertedDocumentStream.Save(targetStream);
                    return targetStream.ToArray();
                }
            }
        }

now I know that config and handler api’s has been removed but I don’t know what should I use in the above scenarios as it is a custom code I cannot find anything in the example which I downloaded from github.

1 Like

@Niteen_Jadhav

In the above code you are enabling the cache and converting a source file to PDF. Let us breakdown code for you.
For caching purpose, please refer to this documentation article.
Secondly, you are converting a stream to stream (of course for the supported file formats):

public static void Run()
{
    using (Converter converter = new Converter(GetFileStream)) 
    {
        PdfConvertOptions options = new PdfConvertOptions();
        var pdfStream = new MemoryStream(); 
        converter.Convert(() => pdfStream, pdfConvertOptions);
    }
}
private static Stream GetFileStream() => File.OpenRead("sample.docx");

Hi,

I saw your above example and did something like below,

    public static GroupDocs.Conversion.Contracts.Func<ConverterSettings> GetConversionHandler(bool isUseCache)
    {
        FileCache cache = new FileCache(CachePath);
        GroupDocs.Conversion.Contracts.Func<ConverterSettings> settingsFactory = () => new ConverterSettings
        {
            Cache = cache
        };
        return settingsFactory;
    }

    public static byte[] ConvertToPdfFromStreamToStream(byte[] inputFileBytes)
    {
        var conversionHandler = GetConversionHandler(false);
        Stream GetFileStream() => new MemoryStream(inputFileBytes);
        using (Converter converter = new Converter(GetFileStream, conversionHandler))
        {
            PdfConvertOptions options = new PdfConvertOptions()
            {

            };
            var pdfStream = new MemoryStream();
            converter.Convert(() => pdfStream, options);
            using (MemoryStream ms = new MemoryStream())
            {
                pdfStream.CopyTo(ms);
                return ms.ToArray();
            }
        }
    }
    public static byte[] ConvertToImageFromStreamToStream(byte[] inputFileBytes, List<int> convertPages = null)
    {
        var conversionHandler = GetConversionHandler(false);
        Stream GetFileStream() => new MemoryStream(inputFileBytes);
        using (Converter converter = new Converter(GetFileStream))
        {
            ImageConvertOptions options = new ImageConvertOptions
            {
                TiffOptions = { Compression = GroupDocs.Conversion.Options.Convert.TiffCompressionMethods.Lzw },
                HorizontalResolution = 200,
                VerticalResolution = 200,
                PageNumber = 1,
            };
            var imageStream = new MemoryStream();
            converter.Convert(() => imageStream, options);
            using (MemoryStream ms = new MemoryStream())
            {
                imageStream.CopyTo(ms);
                return ms.ToArray();
            }
        }
    }

but on the below line
converter.Convert(() => pdfStream, options);
I am getting the following error

Type ‘GroupDocs.Conversion.Options.Convert.Font’ in Assembly
‘GroupDocs.Conversion, Version=20.8.0.0, Culture=neutral, PublicKeyToken=716fcc553a201e56’ is not marked as serializable.

what is the problem with my code?

@Niteen_Jadhav

We are investigating this scenario. Your investigation ticket ID is CONVERSIONNET-5478.

@Niteen_Jadhav

We have an update on this ticket. A quick fix for this issue, you can try to not use cache. Default caching implementation try to serialize load and convert options and most likely that’s why you are seeing this error.

this is what I actually did, below is my code for the same

    public static byte[] ConvertToPdfFromStreamToStream(byte[] inputFileBytes)
    {
        Stream GetFileStream() => new MemoryStream(inputFileBytes);
        using (Converter converter = new Converter(GetFileStream))
        {
            PdfConvertOptions options = new PdfConvertOptions();
            var pdfStream = new MemoryStream();
            converter.Convert(() => pdfStream, options);
            using (MemoryStream ms = new MemoryStream())
            {
                int a = (int)pdfStream.Length; //close stream error on this line
                pdfStream.CopyTo(ms);
                return ms.ToArray();
            }
        }
    }

but the problem is the following error is coming "Cannot access a closed Stream"

so Finally I make it worked with the below code

    public static byte[] ConvertToPdfStream(string fullPath)
    {
        using (Converter converter = new Converter(fullPath))
        {
            var date = DateTime.Now;
            string fullConversionPath = StoragePath + "\\ConversionPath";
            string fileName = "converted_" + Convert.ToString(date) + ".pdf";
            string fileNameWithPath = fullConversionPath + "\\" + fileName;
            if (!System.IO.File.Exists(fullConversionPath))
            {
                Directory.CreateDirectory(fullConversionPath);
            }
            PdfConvertOptions options = new PdfConvertOptions();
            converter.Convert(fileNameWithPath, options);
            var fileBytes = System.IO.File.ReadAllBytes(fileNameWithPath);
            File.Delete(fileNameWithPath);
            return fileBytes;
        }
    }

the above code worked properly but here I have to create a file on my server.

1 Like

@Niteen_Jadhav

Good to know that the issue is fixed.

We’ll take a look at this scenario as well.