6

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;
    }      
}
MiguelSlv
  • 14,067
  • 15
  • 102
  • 169
  • 2
    I think this is by design. Googling this finds a wasteland of people trying to solve this without much on answers. Ultimately, I think the font needs to be installed on the machine in order to print it. Otherwise a typical work around is to bitmap it into an image, but the quality will obviously suffer. – LarsTech Mar 24 '14 at 20:34
  • @LarsTech: this link provides a working example: http://msdn.microsoft.com/en-us/library/y505zzfw(v=vs.110).aspx – Sam Axe Mar 24 '14 at 20:47
  • 3
    @Dan-o Not for the PrintPreviewDialog it doesn't, which is where this OP is having his problems. I repro-ed it, and it works on a form, but not in the preview dialog. – LarsTech Mar 24 '14 at 20:51
  • Are you trying to print barcode fonts? You are opening up a big bag of hurt as these things create huge multi-MB spool files and print horribly as the driver tries to dither and anti-alias them. I strongly suggest using graphics primitives to draw barcodes; there are plenty of .NET libraries for doing this. – Dour High Arch Mar 24 '14 at 21:07
  • @MiguelSv: This link may help: http://stackoverflow.com/questions/10946986/unable-to-view-micr-font-in-print-preview – Sam Axe Mar 25 '14 at 08:02
  • @ByteArtisan why not just install the font and then removing it when your done? – Johan J v Rensburg Aug 29 '14 at 09:16
  • @JohanJvRensburg: Just because it should not be needed too. And adds an potential fail point,ex: may require more privileges, have to check is is already there. – MiguelSlv Aug 31 '14 at 15:55

1 Answers1

0

try this

 private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
    Font _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;
}    


and remove
private Font _barCodeFont;