热敏票据打印机 型号(GP-5890X)
为什么打印出的小票,都不从最左边开始打印,而是从中间向右打印呢?
我听说需要调用打印机函数?应该怎么写呢?
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
public class PrintingExample : System.Windows.Forms.Form 
{
     private System.ComponentModel.Container components;
     private System.Windows.Forms.Button printButton;
     private Font printFont;     int i;
     TextBox textBox1;    public PrintingExample() : base() 
    {
       // The Windows Forms Designer requires the following call.
       InitializeComponent();
    }    // The Click event is raised when the user clicks the Print button.
    private void printButton_Click(object sender, EventArgs e) 
    {
              i = 0;
              printFont = new Font("Arial", 10);
              PrintDocument pd = new PrintDocument();
              pd.PrintPage += new PrintPageEventHandler
                 (this.pd_PrintPage);
              pd.Print();
    }    // 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 textBox1
       while(count < linesPerPage && i < textBox1.Lines.Length)
       {
          line = textBox1.Lines[i++];
          yPos = topMargin + (count * 
             printFont.GetHeight(ev.Graphics));
          ev.Graphics.DrawString(line, printFont, Brushes.Black, 
             leftMargin, yPos, new StringFormat());
          count++;
       }       // If more lines exist, print another page.
       if(line != null)
          ev.HasMorePages = true;
       else
          ev.HasMorePages = false;
    }    // The Windows Forms Designer requires the following procedure.
    private void InitializeComponent() 
    {
       textBox1           = new TextBox();
       textBox1.Parent    = this;
       textBox1.Text      = "ABCD";
       textBox1.Multiline = true;
       textBox1.Width     = 400;
       textBox1.Height    = 100;
       
       
       this.components = new System.ComponentModel.Container();
       this.printButton = new System.Windows.Forms.Button();       this.ClientSize = new System.Drawing.Size(504, 381);
       this.Text = "Print Example";       printButton.ImageAlign = 
          System.Drawing.ContentAlignment.MiddleLeft;
       printButton.Location = new System.Drawing.Point(32, 110);
       printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
       printButton.TabIndex = 0;
       printButton.Text = "打印文本框的内容";
       printButton.Size = new System.Drawing.Size(136, 40);
       printButton.Click += new System.EventHandler(printButton_Click);       this.Controls.Add(printButton);
    }    // This is the main entry point for the application.
    public static void Main(string[] args) 
    {
       Application.Run(new PrintingExample());
    }
}

解决方案 »

  1.   

          float leftMargin = ev.MarginBounds.Left; 这行说明是左边界是从打印设置的左边界开始的。检查一下打印设置的左边界,或打印机的设置。
      

  2.   


          float leftMargin = ev.MarginBounds.Left;试着改为:      float leftMargin = 0;或      float leftMargin = -15;之类的数值,看看打印效果决定什么数值合适。
      

  3.   

     float leftMargin = ev.MarginBounds.Left; 的问题
      

  4.   


         float leftMargin = float.Parse(textBox2.Text.Trim());
    或者干脆改成上面那样,在界面上设置一个文本框 textBox2,用来输入打印的左边界(可以输入负值),试试看什么值合适。
      

  5.   

    float leftMargin =0;   //左边距
    float topMargin = 0;       //顶边距
    float verticalPosition = topMargin; 
    //初始化垂直位置,设为顶边距
    http://www.cnblogs.com/MaoBisheng/archive/2008/12/31/1365001.html