日期:2014-05-17  浏览次数:21202 次

printDocument 实现小票打印,居然不能换行
printDocument,到调用的时候,最后几行就不换行了,麻烦高手给看一下
------------------打印类-------------------------------------
Imports System.Drawing
Imports System.Drawing.Printing

Public Class clsPrint
  '---定义全局变量----
  Public Shared sPrintContent As String '要打印的内容(指待打印的小票内容)

  ''' <summary>
  ''' 用指定的打印机打印小票()
  ''' </summary>
  ''' 待扩展:上下左右的边界可由设置,纸张大小可设置
  ''' <param name="DYJMC">打印机名称</param>
  ''' <remarks></remarks>
  Public Shared Sub A_pPrint(ByVal DYJMC As String)
  Dim OK As Boolean = False

  'Dim PPD As PrintPreviewDialog = New PrintPreviewDialog()'打印预览  
  Dim Pd As PrintDocument = New PrintDocument()
  '--------判断该打印机是否存在---------
  Dim DYJ As PrinterSettings.StringCollection = PrinterSettings.InstalledPrinters
  For Each s In DYJ
  If s = DYJMC Then
  OK = True
  Exit For
  End If
  Next

  If OK Then '如果指定的打印机存在,则用该打印机,否则用默认打印机
  Pd.PrinterSettings.PrinterName = DYJMC '
  End If

  '设置边距
  Dim MARGIN As Margins = New Margins(2, 1, 2, 1)
  Pd.DefaultPageSettings.Margins = MARGIN
  '纸张设置默认
  'Dim pageSize As PaperSize = New PaperSize("First custom size", getWidth(58), 1700)
  'Pd.DefaultPageSettings.PaperSize = pageSize

  '打印事件设置  
  AddHandler Pd.PrintPage, AddressOf pd_PrintPage

  '打印预览 
  'PPD.Document = PD
  'ppd.ShowDialog(me)
  Try
  Pd.Print()
  Catch ex As Exception
  MessageBox.Show(ex.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error)
  Pd.PrintController.OnEndPrint(Pd, New PrintEventArgs())
  End Try

  End Sub

  '获取宽度
  Public Shared Function getWidth(ByVal cm As Double) As Integer
  Return CInt(cm / 25.4) * 100
  End Function

  '添加自定义事件
  Public Shared Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
  Dim printerHDC As IntPtr = ev.Graphics.GetHdc()
  Dim g As Graphics = Graphics.FromHdc(printerHDC)
  g.DrawString(sPrintContent, New Font("宋体", 9), Brushes.Red, New PointF(0, 0))
  End Sub

End Class
-------------------调用类,开始打印的代码----------------------------------

 Private Sub print()
  Dim sTMP As String = ""
  Dim ZJE As Decimal = 0
  '--------增加单号信息-------------
  sTMP = gSysInfo.sMDMC + "班报" + Chr(13) + Chr(10)
  sTMP += "班 号:" + DQBH + " 第" + DQBC + "班" + Chr(13) + Chr(10)
  sTMP += "收 银 员:" + gUserInfo.sUserId + " 收款机号:" + gSysInfo.sDQSYJ + Chr(13) + Chr(10)
  sTMP += "前班余额:" + lbQXYE.Text + " 销售额:" + lbYYE.Text + Chr(13) + Chr(10)
  sTMP += "本班实收:" + lbBBSSJE.Text + " 折扣额:" + lbZKJE.Text + Chr(13) + Chr(10)
  sTMP += "现 金:" + lbXJ.Text + " 刷卡:" + lbSK.Text + Chr(13) + Chr(10)
  sTMP += "钱箱余额:" + lbBBQXYE.Text + " 抵用券:" + lbDYQ.Text + Chr(13) + Chr(10)
  sTMP += "当班时间:" + lbKSSJ.Text + Chr(13) + Chr(10) '开始时间
  sTMP += " " + lbJSSJ.Text + Chr(13) + Chr(10) '结束时间
  sTMP += "打印时间:" + Now.ToString + Chr(13) + Chr(10)
  sTMP += "---------