自定义字体打印随机字符

时间:2015-08-11 06:25:00

标签: c# c#-3.0

我有以下代码来打印:

class Print
{
    public static void print(string titel, string text, short i, Color color) {
        PrintDocument doc = new PrintDocument();
        doc.PrintController = new StandardPrintController();
        doc.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
        {
            e1.Graphics.DrawString(titel, new Font("Microsoft Sans Serif", 20), new SolidBrush(color), new PointF(30, 40));
            PrivateFontCollection pfc = new PrivateFontCollection();
            Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetExecutingAssembly().GetName().Name + ".Barc.ttf");
            byte[] fontdata = new byte[fontStream.Length];
            fontStream.Read(fontdata, 0, (int)fontStream.Length);
            fontStream.Close();
            unsafe
            {
                fixed (byte * pFontData = fontdata)
                {
                    pfc.AddMemoryFont((System.IntPtr)pFontData, fontdata.Length);
                }
            }
            e1.Graphics.DrawString("*" + text + "*", new Font(pfc.Families[0], 20), new SolidBrush(Color.Black), new PointF(30, 120));
        };
        doc.PrinterSettings.Copies = i;
        try
        {
            fd.BeginInvoke(doc, null, null);
        }
        catch (Exception ex)
        {
            MessageBox.Show("error while printing: " + ex.ToString());
        }
    }
    private delegate void FunctionDelegate(PrintDocument doc);
    private static FunctionDelegate fd = new FunctionDelegate(doprint);

    private static void doprint(PrintDocument doc)
    {
        doc.Print();
    }
}

当我使用(例如)Print.print("A1439213616", "A1439213616", 1, Color.Black)从我的电脑打印某些内容时,它将正常工作,使用条形码字体打印第二行。 但是当我将我的程序复制到另一台电脑上时,它仍然正确地打印了第一行,但第二行打印的是默认字体而不是自定义字体#3) , +1*) +. ) . #。 (到目前为止,我能够发现的唯一模式是*每次都被#取代)这里出了什么问题?我使用的是.NET framework 3.5。

更新:不改变任何东西,现在有50%的时间它突然正确地打印出来,50%的时间它仍然和以前一样。

0 个答案:

没有答案