Save file to stream

View as MarkdownMarkdown

Set up your AI agent

Give your coding agent live access to the GroupDocs documentation and API reference through the GroupDocs Docs MCP server — no install, no API key. Learn more

claude mcp add --transport http groupdocs-docs https://docs.groupdocs.com/mcp

Also process documents locally

The GroupDocs.Conversion MCP server lets your agent work with files on your machine; they are never uploaded. It needs the .NET 10 SDK or Docker.

claude mcp add groupdocs-conversion -- dnx GroupDocs.Conversion.Mcp --yes
Folders, licensing and more →

To save the conversion results to a stream, follow these steps:

  1. Specify the method to obtain the stream where the converted file will be sent.
  2. Pass the method’s name as the document parameter to the Convert() method implementations.

The stream provider is a Func<SaveContext, Stream> that receives the whole converted document, or a Func<SavePageContext, Stream> that receives each converted page separately.

The following code snippet shows how to save a file to a stream:

public static void Run()
{
    Func<SaveContext, Stream> getOutputStream = saveContext => GetFileStream($"c:\\files\\converted.{saveContext.TargetFormat.Extension}");

    using (Converter converter = new Converter("c:\\files\\sample.docx"))
    {
        PdfConvertOptions options = new PdfConvertOptions();
        // Pass the output stream as parameter
        converter.Convert(getOutputStream, options);
    }

    Console.WriteLine($"\nSource file converted successfully.\nSent file to stream.");
}

// Obtain the stream for the conversion output
public static Stream GetFileStream(string outFile)
{
    return new FileStream(outFile, FileMode.Create);
}

You can also use the ConvertTo(Func convertedStreamProvider) fluent syntax method to save a file to a stream:

public static void Run()
{
    Func<SaveContext, Stream> getOutputStream = saveContext => GetFileStream($"c:\\files\\converted.{saveContext.TargetFormat.Extension}");

    FluentConverter
        // Specify source file location
        .Load("c:\\files\\sample.docx")
        // Pass the output stream as parameter
        .ConvertTo(getOutputStream)
        .WithOptions(new PdfConvertOptions())
        .Convert();
}

// Obtain the stream for the conversion output
public static Stream GetFileStream(string outFile)
{
    return new FileStream(outFile, FileMode.Create);
}