Free Support Forum - groupdocs.com

Convert stream to PDF using document conversion API in .NET

Hi, I’m using the free version of GroupDocs.Conversion to convert document streams of various types into PDF streams in .NET. However, I noticed that the ConversionHandler has been moved into the Legacy namespace and marked as obsolete and will be removed in January 2020.

I could not find any directions to new namespaces for the ConversionHandler in your .NET documentation, so are there any similar namespaces I can move to for the ConversionHandler at all?

Thank you!

1 Like

@vidaru,

In latest version of the API, we added Converter class as a single entry point to manage the document conversion process to any supported file format. Please go through the migration notes.

ConversionHandler is moved to GroupDocs.Conversion.Legacy.Handler.ConversionHandler namespace. Below is the code that you will use in latest version for document conversion process:

string documentPath = @"C:\sample.docx"; 
string outputPath = @"C:\output\converted.pdf";
  
using (Converter converter = new Converter(documentPath))
{
    PdfConvertOptions convertOptions = new PdfConvertOptions();
    converter.Convert(outputPath, convertOptions);
}

Please let us know the issue(s) you are facing in new version.

Hello, thank you for the reply.

The issue I’m facing in the new version is that I haven’t found a way to return the converted document as a stream. In my problem I’m unable to specify an input path and output path as the files are not stored on disk. My goal is therefore to convert an input document stream into a PDF stream, as I’ve done below in the legacy version:

        var stream = await new AmazonS3Helper().GetFileAsync(filePath, fileName);
     
        var conversionConfig = new ConversionConfig
        {
            StoragePath = ".",
            OutputPath = "."
        };

        var saveOptions = new PdfSaveOptions();
        var cellsLoadOptions = new SpreadsheetLoadOptions();
        cellsLoadOptions.OnePagePerSheet = false;
        saveOptions.ConvertFileType = PdfSaveOptions.PdfFileType.Pdf;
        var conversionHandler = new ConversionHandler(conversionConfig);

        Func<LoadOptions> getLoadOptions = () => new SpreadsheetLoadOptions
        {
            HideComments = true,
            OnePagePerSheet = false
        };

        var convertedDocument = conversionHandler.Convert(stream, "", getLoadOptions.Invoke(), saveOptions);

        MemoryStream convertedStream = new MemoryStream();
        convertedDocument.Save(convertedStream);

Please let me know if there is a way to achieve this in the new version.

1 Like

@vidaru,

Please have a look at this article. You can load document from Amazon S3 storage and return result as a stream. Let us know if this is helpful.

I had a look at the article, and it does take me halfway there, as it enables me to get a stream and convert it. However, is it also possible to return the converted file as a stream, instead of saving it at a specified path?

1 Like

@vidaru,

We are investigating this. Your investigation ticket ID is CONVERSIONNET-3522. As there’s any update, we’ll notify you.

@vidaru,

Hopefully below code example will help you:

const string source = "source.docx";
var streams = new Dictionary<int, MemoryStream>();
using (var converter = new Converter(source))
{
    var options = new ImageConvertOptions
    {
        Format = ImageFileType.Png
    };
    converter.Convert(page => new MemoryStream(), (page, convertedStream) =>
    {
        var memoryStream = new MemoryStream();
        convertedStream.CopyTo(memoryStream);
        streams.Add(page, memoryStream);
    }, options);
}