我有一个RTF
格式文本,通过调用Editor.Document.GetText(TextGetOptions.FormatRtf, out string rtf);
从UWP RichEditBox
接收。我需要将其转换为html,但我找到的最佳解决方案是MarkupConverter
。无论如何,这使用一个WPF RichTextBox
,它加载RTF格式的文本,然后作为XAML从那里获取,然后将其转换为HTML。
问题是,如果我设置一个更大的字体大小,在RTF中显示为\fs44
,并且当它转换为XAML时,它将显示如下方式:FontSize="34.666666666666664"
。我希望看到FontSize="34pt"
(或35,不重要)。
我understand为什么会发生这种情况,但是有什么方法可以让RichTextBox绕过它并把它放出来吗?
如果你能提出更好的方法把RTF转换成HTML,我也会很感激。
发布于 2021-02-03 13:50:48
我通过搜索html中的字体大小并将其替换为regexp来解决这个问题:
public static string ConvertFloatingFontSize(string html) {
var matches = Regex.Matches(html, @"font-size:([0-9]+.?[0-9]*);");
if (matches.Count > 0) {
foreach (Match match in matches) {
var fontSize = match.Value;
if (html.IndexOf(fontSize) >= 0 && match.Groups.Count > 1) {
double.TryParse(match.Groups[1].Value, out double result);
if (result > 0) {
int sizeAsInt = (int)result;
html = html.Replace(fontSize, $"font-size:{sizeAsInt}px;");
}
}
}
}
return html;
}
https://stackoverflow.com/questions/65953078
复制相似问题