简体   繁体   English

使用iTextSharp在系统中使用字体

[英]Using Fonts in System with iTextSharp

I want to use iTextSharp to write some text. 我想用iTextSharp写一些文字。 I'm using this method: 我正在使用这种方法:

var font = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);

My question is: does iTextSharp support all fonts in the system fonts directory? 我的问题是:iTextSharp支持系统字体目录中的所有字体吗?

Say I have a font called 'mycoolfont' selected by the user in the font chooser dialog. 假设我在字体选择器对话框中选择了一个名为“mycoolfont”的字体。 Can I create a new iTextSharp font like this? 我可以创建一个新的iTextSharp字体吗?

var font = BaseFont.CreateFont("mycoolfont", BaseFont.WINANSI, BaseFont.EMBEDDED);
overContent.SetFontAndSize(font, fontSize);

UPDATE: 更新:

I tried var font = BaseFont.CreateFont("Verdana", BaseFont.WINANSI, BaseFont.EMBEDDED); 我试过var font = BaseFont.CreateFont("Verdana", BaseFont.WINANSI, BaseFont.EMBEDDED); but got the error "Verdana" is not recognized by itextsharp 但得到错误“Verdana”不被itextsharp识别

1st you need to register the font and then just retrieve it from the FontFactory (and don't create it every time): 首先,您需要注册字体,然后从FontFactory中检索它(并且不要每次都创建它):

public static iTextSharp.text.Font GetTahoma()
{
    var fontName = "Tahoma";
    if (!FontFactory.IsRegistered(fontName))
    {
         var fontPath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\tahoma.ttf";
         FontFactory.Register(fontPath);
    }
    return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
}

I ended up combining the 2 answers here into this method: 我最后将这两个答案合并到这个方法中:

public static Font GetFont(string fontName, string filename)
{
    if (!FontFactory.IsRegistered(fontName))
    {
        var fontPath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\" + filename;
        FontFactory.Register(fontPath);
    }
    return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}

Which I then use in my code like so: 然后我在我的代码中使用它,如下所示:

writer.DirectContent.SetFontAndSize(GetFont("Franklin Gothic Medium Cond", "FRAMDCN.TTF").BaseFont, 24f);

On Windows you can find out the font's file name from the font's property sheet: 在Windows上,您可以从字体的属性表中找到字体的文件名:

在此输入图像描述

I also found that you have to use the font's exact name on the Details tab: 我还发现你必须在Details选项卡上使用字体的确切名称:

在此输入图像描述

Im posting this since someone else might find this useful. 我发布这个,因为其他人可能会觉得这很有用。 I had a similar problem when i ran my code on server. 当我在服务器上运行代码时,我遇到了类似的问题。 The reason being itextsharp could not find the font style in OS. 原因是itextsharp无法在OS中找到字体样式。 My PDF showed some random font style when it could not find the font(dint throw error). 我的PDF在找不到字体时显示了一些随机的字体样式(dint throw error)。 I copied the required font files (.ttf) to my project bin folder and used following code. 我将所需的字体文件(.ttf)复制到我的项目bin文件夹并使用以下代码。

public static BaseFont GetFont(string fontName)
{
    return BaseFont.CreateFont(HttpContext.Current.Server.MapPath("~/Bin/" +   fontName + ".ttf"), BaseFont.CP1252, BaseFont.EMBEDDED);
}

Here i get the desired font 在这里,我得到了所需的字体

`BaseFont sm = GetFont("comic"); //The fontName here should exactly match` the` file name in bin folder

In case this variation helps someone. 如果这种变化有助于某人。 This is a variation on VahidN's answer. 这是VahidN答案的变体。

In this solution, you can use (at least on our filesystem) the fonts in the fonts folder of Visual Studio. 在此解决方案中,您可以使用(至少在我们的文件系统上)Visual Studio的fonts文件夹中的字体。 This makes for a more self-contained (and potentially more 'portable') Visual Studio project. 这使得Visual Studio项目更加独立(可能更“便携”)。 So rather than trying to find the fonts folder on your Windows operating system... which may change the next time Windows develops a new operating system, the fonts are all in your Project. 因此,不要试图在Windows操作系统上找到fonts文件夹...这可能会在Windows下一次开发新操作系统时发生变化,这些字体都在您的项目中。

Please note that I need 'BaseFont.EMBEDDED' to be true for my particular Project. 请注意,我需要'BaseFont.EMBEDDED'才能适用于我的特定项目。 This is not generally needed for all Projects (see other answers on this page). 所有项目通常不需要这样做(请参阅本页的其他答案)。 Please vary the arguments of 'FontFactory.GetFont()' as needed for your Project. 请根据项目的需要更改“FontFactory.GetFont()”的参数。 There are many overloads available. 有许多重载可用。

Side-note: The fonts folder of the Windows file system shouldn't change. 附注:Windows文件系统的fonts文件夹不应更改。 But is it guaranteed to stay in the same place in the next version of Windows? 但它是否保证在下一版Windows中保持相同的位置? Plus if you load your Project onto a new computer because your hard drive crashed, and you are using a Project backup... is the font you need already in the fonts folder of your Windows file system? 此外,如果您将项目加载到新计算机上,因为您的硬盘驱动器崩溃了,并且您正在使用项目备份...是您在Windows文件系统的fonts文件夹中已经需要的字体? If so, great. 如果是这样,很好。 If not, you've got to go get it. 如果没有,你必须得到它。 No big deal usually. 通常没什么大不了的。 But, if you had tied the font permanently to your Project via this solution, it's one less hassle. 但是,如果你通过这个解决方案将字体永久地绑定到你的项目,那就不那么麻烦了。

The main change to the code is: 代码的主要变化是:

Instead of 'Environment.GetEnvironmentVariable("SystemRoot")' 而不是'Environment.GetEnvironmentVariable(“SystemRoot”)'

Use: System.Web.Hosting.HostingEnvironment.MapPath("\\") 使用:System.Web.Hosting.HostingEnvironment.MapPath(“\\”)

Also note that 'ttf' font file has been placed in the 'fonts' folder of my Project: 另请注意,'ttf'字体文件已放置在我的项目的'fonts'文件夹中: Visual Studio Project中fonts字体目录中的字体文件

public static iTextSharp.text.Font GetFontAwesome()
{
string fontName = "fontawesome";
if (!FontFactory.IsRegistered(fontName))
{
var fontPath = System.Web.Hosting.HostingEnvironment.MapPath("\\") + "fonts\\fontawesome-webfont.ttf";
FontFactory.Register(fontPath, fontName);
}

var FontColour = new BaseColor(0, 0, 0); // optional... ints 0, 0, 0 are red, green, blue
int FontStyle = iTextSharp.text.Font.NORMAL;  // optional
float FontSize = iTextSharp.text.Font.DEFAULTSIZE;  // optional

return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, FontSize, FontStyle, FontColour );
 // last 3 arguments can be removed
}

Then just call the function... 然后只需调用函数...

var fontawesomeFont = GetFontAwesome(); var fontawesomeFont = GetFontAwesome();

The use the function: 使用功能:

    Paragraph paragraph = new Paragraph();
    paragraph.Add(new Chunk("Hello world! "));
    paragraph.Add(new Chunk("\xf118", fontawesomeFont));
    paragraph.Add(new Chunk("\xf14a", fontawesomeFont));
    paragraph.Add(new Chunk("\xf0c8", fontawesomeFont));
    paragraph.Add(new Chunk("\xf046", fontawesomeFont)); 
    paragraph.Add(new Chunk("\xf096", fontawesomeFont)); 

    document.Add(paragraph);

This variation allows you to also change the color however you wish via 'new BaseColor(0, 0, 0)' above. 此变体允许您通过上面的“新BaseColor(0,0,0)”更改颜色。 (If you want, try (255,0,0) for a red font.) (如果需要,请尝试(255,0,0)获取红色字体。)

FYI - Specific to fontawesome 仅供参考 - 具体到fontawesome

I'm using the font awesome cheatsheet codes from: https://fontawesome.com/v4.7.0/cheatsheet/ 我正在使用以下字体令人敬畏的备忘单代码: https//fontawesome.com/v4.7.0/cheatsheet/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM