gpt4 book ai didi

c# - 将 RTF 字符串转换为 XAML 字符串

转载 作者:太空狗 更新时间:2023-10-29 22:58:40 24 4
gpt4 key购买 nike

在 C# 中将 RTF 字符串转换为 XAML 字符串的最有效方法是什么?我想使用 System.Windows.Documents.XamlRtfConverter.ConvertRtfToXaml(string rtfContent) 但遗憾的是该类是内部类。

最佳答案

您可以从 RTF 字符串转换为 XAML 字符串,但会丢失图像:

 var rtf = File.ReadAllText(rtfFileName);
var doc = new FlowDocument();
var range = new TextRange(doc.ContentStart, doc.ContentEnd);
using (var inputStream = new MemoryStream(Encoding.ASCII.GetBytes(rtf)))
{
range.Load(inputStream, DataFormats.Rtf);
using (var outputStream = new MemoryStream())
{
range.Save(outputStream, DataFormats.Xaml);
outputStream.Position = 0;
using (var xamlStream = new StreamReader(outputStream))
{
var xaml = xamlStream.ReadToEnd();
File.WriteAllText(xamlFileName, xaml);
}
}
}

要保存图像,您必须从 RTF 字符串转到 XAML 包:

 var rtf = File.ReadAllText(rtfFileName);
var doc = new FlowDocument();
var range = new TextRange(doc.ContentStart, doc.ContentEnd);
using (var inputStream = new MemoryStream(Encoding.ASCII.GetBytes(rtf)))
{
range.Load(inputStream, DataFormats.Rtf);
using (var outputStream = new FileStream(xamlFileName, FileMode.Create))
{
range.Save(outputStream, DataFormats.XamlPackage);
}
}

关于c# - 将 RTF 字符串转换为 XAML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26123907/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com