我正在尝试使用私有字体(私有字体汇总)来更改RichTextBox字体。问题是,该程序在通过路径通往路径时识别字体,但是当我更改RTB字体时,它不起作用,而是使用 calibri 。
代码非常简单:
var RTB = new System.Windows.Forms.RichTextBox();
var privateFont = new System.Drawing.Text.PrivateFontCollection();
privateFont.AddFontFile(@"path.ttf");
RTB.SelectAll();
RTB.SelectionFont = new System.Drawing.Font(privateFont.Families[0], 12);
我正在使用它来格式化RTF文本文件。当我生成文件时,它带有Calibri字体。最让我着迷的是,如果我更改 privatefont.addfontfile(@“ path.ttf”);
例如,在 arial 中,文本是在我想要的字体中返回的,但不是整个文本,文本的末尾以 calibri 返回。
我不知道这里发生了什么。
I'm trying to use a private font (PrivateFontCollection) to change a RichTextBox font. The problem is, the program recognizes the font when I pass the path to it but, when I change the RTB font, it doesn't work and instead it uses Calibri.
The code is very simple:
var RTB = new System.Windows.Forms.RichTextBox();
var privateFont = new System.Drawing.Text.PrivateFontCollection();
privateFont.AddFontFile(@"path.ttf");
RTB.SelectAll();
RTB.SelectionFont = new System.Drawing.Font(privateFont.Families[0], 12);
I'm using it to format a RTF text file. When I generate the file it comes out with the Calibri font. And what intrigues me the most is, if I change the privateFont.AddFontFile(@"path.ttf");
to Arial, for example, the text is returned in the font I wanted, but not the whole text, the end of the text is returned in Calibri.
I don't know what is happening here.
发布评论
评论(1)
要回答您的问题“这里发生了什么”的问题,以便在导出的RTF文件中识别字体,要求将字体安装为系统字体或手动嵌入您所在的文件中的字体或从
RichTextbox
导出。前者似乎不那么笨拙,我知道安装字体的三种方法是:mainform
ctor时的字体安装。下面的代码检查是否存在名为 Smile.ttf 的字体,并调用此Windows System Installer对话框(如果不是):不管是否被安装为系统字体私有字体在内存中,可以显示在
richtextbox
本身中正常工作:但是,在 wordpad 或 word (对两个) dies 取决于字体是否安装在系统中:
从技术上讲, rtf spec 和 openxml (用于MS Word)支持嵌入字体,但我一直在阅读说,如果您不是 在 MS Office 产品。显然,我现在已经脱离了该主题的深处,我制作了一个word doc,嵌入了 smiley.ttf 字体,使用MS Word中的保存选项将其导出到RTF,然后通过<进行了测试。 em>卸载 Windows个性化设置中的字体。即使在检查文档的RTF文本时,我可以清楚地看到嵌入的字体,但在尝试时也无法使用。因此,IMO不建议您嵌入字体,并且会使用上述策略之一将其安装在已部署的设备上。
To answer your question of "what is happening here", for a font to be recognized in an exported RTF file requires that the font either be installed as a system font or manually embedded in the file that you are exporting from the
RichTextBox
. The former seems less dicey and the three ways I know of to make the font be installed are:MainForm
ctor is initially run. The code below checks for the presence of a font named smiley.ttf and invokes this Windows system installer dialog if it's not:TESTBENCH
Regardless of whether or not it gets installed as a system font the private font is in memory and can be shown to work properly in the
RichTextBox
itself:However, the result of exporting and opening the file in Wordpad or Word (tested both) does depend on whether the font is installed in the system or not:
Technically speaking, both the RTF spec and the openxml (for MS Word) support embedded fonts but I keep reading things that say that they tend to get stripped out if you're not opening the RTF file in an MS Office product. Being clearly off the deep end with the topic by now, I made a Word doc, embedded the smiley.ttf font using the Save options in MS Word, exported it to RTF, and then tested it by uninstalling the font in the Windows Personalization settings. Even though I can clearly see the font embed when I inspect the RTF text of the document IT DID NOT WORK when I tried it. So, IMO I wouldn't recommend embedding the font and would use one of the above strategies to install it on the deployed device instead.