I'm trying to load a private font, using System.Drawing.Text.PrivateFontCollection. The goal is not to have to install the font on the system.
All the examples i find it look pretty simple. Just load using PrivateFontCollection and then create a font from it.
Below my simple class to test it.
It works only if i install the font. In not, the text is printed in the dialog preview as using some default font. I checked that the font is correctly loaded. What i'm missing ? Thank for any help.
public partial class Test : Form
{
private PrintDocument printDocument1 = new PrintDocument();
System.Drawing.Text.PrivateFontCollection privateFonts;
private Font _barCodeFont;
public Test()
{
InitializeComponent();
}
private void Test_Load(object sender, EventArgs e)
{
privateFonts = new System.Drawing.Text.PrivateFontCollection();
privateFonts.AddFontFile("Code128.ttf");
}
private void btbTest_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.DocumentName = "Label";
PrintPreviewDialog pp = new PrintPreviewDialog();
pp.Document = pd;
pp.WindowState = FormWindowState.Normal;
pp.ShowDialog();
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
_barCodeFont = new Font(privateFonts.Families[0], 12, FontStyle.Regular);
ev.Graphics.DrawString("Should Be a bar code", _barCodeFont, Brushes.Black, 0, 0);
ev.HasMorePages = false;
}
}