2017-08-09 60 views
0

Iam使用Visual Studio 2012来建立和打印机:EPSON TM-T88IV收据打印。 我创建了一个PrintDocument的,这里是我的代码:打印收据与动态高度printDocument C#

public void print_ticket(string service){ 

    int pos = Array.IndexOf(x_service_print_key, service); 

    // Nếu phím bấm tồn tại để kích hoạt in phiếu 
    if (pos > -1) 
    { 
     string printer_code = "1"; 
     string service_code = x_service_code[pos]; 
     string priority = x_priority[pos]; 
     string number = (Convert.ToInt32(x_serial_number[pos]) + 1).ToString().PadLeft(x_serial_number[pos].Length, '0'); 

     // Lấy số cuối cùng của dịch vụ cộng thêm 1 và gán cho biến number 
     string sql = "SELECT number FROM current WHERE service_code = '"+x_service_code[pos]+"' ORDER BY id DESC LIMIT 1"; 
     MySqlCommand cmd = new MySqlCommand(sql, connection); 
     cmd.CommandTimeout = 60; 
     DbDataReader reader = cmd.ExecuteReader(); 
     if (reader.HasRows) 
     { 
      while (reader.Read()) 
      { 
       number = (Convert.ToInt32(reader.GetValue(reader.GetOrdinal("number"))) + 1).ToString().PadLeft(x_serial_number[pos].Length, '0'); 
      } 
     } 
     reader.Close(); 

     // Insert vào database số mới. 
     sql = "INSERT INTO current(printer_code, service_code, priority, number) VALUES('" + printer_code + "','" + service_code + "'," + priority + ",'" + number + "')"; 
     cmd = new MySqlCommand(sql, connection); 
     int rowCount = cmd.ExecuteNonQuery(); 

     // Cập nhật ra màn hình 
     this.Controls.Find("lbl_" + x_service_code[pos].ToString(), true).FirstOrDefault().Text = number; 


     // In phiếu ra giấy 
     printTicket.PrintPage += (sender, e) => 
     { 
      e.Graphics.Clear(Color.White); 
      // Khai báo thông số text 
      // -- Màu sắc 
      SolidBrush drawBrush = new SolidBrush(Color.Black); 
      // -- Font chữ, size chữ 
      Font drawFont1 = new System.Drawing.Font("Arial", 12, FontStyle.Bold); 
      Font drawFont2 = new System.Drawing.Font("Arial", 60, FontStyle.Bold); 
      Font drawFont3 = new System.Drawing.Font("Arial", 16, FontStyle.Bold); 
      Font drawFont4 = new System.Drawing.Font("Arial", 10, FontStyle.Italic | FontStyle.Bold); 
      // -- Căn giữa 
      StringFormat drawFormat = new StringFormat(); 
      drawFormat.Alignment = StringAlignment.Center; 

      // Logo 
      Image imgLogo = QMSBUTTON.Properties.Resources.p_logo; 
      e.Graphics.DrawImage(imgLogo, 20, 0, 280, 24); 
      // Khoa khám bệnh 
      RectangleF recAtZero = new RectangleF(0, 25, e.PageBounds.Width, e.PageBounds.Height); 
      e.Graphics.DrawString("KHOA KHÁM BỆNH", drawFont1, drawBrush, recAtZero, drawFormat); 
      // Số thứ tự 
      recAtZero = new RectangleF(0, 50, e.PageBounds.Width, e.PageBounds.Height); 
      e.Graphics.DrawString(number, drawFont2, drawBrush, recAtZero, drawFormat); 
      // Tên dịch vụ 
      recAtZero = new RectangleF(0, 150, e.PageBounds.Width, e.PageBounds.Height); 
      e.Graphics.DrawString(x_service_name[pos], drawFont3, drawBrush, recAtZero, drawFormat); 
      // Phiếu in lúc 
      recAtZero = new RectangleF(0, 180, e.PageBounds.Width, e.PageBounds.Height); 
      e.Graphics.DrawString("Phiếu in lúc: " + DateTime.Now.ToString("HH:mm:ss - dd/MM/yyyy"), drawFont1, drawBrush, recAtZero, drawFormat); 
      // Phiếu chỉ có giá trị trong ngày! 
      recAtZero = new RectangleF(0, 200, e.PageBounds.Width, e.PageBounds.Height); 
      e.Graphics.DrawString("Phiếu chỉ có giá trị trong ngày!", drawFont4, drawBrush, recAtZero, drawFormat); 


     }; 

     printTicketDialog.ShowDialog(); 
     //printTicket.Print(); 
    } 
} 

的问题是,当我打印,页面的高度如此之长并完成文档之后丝毫没有削减。我需要它在最后一次绘图文本之后动态切割。

对不起,我的英语不好。谢谢!

enter image description here

回答

1
// Add list of supported paper sizes found on the printer. 
// The DisplayMember property is used to identify the property that will provide the display string. 
comboPaperSize.DisplayMember = "PaperName"; 

PaperSize pkSize; 
for (int i = 0; i < printDoc.PrinterSettings.PaperSizes.Count; i++){ 
    pkSize = printDoc.PrinterSettings.PaperSizes[i]; 
    comboPaperSize.Items.Add(pkSize); 
} 

// Create a PaperSize and specify the custom paper size through the constructor and add to combobox. 
PaperSize pkCustomSize1 = new PaperSize("First custom size", 100, 200); 

comboPaperSize.Items.Add(pkCustomSize1); 

链接:https://msdn.microsoft.com/en-us/library/system.drawing.printing.printersettings.papersizes(v=vs.110).aspx

1

我找到了办法:

PaperSize pageSize = new PaperSize(); 
pageSize.Width = 284; 
printDocument.DefaultPageSettings.PaperSize = pageSize; 

设置papeSize到PrintDocument的,但只设置宽度,不设身高。 printDialog看起来很奇怪,但打印机是正确的。