Convert and save result as stream

Hi there, I have this peace of code:

            // // Convert the document to PDF
            var convertedStream = new MemoryStream();

            using var converter = new Converter(() => new MemoryStream(seekableStream.ToArray()));

            // Convert and save the result to the convertedStream
            converter.Convert((SaveContext context) => convertedStream, new PdfConvertOptions());

            // Reset the convertedStream position
            convertedStream.Position = 0;

After executing the line converter.Convert(...) the stream convertedStream is closed and If I try to access I get an error. Why is the stream closed? How can I work with the stream after being converted to PDF if this is closed after being converted?

Regards, Oscar

@oscar.rodriguez

It seems that the issue you’re encountering is related to the way the Converter class handles the output stream during the conversion process. When you pass a stream to the Convert method, the Converter may close the stream after the conversion is complete, which is why you’re seeing an error when trying to access convertedStream afterward.

To work around this issue, you can create a new MemoryStream instance for the output and ensure that it remains open after the conversion. Here’s an example of how you can modify your code:

// Convert the document to PDF
using var convertedStream = new MemoryStream();

using var converter = new Converter(() => new MemoryStream(seekableStream.ToArray()));

// Convert and save the result to the convertedStream
converter.Convert((SaveContext context) => 
{
    // Return the convertedStream for writing
    return convertedStream;
}, new PdfConvertOptions());

// Reset the convertedStream position
convertedStream.Position = 0;

// Now you can work with convertedStream

In this example, the convertedStream is declared with using, which ensures that it is properly disposed of after use. However, if you need to keep it open for further processing, you can manage its lifecycle outside of the using statement. Just make sure to handle the stream appropriately to avoid any resource leaks.

If you still encounter issues, consider checking the documentation for the Converter class to see if there are any specific notes regarding stream management during conversion.

Thank you for your answer, It doesn’t work in this way either, this was exactly what Chat GPT suggested me, but it still closes the stream, and the only difference with my original code is the using for the convertedStream, anyway it doesn’t work in this way.

Hello @oscar.rodriguez ,

Thank you for contacting us. To handle the stream after conversion, kindly use the code provided below.

using (Converter converter = new Converter("inputFilePath"))
{
    converter.Convert(new PdfConvertOptions(), (ConvertedContext convertedContext) =>
    {
        var stream = convertedContext.ConvertedStream;
        //work with stream
    });
 
}

Should you have further questions, feel free to reach out to us anytime.

I have to use a stream as input, the code you provided me is for input file path and contains the ConvertedContext as a parameter in the lambda function, but for the stream overloading, I only see the SaveContext parameter, which does not contain the convertedStream property…

@oscar.rodriguez ,

Alternatively, you can use an input stream instead of an input file. Here’s the modified version of your example:

            // Convert the document to PDF
            var convertedStream = new MemoryStream();

            using var converter = new Converter(() => new MemoryStream(seekableStream.ToArray()));

            // Convert and save the result to the convertedStream
            converter.Convert(new PdfConvertOptions(), (ConvertedContext convertedContext) =>
            {
                convertedStream = (MemoryStream)convertedContext.ConvertedStream;

                //work with stream
            });

If this method is also unsuitable for your needs, please describe your use case in more detail, as we are not entirely sure how we can help you in this situation.

Note: You correctly pointed out that the stream is disposed after conversion. This was implemented because most clients forgot to close it, leading to memory leaks. To manage the converted stream, the ConvertedContext.ConvertedStream is used, as I already demonstrated to you.