New to Telerik Document ProcessingStart a free 30-day trial

Automatic Output Stream Clearing on Export

Updated on May 13, 2026
Minimum VersionQ2 2026

As of Q2 2026, the Export() methods of all format providers in the Telerik Document Processing Libraries automatically clear the output stream before writing new content. When the Export() method is called on a seekable stream, the stream is truncated to zero length prior to writing, ensuring that no stale data remains.

Behavioral Change

When reusing a Stream for multiple consecutive export operations, for example, exporting to a MemoryStream or a FileStream opened with FileMode.OpenOrCreate, the previously written content could remain at the tail of the stream if the new document was smaller than the old one. This caused corrupted output files because trailing bytes from the earlier export persisted after the new export completed.

The stream clearing is now performed automatically by the base classes that all format providers inherit from. The affected format providers include:

LibraryFormat Providers
RadPdfProcessingPdfFormatProvider, SkiaImageFormatProvider
RadWordsProcessingDocxFormatProvider, DocFormatProvider, RtfFormatProvider, HtmlFormatProvider, TxtFormatProvider, PdfFormatProvider
RadSpreadProcessingXlsxFormatProvider, XlsFormatProvider, XlsmFormatProvider, CsvFormatProvider, TxtFormatProvider, PdfFormatProvider, JsonFormatProvider

The stream is cleared only when Stream.CanSeek returns true. Non-seekable streams (such as network streams) are not affected and continue to behave as before.

No code changes are required on the consumer side. The clearing happens automatically inside the Export() methods.

Using the Export Methods with Reused Streams

Example 1: Reusing a MemoryStream for multiple exports

C#

RadFlowDocument largeDocument = CreateLargeRadFlowDocument(); 
// CreateLargeRadFlowDocument() is a custom method that creates a large instance of RadFlowDocument.
// You can replace it with the instance you would like to export.

RadFlowDocument smallDocument = CreateRadFlowDocument();
// CreateRadFlowDocument() is a custom method that creates a small instance of RadFlowDocument.
// You can replace it with the instance you would like to export.


Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider provider =
    new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();

using (MemoryStream stream = new MemoryStream())
{
    // First export writes a large document.
    provider.Export(largeDocument, stream, TimeSpan.FromSeconds(10));
    // stream.Length is e.g. 50000 bytes.

    // Second export writes a smaller document to the same stream.
    // The stream is automatically cleared before writing.
    provider.Export(smallDocument, stream, TimeSpan.FromSeconds(10));
    // stream.Length correctly matches only the second document.
}

When reusing a stream, the second Export() call truncates the stream to zero length before writing, so only the new document data is present.

Example 2: Exporting to a FileStream

C#
RadFixedDocument document = new RadFixedDocument();
Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider pdfProvider =
   new Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.PdfFormatProvider();

using (FileStream output = new FileStream("output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    pdfProvider.Export(document, output, TimeSpan.FromSeconds(10));
}

Even when opening the file with FileMode.OpenOrCreate, the stream is cleared automatically so no leftover data from a previous file remains.

If you open a FileStream with FileMode.Create, the file is already truncated by the OS, so the automatic clearing has no additional effect in that scenario.

Backward Compatibility

This is a behavioral change in existing Export() methods. Code that intentionally preserves prior stream content before calling Export() is affected. The stream is now always truncated before writing. If you need to prepend content, write it after the export completes, or use a separate stream and combine them afterward.

For the majority of usage scenarios, where the stream is either freshly created or being reused for a new export, this change is transparent and prevents potential file corruption.

See Also