RTF 到 HTML 通过 RichTextBox,如何设置正确的字体大小?
RTF to HTML via RichTextBox, how to set correct font-sizes?
我有一个 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 将其四舍五入并放置 pt 文本?
如果您能提出一种更好的将 RTF 格式转换为 HTML 的方法,我将不胜感激。
我通过搜索 html 中的字体大小并将其替换为正则表达式解决了这个问题:
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;
}
我有一个 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 将其四舍五入并放置 pt 文本?
如果您能提出一种更好的将 RTF 格式转换为 HTML 的方法,我将不胜感激。
我通过搜索 html 中的字体大小并将其替换为正则表达式解决了这个问题:
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;
}