List all the available / installed Fonts in the System - c#

I am trying to list all the available Fonts in the system with dropdown list. Mainly, I got to show Arial Bold font in the dropdown list and I have installed the same in my system. But the problem is, My Font Arial Bold is not getting populated in the list of available fonts.
Code
InstalledFontCollection installedFontCollection = new InstalledFontCollection();
var fontFamilies = installedFontCollection.Families;
foreach (FontFamily font in fontFamilies)
{
ddllblFontFamilyHead.Items.Add(font.Name);
}
I am using the above fonts to apply to iTextSharp PDF library to design my pdf content.
Code for applying Fonts to PDF file
var fontHeader = FontFactory.GetFont(_label.SFontName == null ? "Arial" : _label.SFontName, BaseFont.CP1250, true, _label.SFontSize == null ? 10 : _label.SFontSize.Value, 0);
Any help to this issue will be highly appreciated.

tested solution
InstalledFontCollection fontCol = new InstalledFontCollection();
for (int x = 0; x <= fontCol.Families.Length-1;x++ )
{
listBox1.Items.Add(fontCol.Families[x].Name);
}

There's a class named FontFactory in iTextSharp. This class only contains the standard Type 1 fonts by default, but you can register font directories to add more fonts.
For instance:
FontFactory.RegisterDirectories();
The RegisterDirectories() method will look at all the usual directories where your operating system stores fonts. This won't capture all the fonts, but you can add additional directories where you know there are fonts:
FontFactory.RegisterDirectory(myFontFolder);
You can then get all the names of the registered fonts like this:
foreach (String f in FontFactory.RegisteredFonts) {
listBox1.Items.Add(f);
}

Related

c# How to get standard font style object in Word

I'm inserting variable text from a *.html file into a Word document and have to adapt the font(name and size) of the inserted text to the rest of the document.
I have a working solution but I don't like the way I did it, so I'm searching another way to get the standard font name and size from Word application.
Another problem is that NameLocal can be in different languages. So I also need another way to find the Headers. I already tried Style.Type but it has always value "1"
My code so far:
foreach (Word.Style style in Globals.ThisAddIn.Application.ActiveDocument.Styles)
{
if (style.NameLocal.Equals("Normal")) // find correct style object
{
float size = style.Font.Size;
string font = style.Font.Name;
foreach (Word.Paragraph paragraph in Globals.ThisAddIn.Application.ActiveDocument.Paragraphs)
{
if (paragraph.Range.get_Style().NameLocal.Contains("Heading")) // find all headers
{
paragraph.Range.Font.Size = size;
paragraph.Range.Font.Name = font;
}
}
break;
}
}
The reason why I'm not simply changing the style is so the headers are still marked as headers.
I'm pretty clueless atm
For built-in styles, the Word object model provides the enumeration WdBuiltinStyle. Using this instead of a string value (the local name of a style) makes specifying a style language-independent. In addition, the built-in styles will always be present in a document so there's no need to loop the Styles collection of a document to get a particular style.
So, for example:
Word.Document doc = Globals.ThisAddin.Application.ActiveDocument;
Word.Style style = doc.Styles[Word.WdBuildinStyle.wdStyleNormal];
float size = style.Size;
string font = style.Font.Name;
foreach (Word.Paragraph paragraph in doc)
{
if (paragraph.Range.get_Style() = Word.WdBuildinStyle.wdStyleHeading1)
{
paragraph.Range.Font.Size = size;
paragraph.Range.Font.Name = font;
}
}

How can I make default fontstyle be one that the font likes?

I have a small app that get a list of all the windows fonts. I can then choose one and display all its characters. All worked till I then found I had a problem, not all fonts have FontStyle:Regular. No problem I could do a check but this is where I run into a problem, when I start the system seems to have FontStyle:Regular as its default and I cannot change it so if I run the code below with text "Aharoni" selected in my combobox it falls over telling me that regular is not supported. How can I make it ignore the style or force the style to be one it uses like bold?
var cvt = new FontConverter();
Font fname = cvt.ConvertFromString(cmbobx_fontname.Text) as Font;
If I cannot do this then is it possible to get the styles that the choosen font will support?
The FontFamily class has a method to check if a style is available for the font - so rather than try to create a Font directly, first create a FontFamily instance & interate through the styles till you find one that is supported e.g.
FontFamily fontf = new FontFamily("Aharoni");
System.Drawing.FontStyle fs = System.Drawing.FontStyle.Regular;
System.Drawing.Font font;
if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Regular))
fs = System.Drawing.FontStyle.Regular;
else if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Bold))
fs = System.Drawing.FontStyle.Bold;
else if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Italic))
fs = System.Drawing.FontStyle.Italic;
else if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Strikeout))
fs = System.Drawing.FontStyle.Strikeout;
else if (fontf.IsStyleAvailable(System.Drawing.FontStyle.Underline))
fs = System.Drawing.FontStyle.Underline;
else
throw new Exception("No Font Styles Available");
font = new System.Drawing.Font(fontf, 10, fs);
You could iterate through the FontStyle enum - rather than use if/else as I have done.

Font does not support style 'Regular' - using Semibold fonts in C#

I have a TrueType font, which is "Semibold". I try to use that in the following method:
private FontFamily GetFontFamily(string name)
{
PrivateFontCollection pfc = new PrivateFontCollection();
var path = Server.MapPath("~/Static/webfont/" + name + ".ttf");
pfc.AddFontFile(path);
return pfc.Families[0];
}
private Font GetFont(string name, int size,FontStyle style)
{
return new Font(GetFontFamily(name), size, style);
}
Where I provide a name of my font, and it finds the Sentinel-SemiboldItalic.ttf font. As a FontStyle, I have tried to provide any of the options in the .NET framework (regulary, bold, italic, underline and strikeout).
No matter what, I get the following error:
Font 'Sentinel Semibold' does not support style 'Regular'.
What should I do? How to use a semibold font as a font in C#? Also, can I somehow convert my TrueType font to a regular one (if that would fix the issue) ?
As you can read in the answer to this question, each font has its own properties (for example: enabling a Regular Style or not), as provided by the font creator.
By looking at your font (even just by looking at its name), it seems clear that it is a sub-type whose defining characteristics are precisely being (semi-)bold and italic; consequently, it does sound logical to not have the option to remove these features. If you want a non-bold, non-italic version, you should rely on the parent family (Sentinel).
I too faced the same issue like you. And found that even though we are adding the font[.ttf] file, the incorrect font is getting added in privatefontcollection. Hence i found the workaround for this case. This issue will be thrown randomly. So add the font till correct font is added in private font collection.
private FontFamily GetFontFamily(string name)
{
PrivateFontCollection pfc = new PrivateFontCollection();
var path = Server.MapPath("~/Static/webfont/" + name + ".ttf");
pfc.AddFontFile(path);
**while (pfc.FontFamilies != null && pfc.Families.Length > 0 && (pfc.FontFamilies[0] as FontFamily).Name != "YourFontName")
{
pfc.Dispose();
pfc = new PrivateFontCollection();
GetFontFamily();
}
return pfc.Families[0];
}

C#, VS2010 - Changing font and other properties?

Creating a text editor just to try and hone my programming skills some more. I have the winform opening new text files, saving them and the usual undo, redo, copy, paste etc etc. However. I'm now trying to change the font.
When you click the "change font" button in the menu strip, a new form appears and loads all available fonts you can use into a list box.
List<string> fonts = new List<string>();
foreach (FontFamily font in System.Drawing.FontFamily.Families)
{
fonts.Add(font.Name);
}
listboxfont.DataSource = fonts;
Now before I edit the text on the other page, I wanted to edit a sample label to test everything is okay!
After some research, I come across many bits of code like this..
lblsample.Font = new Font(listboxfont.SelectedItem, 12);
I might be wrong, but I see no reason why I can't use the selected item from the list box, which IS the fonts and use that to edit the label however it is giving me the error..
"Text_editor.font does not contain a constructor that takes 2
arguments.
Have tried and tried but no luck. Can anybody help?
It is because listboxfont.SelectedItem is an object. You need to cast it to a string so:
lblsample.Font = new Font((string)listboxfont.SelectedItem, 12);
or if you prefer:
lblsample.Font = new Font(listboxfont.SelectedItem.ToString(), 12);
That should do the trick!
UPDATE - Full example
Add listbox named listboxfont
Add label named lblsample
Add button named btnPreview
private void Form1_Load(object sender, EventArgs e)
{
List<string> fonts = new List<string>();
foreach (FontFamily font in System.Drawing.FontFamily.Families)
{
fonts.Add(font.Name);
}
listboxfont.DataSource = fonts;
}
private void btnPreview_Click(object sender, EventArgs e)
{
lblsample.Font = new Font(listboxfont.SelectedItem.ToString(), 12);
}
You have declared a variable named font in your Text_editor form. (note the casing.. all lowercase!)
OR
The message you typed in your question has a typo. Is it Text_editor.fonts does not contain a constructor that takes 2 arguments.? If yes, then you have used your fonts variable wrongly, which is not of Font type, but instead is of List<string> type.
Name your variables properly, and it should start working.
Try this:
lblsample.Font = new Font(listboxfont.SelectedItem.ToString(), 12.0f);
The Font constructor requests a String and a float
public Font(
string familyName,
float emSize
)

Is it possible to use otf fonts on winforms?

I've tried using Open Type Fonts both for labels, textboxes and when drawing in a paint-event. But it doesn't work. Is there any way to make Open Type Font work?
This isn't possible in Winforms, GDI+ only supports TrueType fonts. You'll have to move to WPF to get OpenType support.
You can use the System.Windows.Media namespace as you would do in WPF, here you have an example:
public static string GetFontList(String ElementSeparator)
{
string r = "";
// WPF incl Adobe and OpenType
foreach (System.Windows.Media.FontFamily fontFam in System.Windows.Media.Fonts.SystemFontFamilies)
{
r += fontFam.Source;
r += ElementSeparator;
}
// True type, incl Type face names e.g. Arial Rounded MT Bold
foreach (System.Drawing.FontFamily f in System.Drawing.FontFamily.Families)
{
if(!r.Contains(f.Name))
{
r += f.Name;
r += ElementSeparator;
}
}
return r;
}

Categories

Resources