Reputation: 6500
I want to use iTextSharp to write some text. 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?
Say I have a font called 'mycoolfont' selected by the user in the font chooser dialog. Can I create a new iTextSharp font like this?
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);
but got the error "Verdana" is not recognized by itextsharp
Upvotes: 17
Views: 53212
Reputation: 19136
1st you need to register the font and then just retrieve it from the FontFactory (and don't create it every time):
public static iTextSharp.text.Font GetTahoma()
{
var fontName = "Tahoma";
if (!FontFactory.IsRegistered(fontName))
{
var fontPath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\tahoma.ttf";
FontFactory.Register(fontPath,fontName);
}
return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
Upvotes: 23
Reputation: 71
In case this variation helps someone. This is a variation on VahidN's answer.
In this solution, you can use (at least on our filesystem) the fonts in the fonts folder of Visual Studio. This makes for a more self-contained (and potentially more 'portable') Visual Studio project. 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.
Please note that I need 'BaseFont.EMBEDDED' to be true for my particular Project. 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. There are many overloads available.
Side-note: The fonts folder of the Windows file system shouldn't change. But is it guaranteed to stay in the same place in the next version of 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? 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")'
Use: System.Web.Hosting.HostingEnvironment.MapPath("\")
Also note that 'ttf' font file has been placed in the 'fonts' folder of my Project:
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();
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. (If you want, try (255,0,0) for a red font.)
FYI - Specific to fontawesome
I'm using the font awesome cheatsheet codes from: https://fontawesome.com/v4.7.0/cheatsheet/
Upvotes: 2
Reputation: 13476
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:
I also found that you have to use the font's exact name on the Details tab:
Upvotes: 12
Reputation: 231
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. My PDF showed some random font style when it could not find the font(dint throw error). I copied the required font files (.ttf) to my project bin folder and used following code.
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
Upvotes: 6