我正在尝试使用第三方库(一个演示,因此我无法获得支持),它提供了将HTML转换为RTf等的类,问题是我正在使用的方法需要{ {1}}存储RTF转换,但我无法正确设置,我的流仍为空。
我认为我遗漏了一些关于Stream的使用情况,因为我从不需要使用Stream,所以我真的不知道我是否正确使用了Stream对象。
我做错了什么?
System.Io.Stream
答案 0 :(得分:3)
您正在将所有内容推送到空流(Stream.Null
);它的作用是扔掉所有东西。那是所有它。
请尝试使用新的MemoryStream
,同时确保在放入数据之后以及在阅读之前将其回放(将Position
设置为0
)。
来自Stream.Null
上的MSDN:
使用
Null
将输出重定向到不会消耗任何操作系统资源的流。当在Stream
上调用提供写入的Null
方法时,调用只会返回,并且不会写入任何数据。Null
还实现了Read
方法,该方法在不读取数据的情况下返回零。
答案 1 :(得分:0)
由于@Marc Gravell解决方案,我想分享一个我写过的帮助类。
#Region " Imports "
Imports Nasosoft.Documents.Transform
Imports System.IO
Imports System.Text
#End Region
''' <summary>
''' Performs document conversion operations.
''' </summary>
Public Class DocumentConverter
#Region " Public Methods "
''' <summary>
''' Converts RTF text to HTML.
''' </summary>
''' <param name="RtfText">Indicates the RTF text.</param>
''' <returns>System.String.</returns>
Public Shared Function Rtf2Html(ByVal RtfText As String,
Optional ByVal TextEncoding As Encoding = Nothing) As String
TextEncoding = If(TextEncoding Is Nothing, Encoding.Default, TextEncoding)
Dim RtfStream As New MemoryStream(TextEncoding.GetBytes(RtfText))
Dim HtmlStream As New MemoryStream
Dim HtmlText As String = String.Empty
Using Converter As New RtfToHtmlTransform()
Converter.Load(RtfStream)
Converter.Transform(HtmlStream)
End Using
HtmlStream.Position = 0
Using StrReader As New StreamReader(HtmlStream)
HtmlText = StrReader.ReadToEnd
End Using
RtfStream.Close()
HtmlStream.Close()
Return HtmlText
End Function
''' <summary>
''' Converts RTF text to TXT (Plain text).
''' </summary>
''' <param name="RtfText">Indicates the RTF text.</param>
''' <returns>System.String.</returns>
Public Shared Function Rtf2Txt(ByVal RtfText As String,
Optional ByVal TextEncoding As Encoding = Nothing) As String
TextEncoding = If(TextEncoding Is Nothing, Encoding.Default, TextEncoding)
Dim RtfStream As New MemoryStream(TextEncoding.GetBytes(RtfText))
Dim TextStream As New MemoryStream
Dim PlainText As String = String.Empty
Using Converter As New RtfToTextTransform()
Converter.Load(RtfStream)
Converter.Transform(TextStream)
End Using
TextStream.Position = 0
Using StrReader As New StreamReader(TextStream)
PlainText = StrReader.ReadToEnd
End Using
RtfStream.Close()
TextStream.Close()
Return PlainText
End Function
''' <summary>
''' Converts HTML text to RTF.
''' </summary>
''' <param name="HtmlText">Indicates the HTML text.</param>
''' <returns>System.String.</returns>
Public Shared Function Html2Rtf(ByVal HtmlText As String,
Optional ByVal TextEncoding As Encoding = Nothing) As String
TextEncoding = If(TextEncoding Is Nothing, Encoding.Default, TextEncoding)
Dim HtmlStream As New MemoryStream(TextEncoding.GetBytes(HtmlText))
Dim RtfStream As New MemoryStream
Dim RtfText As String = String.Empty
Using Converter As New HtmlToRtfTransform()
Converter.Load(HtmlStream)
Converter.Transform(RtfStream)
End Using
RtfStream.Position = 0
Using StrReader As New StreamReader(RtfStream)
RtfText = StrReader.ReadToEnd
End Using
HtmlStream.Close()
RtfStream.Close()
Return RtfText
End Function
''' <summary>
''' Converts HTML text to TXT (Plain text).
''' </summary>
''' <param name="HtmlText">Indicates the HTML text.</param>
''' <returns>System.String.</returns>
Public Shared Function HtmlTxt(ByVal HtmlText As String,
Optional ByVal TextEncoding As Encoding = Nothing) As String
TextEncoding = If(TextEncoding Is Nothing, Encoding.Default, TextEncoding)
Dim HtmlStream As New MemoryStream(TextEncoding.GetBytes(HtmlText))
Dim TextStream As New MemoryStream
Dim PlainText As String = String.Empty
Using Converter As New HtmlToTextTransform()
Converter.Load(HtmlStream)
Converter.Transform(TextStream)
End Using
TextStream.Position = 0
Using StrReader As New StreamReader(TextStream)
PlainText = StrReader.ReadToEnd
End Using
HtmlStream.Close()
TextStream.Close()
Return PlainText
End Function
#End Region
End Class