public string ConvertRTF(byte[] bytes)
{
if (bytes == null)
{
throw new ArgumentNullException();
}
FlowDocument document = new FlowDocument();
// open the file for reading
using (MemoryStream stream = new MemoryStream(bytes, true))
{
// create a TextRange around the entire document
TextRange documentTextRange = new TextRange(document.ContentStart, document.ContentEnd);
if (documentTextRange.CanLoad(DataFormats.Rtf))
documentTextRange.Load(stream, DataFormats.Rtf);
}
return XamlWriter.Save(document);
}
[STAThread]
static void Main(string[] args)
{
OpenFileDialog dialog = new OpenFileDialog
{
Filter = "import files (*.rtf)|*.rtf"
};
if (dialog.ShowDialog() != DialogResult.OK)
return;
byte[] data;
using (Stream filestream = dialog.OpenFile())
{
int offset = 0;
data = new byte[filestream.Length];
int remaining = data.Length;
while (remaining > 0)
{
int read = filestream.Read(data, offset, remaining);
if (read <= 0)
throw new EndOfStreamException
(String.Format("End of stream reached with {0} bytes left to read", remaining));
remaining -= read;
offset += read;
}
}
FlowDocument document = new FlowDocument();
using (MemoryStream stream = new MemoryStream(data))
{
// create a TextRange around the entire document
TextRange documentTextRange = new TextRange(document.ContentStart, document.ContentEnd);
documentTextRange.Load(stream, DataFormats.Rtf);
}
Console.WriteLine("test ok");
}