TextRange.Save 方法

定義

將目前選取範圍以指定的資料格式儲存至指定的資料流。

多載

Save(Stream, String)

將目前選取範圍以指定的資料格式儲存至指定的資料流。

Save(Stream, String, Boolean)

使用保留自訂 TextElement 物件的選項,將目前選取範圍以指定的資料格式儲存至指定的資料流。

Save(Stream, String)

將目前選取範圍以指定的資料格式儲存至指定的資料流。

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)

參數

stream
Stream

要將目前選取範圍儲存到的空的、可寫入資料流。

dataFormat
String

儲存目前選取範圍時要使用的資料格式。 目前支援的資料格式有 RtfTextXamlXamlPackage

例外狀況

streamdataFormatnull

不支援指定的資料格式。

-或-

stream 載入的內容與指定的資料格式不符。

範例

下列範例示範 Save 方法的用法。

// 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

備註

當這個方法傳回時, stream 會保持開啟狀態,且內的 stream 目前位置未定義。

在儲存作業中,目前選取範圍中的內容可能會轉換成 所 dataFormat 指定的資料格式。

另請參閱

適用於

Save(Stream, String, Boolean)

使用保留自訂 TextElement 物件的選項,將目前選取範圍以指定的資料格式儲存至指定的資料流。

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)

參數

stream
Stream

要將目前選取範圍儲存到的空的、可寫入資料流。

dataFormat
String

儲存目前選取範圍時要使用的資料格式。 目前支援的資料格式有 RtfTextXamlXamlPackage

preserveTextElements
Boolean

true 表示保留自訂 TextElement 物件,否則為 false

例外狀況

streamdataFormatnull 時發生。

當指定的資料格式不受支援時發生。 如果從 stream 載入的內容與指定的資料格式不符也會發生。

備註

當 為 falsepreserveTextElements ,自訂 TextElement 物件會儲存為已知 TextElement 型別。 例如,假設您建立名為 Heading1 的自訂 TextElement ,其繼承自 Paragraph 。 當您呼叫這個設定為 false 的方法 preserveTextElements 時, Heading1 會在 儲存 時 TextRange 轉換成 Paragraph 。 當您呼叫這個設定為 true 的方法 preserveTextElements 時, Heading1 會儲存而不轉換。 若要保留自訂文字元素, dataFormat 必須設定為 DataFormats.Xaml

Save(Stream, String, Boolean).NET Framework 3.5 版引進。 如需詳細資訊,請參閱版本和相依性

適用於