New to Telerik Document ProcessingStart a free 30-day trial

PdfStreamWriter

Updated on Jul 15, 2026

The PdfStreamWriter class enables you to write file content directly to a stream. This is the root element of the streaming mechanism used when exporting a PDF document.

Create a PdfStreamWriter Instance

To create a PdfStreamWriter instance, pass the stream of the file you want to work with as a constructor parameter.

Instantiate PdfStreamWriter

C#
using (PdfStreamWriter writer = new PdfStreamWriter(File.OpenWrite(resultDocument)))
{
    //...
}

PdfStreamWriter also exposes an additional overload that allows you to keep the stream open after disposing the writer instance. Pass true as the value for the second constructor parameter (leaveStreamOpen).

PdfStreamWriter inherits from IDisposable. Ensure the object is disposed when you are done with it. Otherwise, the content might not be written in the exported file. The best way to handle this is to wrap it in a using statement.

Using PdfStreamWriter with MemoryStream

The constructor of PdfStreamWriter accepts any class inheriting from Stream. In many cases, you might not need to save a file but instead preserve it in memory to directly send it to a client, for example. For this scenario, use a MemoryStream to preserve the document data.

Two important points when you work with MemoryStream:

  • Set the second parameter of the PdfStreamWriter constructor to true so you can use the stream after you are done with PdfStreamWriter.
  • All the data is flushed into the stream when disposing PdfStreamWriter. Dispose the object before further processing the MemoryStream to ensure all the required document data is saved.

Instantiate PdfStreamWriter with MemoryStream

C#
MemoryStream stream = new MemoryStream();
using (PdfStreamWriter writer = new PdfStreamWriter(stream, true))
{
    //...
}

PdfStreamWriter Members

The members of the class allow you to set several properties of the document and generate and write new pages.

  • BeginPage(): Returns an instance of the PdfPageStreamWriter class, which draws the content of the page. For more information, see the PdfPageStreamWriter article. The overloads of BeginPage() allow you to pass the size and the Rotation of the page.

    Insert a new page into a document

    C#
    using (PdfStreamWriter writer = new PdfStreamWriter(File.OpenWrite(resultDocument)))
    {
        Size size = new Size(700, 1200);
        Rotation rotation = Rotation.Rotate270;
    
        using (PdfPageStreamWriter pageWriter = writer.BeginPage(size, rotation))
        {
            //Use the pageWriter object to fill the content of the page.
        }
    }
  • WritePage(): Enables you to pass an already constructed page object. With the different overloads, you can pass an instance of RadFixedPage or PdfPageStreamWriter.

    Insert an already generated page into a document

    C#
    using (PdfStreamWriter writer = new PdfStreamWriter(File.OpenWrite(resultDocument)))
    {
        RadFixedPage page = new RadFixedPage();
        writer.WritePage(page);
    }

Settings of PdfStreamWriter

Through the Settings property of PdfStreamWriter, you can control how the document is exported. The following table describes the available properties:

PropertyDescription
DocumentInfoA property of type RadFixedDocumentInfo, intended to hold additional information about the document. The RadFixedDocumentInfo class allows you to set the title, author, and description of the document.
ImageQualityGets or sets the default image quality when exporting images to PDF. This property is of type ImageQuality and defaults to High. The value of this property is overridden when you specify ImageQuality in the ImageSource constructor or when you create an ImageSource from EncodedImageData. Higher quality results in a larger document size.
ImageCompressionGets or sets the image compression type. The possible values are:
* None: No compression is applied.
* Default: The image compression is preserved as it is in the original document.
* FlateDecode: The images are encoded with a FlateDecode filter.
* DCTDecode: Compresses data using a DCT (discrete cosine transform) technique based on the JPEG standard.
WriteAnnotationsA Boolean property indicating whether the annotations are included in the exported document.
StreamCompressionGets or sets the content stream compression type. Possible values are:
* None: The content streams are not encoded.
* FlateDecode: Compresses data using the zlib/deflate compression method.

When merging document pages with PdfStreamWriter, the form fields may be duplicated. Starting with Q2 2025, the PdfStreamWriterSettings class offers the MergedFieldNameResolving event. This event occurs when resolving conflicts between field names while merging instances with duplicated names:

C#
FileStream stream = new FileStream(@"file_1.pdf", FileMode.Create, FileAccess.Write);
using (PdfStreamWriter fileWriter = new PdfStreamWriter(stream))
{
    fileWriter.Settings.MergedFieldNameResolving += (s, e) =>
    {
        e.NewName = "new name here";
    };
    // Iterate through the files you would like to merge 
    for (int i = 0; i < 2; i++)
    {
        // Open each of the files 
        using (PdfFileSource fileToMerge = new PdfFileSource(File.OpenRead(@"file_2.pdf")))
        {
            // Iterate through the pages of the current document 
            foreach (PdfPageSource pageToMerge in fileToMerge.Pages)
            {
                // Append the current page to the fileWriter, which holds the stream of the result file 
                fileWriter.WritePage(pageToMerge);
            }
        }
    }
}

The XAML SDK repository on GitHub contains examples showing the capabilities of PdfStreamWriter:

See Also