在WPF中加载/保存RTF时保留字体大小 [英] Retaining font size when loading/saving RTF in WPF

查看:84
本文介绍了在WPF中加载/保存RTF时保留字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下RTF文档

{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fswiss\fprq2\fcharset0 Segoe UI;}{\f1\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1507;}\viewkind4\uc1\pard\f0\fs22 Sample Text\f1\fs20\par
}

它包含Segoe UI 11 pt字体的示例文本".现在,当我分别使用WPF的FlowDocument和TextRange.Load()和.Save()加载然后保存文档时,字体大小减小到10.5pt.使用RTF作为输入/输出时,有什么方法可以保留原始字体大小?

It contains "Sample Text" in font Segoe UI 11 pt. Now when I load and then save the document using WPF's FlowDocument and TextRange.Load() and .Save(), respectively, the font size is reduced to 10.5pt. Is there any way to retain the original font size when using RTF as input/output?

推荐答案

WPF的所有度量单位均为像素(尽管不完全是屏幕像素).即使您指定了 TextRange FontSize .

All of WPF's measurement is in pixels (although not exactly screen pixels). Even when you specify the FontSize of a TextRange.

在内部,当您在XAML中指定类似 FontSize ="14pt" 之类的内容时,WPF使用 LengthConverter 并根据与度量单位相关的因数更改合格的double你给.因此11大约乘以1.3333333.因此,如果您将纯双精度值输入 FontSize 属性,则单位为像素.

Internally, when you specify something like FontSize="14pt" in XAML, WPF uses LengthConverter and changes that qualified double based on a factor associated with the unit of measurement you give. So 11 gets multiplied by 1.3333333, approximately. So if you feed a plain double value to a FontSize property, the unit is pixels.

但是,如果您使用 FontSize ="14.0001pt" ,或将点数乘以1.3333334,或者甚至只是将0.0001添加到像素测量中,它的偏移量就足以使您实际得到\fs22或\ fs28(而不是分别为\ fs21或\ fs27).这是在WPF中设置尺寸时使用的.

However, if you use FontSize="14.0001pt", or multiply the points by 1.3333334, or maybe even just add 0.0001 to the pixel measurement, it offsets things just enough that you actually get \fs22 or \fs28 (rather than \fs21 or \fs27, respectively). This is for when you're setting a size in WPF.

您拥有\ fs22,Load(),Save(),然后拥有\ fs21的原因与同一件事有关.解析器将RTF转换为WPF对象.因此22个半点变成了14.666666666667像素.当您再次保存()时,这些像素将转换回不同的单位,但不是很正确.14.666666666667像素变成21个半点,但是14.6666674像素变成22个半点,这就是您想要的.

The reason you have \fs22, Load(), Save(), and then have \fs21 has to do with the same thing. A parser takes the RTF and converts it into WPF objects. So 22 half-points become something like 14.666666666667 pixels. When you Save() again, those pixels get converted back to a different unit, but not very correctly. 14.666666666667 pixels become 21 half points, but 14.6666674 pixels become 22 half points, which is what you wanted.

也许此信息将使您了解如何以不同的方式获取RTF.也许您可以改用XAML并将其转换.也许有一个不错的免费的第三方XAML到RTF转换器,它没有令人讨厌的舍入错误.

Maybe this information will give you an idea how to get RTF differently. Maybe you can get XAML out instead and convert it. Maybe there's a nice free third-part XAML-to-RTF converter that doesn't have annoying rounding errors.

这篇关于在WPF中加载/保存RTF时保留字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆