Επεξεργασία

Κοινή χρήση μέσω


TextRange.Save Method

Definition

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

Overloads

Save(Stream, String)

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

Save(Stream, String, Boolean)

Saves the current selection to a specified stream in a specified data format, with the option of preserving custom TextElement objects.

Save(Stream, String)

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

public:
 void Save(System::IO::Stream ^ stream, System::String ^ dataFormat);
public void Save (System.IO.Stream stream, string dataFormat);
member this.Save : System.IO.Stream * string -> unit
Public Sub Save (stream As Stream, dataFormat As String)

Parameters

stream
Stream

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

dataFormat
String

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

Exceptions

stream or dataFormat is null.

The specified data format is unsupported.

-or

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

Examples

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;
}
' 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.  
Private Function BoldFormatStream(ByVal inputStream As Stream, ByVal dataFormat As String) As Stream
    ' A text container to read the stream into.
    Dim workDoc As New FlowDocument()
    Dim selection As New TextRange(workDoc.ContentStart, workDoc.ContentEnd)
    Dim outputStream As Stream = 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) Then
            selection.Load(inputStream, dataFormat)
        End If
    Catch e As Exception ' Load failure return a null stream. 
        Return outputStream
    End Try

    ' Apply Bold formatting to the selection, if it is not empty.
    If Not selection.IsEmpty Then
        selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold)
    End If

    ' Save the formatted selection to a stream, and return the stream.
    If selection.CanSave(dataFormat) Then
        selection.Save(outputStream, dataFormat)
    End If

    Return outputStream
End Function

Remarks

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.

See also

Applies to

Save(Stream, String, Boolean)

Saves the current selection to a specified stream in a specified data format, with the option of preserving custom TextElement objects.

public:
 void Save(System::IO::Stream ^ stream, System::String ^ dataFormat, bool preserveTextElements);
public void Save (System.IO.Stream stream, string dataFormat, bool preserveTextElements);
member this.Save : System.IO.Stream * string * bool -> unit
Public Sub Save (stream As Stream, dataFormat As String, preserveTextElements As Boolean)

Parameters

stream
Stream

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

dataFormat
String

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

preserveTextElements
Boolean

true to preserve custom TextElement objects; otherwise, false.

Exceptions

Occurs when stream or dataFormat is null.

Occurs when the specified data format is unsupported. May also be raised if content loaded from stream does not match the specified data format.

Remarks

When preserveTextElements is false, custom TextElement objects are saved as known TextElement types. For example, suppose you create a custom TextElement called Heading1, which inherits from Paragraph. When you call this method with preserveTextElements set to false, Heading1 is converted to a Paragraph when the TextRange is saved. When you call this method with preserveTextElements set to true, Heading1 is saved without being converted. To preserve custom text elements, dataFormat must be set to DataFormats.Xaml.

Save(Stream, String, Boolean) is introduced in the .NET Framework version 3.5. For more information, see Versions and Dependencies.

Applies to