Export (0) Print
Expand All

TextRange.Save Method (Stream, String)

Saves the current selection to a specified stream in a specified data format.

Namespace:  System.Windows.Documents
Assembly:  PresentationFramework (in PresentationFramework.dll)

public void Save(
	Stream stream,
	string dataFormat
)

Parameters

stream
Type: System.IO.Stream

An empty, writable stream to save the current selection to.

dataFormat
Type: System.String

A data format to save the current selection as. Currently supported data formats are DataFormats.Rtf, DataFormats.Text, DataFormats.Xaml, and DataFormats.XamlPackage.

ExceptionCondition
ArgumentNullException

stream or dataFormat is null.

ArgumentException

The specified data format is unsupported.

-or

Content loaded from stream does not match the specified data format.

When this method returns, stream is left open, and the current position within stream is undefined.

As part of the save operation, content in the current selection may be converted to the data format specified by dataFormat.

The following example demonstrates the use of the Save method.

// This method accepts an input stream and a corresponding data format.  The method 
// will attempt to load the input stream into a TextRange selection, apply Bold formatting 
// to the selection, save the reformatted selection to an alternat stream, and return  
// the reformatted stream.  
Stream BoldFormatStream(Stream inputStream, string dataFormat)
{
    // A text container to read the stream into.
    FlowDocument workDoc = new FlowDocument();
    TextRange selection = new TextRange(workDoc.ContentStart, workDoc.ContentEnd);
    Stream outputStream = new MemoryStream();

    try
    {
        // Check for a valid data format, and then attempt to load the input stream 
        // into the current selection.  Note that CanLoad ONLY checks whether dataFormat 
        // is a currently supported data format for loading a TextRange.  It does not  
        // verify that the stream actually contains the specified format.  An exception  
        // may be raised when there is a mismatch between the specified data format and  
        // the data in the stream.  
        if (selection.CanLoad(dataFormat))
            selection.Load(inputStream, dataFormat);
    }
    catch (Exception e) { return outputStream; /* Load failure; return a null stream. */ }

    // Apply Bold formatting to the selection, if it is not empty. 
    if (!selection.IsEmpty)
        selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);

    // Save the formatted selection to a stream, and return the stream. 
    if (selection.CanSave(dataFormat))
        selection.Save(outputStream, dataFormat);

    return outputStream;
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft