想通过普通打印机,将条码打印出来。
目前有39码的字库,已经安装到系统去了。
在WORD文档中写了几个字,将字体设置为条码字体,已经打印成功。如何在C#中设置字体打印?

解决方案 »

  1.   

    public class PrintDirect
    {
      [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
      CallingConvention=CallingConvention.StdCall )]
      public static extern long OpenPrinter(string pPrinterName,ref IntPtr phPrinter,
      int pDefault);
      [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
      CallingConvention=CallingConvention.StdCall )]
      public static extern long StartDocPrinter(IntPtr hPrinter, int Level,
      ref DOCINFO pDocInfo);  [ DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
      CallingConvention=CallingConvention.StdCall)]
      public static extern long StartPagePrinter(IntPtr hPrinter);
      [ DllImport( "winspool.drv",CharSet=CharSet.Ansi,ExactSpelling=true,
      CallingConvention=CallingConvention.StdCall)]
      public static extern long WritePrinter(IntPtr hPrinter,string data,
      int buf,ref int pcWritten);  [ DllImport( "winspool.drv" ,CharSet=CharSet.Unicode,ExactSpelling=true,
      CallingConvention=CallingConvention.StdCall)]
      public static extern long EndPagePrinter(IntPtr hPrinter);  [ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
      CallingConvention=CallingConvention.StdCall)]
      public static extern long EndDocPrinter(IntPtr hPrinter);  [ DllImport("winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=true,
      CallingConvention=CallingConvention.StdCall )]
      public static extern long ClosePrinter(IntPtr hPrinter);
    }知道使用上面的API直接输出到打印机去,可字体怎么设置还是不知道。
      

  2.   

    你的数据可以放到word里面再打印阿...
      

  3.   

    是啊,既然word没有问题那把数据放到word中不就可以了么
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Drawing.Printing;namespace test39barcode
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
                private System.Windows.Forms.Button printButton;
                private Font printFont;
                private StreamReader streamToPrint;
                // The Click event is raised when the user clicks the Print button.
                private void printButton_Click(object sender, EventArgs e)
                {
                    try
                    {
                        streamToPrint = new StreamReader
                           ("C:\\MyFile.txt");
                        try
                        {
                            printFont = new Font("3 of 9 Barcode", 30);
                            PrintDocument pd = new PrintDocument();
                            pd.PrintPage += new PrintPageEventHandler
                               (this.pd_PrintPage);
                            pd.Print();
                        }
                        finally
                        {
                            streamToPrint.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }            // The PrintPage event is raised for each page to be printed.
                private void pd_PrintPage(object sender, PrintPageEventArgs ev)
                {
                    float linesPerPage = 0;
                    float yPos = 0;
                    int count = 0;
                    float leftMargin = ev.MarginBounds.Left;
                    float topMargin = ev.MarginBounds.Top;
                    string line = null;                // Calculate the number of lines per page.
                    linesPerPage = ev.MarginBounds.Height /
                       printFont.GetHeight(ev.Graphics);
                    // Print each line of the file.
                    while (count < linesPerPage &&
                       ((line = streamToPrint.ReadLine()) != null))
                    {
                        yPos = topMargin + (count *
                           printFont.GetHeight(ev.Graphics));
                        ev.Graphics.DrawString(line, printFont, Brushes.Black,
                           leftMargin, yPos, new StringFormat());
                        count++;
                    }
                    printFont = new Font("宋体", 30);
                    ev.Graphics.DrawString("三月放风筝", printFont, Brushes.Black,
                       leftMargin + 20, yPos + 20, new StringFormat());                // If more lines exist, print another page.
                    if (line != null)
                        ev.HasMorePages = true;
                    else
                        ev.HasMorePages = false;
                }
            private void button1_Click(object sender, EventArgs e)
            {        }
        }
    }
      

  5.   

    namespace test39barcode
    {
        partial class Form2
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.printButton = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(12, 54);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // printButton
                // 
                this.printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
                this.printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
                this.printButton.Location = new System.Drawing.Point(50, 135);
                this.printButton.Name = "printButton";
                this.printButton.Size = new System.Drawing.Size(136, 40);
                this.printButton.TabIndex = 0;
                this.printButton.Text = "Print the file.";
                this.printButton.Click += new System.EventHandler(this.printButton_Click);
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(504, 381);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.printButton);
                this.Name = "Form2";
                this.Text = "Print Example";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;
        }
    }
      

  6.   

    找了很多资料,都没找到,要不就是使用API函数的。现在问题已经解决了。想要试验的可以将上面文件拷贝回去看看。
    使用39码打印了字母,数字后,再把字体设置回来才能正确打印汉字。发现也有不少人需要这些资料,以此共勉