Save file to stream

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 document parameter could be of Func<Stream>, Func<FileType, Stream>, Func<int, Stream>, or Func<int, FileType, Stream> type.

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

With v24.10 and later:

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.OpenOrCreate);
}

Before v24.10:

public static void Run()
{
    Func<Stream> getOutputStream = () => GetFileStream("c:\\files\\converted.pdf");

    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.OpenOrCreate);
}

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

With v24.10 and later:

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)
        .Convert();
}

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

Before v24.10:

public static void Run()
{
    Func<Stream> getOutputStream = () => GetFileStream("c:\\files\\converted.pdf");

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

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