c#中的FontFamily问题

Posted

技术标签:

【中文标题】c#中的FontFamily问题【英文标题】:FontFamily issue in c# 【发布时间】:2015-04-10 01:29:59 【问题描述】:

我正在为我的网页使用 CSS 中的 Google 字体“Open Sans,sans-serif”。​​我正在使用 evo pdf 生成 PDF。在此使用 TextElement 动态创建页眉和页脚。

我的问题是在 c# fontfamily 中没有这样的 Google 字体。如何在 c# fontfamily 中获得“Open Sans,sans-serif”?

TextElement footerText = new TextElement(0, 15, "Page &p; of &P;  ",
                new System.Drawing.Font(this.OpenSans,12, FontStyle.Regular, GraphicsUnit.Pixel));
footerText.TextAlign = HorizontalTextAlign.Right;
footerText.ForeColor = Color.Black;
footerText.EmbedSysFont = true;
htmlToPdfConverter.PdfFooterOptions.AddElement(footerText);

【问题讨论】:

【参考方案1】:

您实际上可以使用本地系统上未安装的字体。您只需要 .ttf 或 .otf 文件并从文件中加载字体,如下所示:

// Embed a True Type font from a local file in PDF document
PdfFont localTrueTypeFont = pdfDocument.AddFont(@"DemoAppFiles\Input\Fonts\TrueType.ttf");

// Add a text element using the local True Type font to PDF document
TextElement localFontTtfTextElement = new TextElement(xLocation, yLocation, "This text uses a True Type Font loaded from a local file", localTrueTypeFont);
addTextResult = pdfPage.AddElement(localFontTtfTextElement);

您还可以通过more code samples for text and fonts handling 观看整个演示。您必须在该页面中选择“示例代码”选项卡。

【讨论】:

【参考方案2】:
TextElement footerText = new TextElement(0, pdfConverter.PdfFooterOptions.FooterHeight - 15, 
            "This is page &p; of &P;  ",
            new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 

这不是你要找的吗:evo guide to using fonts

【讨论】:

James 特别想要一种 Google 字体,而不是标准的 Times New Roman。

以上是关于c#中的FontFamily问题的主要内容,如果未能解决你的问题,请参考以下文章

将 FontFamily 应用于 Silverlight 4 Beta 中的所有控件

如何在 Xamarin.Form 应用程序中使用 text 属性为项目中的所有控件全局设置 FontFamily?

当我在颤动中按下 ElevatedButton 时,我希望能够更改 Fontfamily

Android:在 TextAppearance 中设置 fontFamily 不起作用

Shoutem 更改默认 fontFamily 不起作用

获取数据网格列值 - WPF C#