views:

234

answers:

1

I have a small WPF application that hosts a RichTextBox:

<RichTextBox SpellCheck.IsEnabled="True" 
             FontFamily="Verdana"
             AcceptsTab="True"
             AcceptsReturn="True"
             FontSize="14" />

As shown the code snippet above, I set the font size to 14. If I copy text from this RichTextBox to Microsoft Word or Microsoft WordPad, the font size is reported to be 10.5. Similar oddities persist if I paste text from Word or WordPad. I have verified that the text being selected from my app is sized at 14. Any help would be greatly appreciated!

+1  A: 

Using FontSize and assigning a simple number means the unit is pixels. Try setting Fontsize="14pt" instead.

Other units: px, in, cm.

Joel B Fant
Excellent catch - wrongly assumed that it was converting the value to the traditional font sizing standard of point. Thanks!
Joel
Interestingly, this gives me a font size of only 13.5pt in Word (the RTF copied to the clipboard contains the value 27 for the font size, instead of 28). What am I missing?
0xA3
@divo: I don't know. There are other questions about RTF and font sizes around here on SO. RTF just seems to do what it wants. It may even have to do with what system dll is being used for the RTF.
Joel B Fant
WPF takes the qualified double (such as "14pt") and converts it a normal WPF pixel by multiplying by a factor. For points, the factor is 1.3333333 (sortof). It turns out that if you make it slightly larger (add 0.0001, multiply by 1.33333334 instead, set the `FontSize` to "14.001pt"), then it crosses a slight boundary and you get correct results when it parses the contents to RTF (which is what happens when you copy to the clipboard).
Joel B Fant
@Joel: Thanks a lot for your explanation, that makes sense. The factor is the conversion factor from pt to pixel for the standard Windows screen resolution of 96dpi (i.e. 96 dpi / 72 points per inch).
0xA3