VB.NET 对打印机的操作 精点
2009-04-17 16:33
295 查看
Public Function PrintReport(ByVal dtbPrint As DataTable, ByVal strRptName As String, Optional ByVal bolPreview As Boolean = False) As Boolean
Dim rpttest As New SCJ_0140
Dim pdPrintDoc As New System.Drawing.Printing.PrintDocument
Dim rawkind As Integer = 1
Dim pmPrintMargins As CrystalDecisions.Shared.PageMargins
Dim intI As Integer
Dim dtPage As ReturnTable
Dim strPrintName, strPageName, strPageSource As String
Dim blnPrintName, blnPageSize, blnPageSource As Boolean
Dim ppd As New PrintPreviewDialog
Dim urv_frm As New UR_VIEW
Dim ColName As String
Dim txtobj As TextObject
Cursor.Current = Cursors.WaitCursor
Try
If (dtbPrint Is Nothing) Then
Return False
End If
If (dtbPrint.Rows.Count < 1) Then
Return True
End If
For intI = 0 To dtbPrint.Columns.Count - 1
ColName = dtbPrint.Columns(intI).ToString().Trim().Replace("_", "")
If (ColName <> "UserID" And ColName <> "MAWBNO") Then
txtobj = CType(rpttest.Section3.ReportObjects.Item("txt" + ColName), TextObject)
If (Not dtbPrint.Rows(0).Item(intI) Is System.DBNull.Value) Then
Select Case ColName
Case "SystemDate"
txtobj.Text = DateTime.Today.ToShortDateString().Substring(2)
Case "QuantityOfGoods"
txtobj.Text = "FREIGHT PREPAID " + Chr(13) + Chr(10) + "TOTAL DIM M3[" + Trim(CType(dtbPrint.Rows(0)("QuantityOfGoods"), String) + "") + "]"
Case "GrossWeight"
txtobj.Text = Trim(CType(dtbPrint.Rows(0)(intI), String) + "") + "KG"
Case "ChargeableWeight"
txtobj.Text = Trim(CType(dtbPrint.Rows(0)(intI), String) + "") + "kg"
Case Else
txtobj.Text = Trim(CType(dtbPrint.Rows(0)(intI), String) + "")
End Select
Else
If ColName = "SystemDate" Then
txtobj.Text = DateTime.Today.ToShortDateString().Substring(2)
Else
txtobj.Text = ""
End If
End If
End If
Next
'印字パラメータTBLSVデータ存在チェック
dtPage = PrintProcess.CheckPageset(strRptName, gstrIpAddress)
If dtPage.Flag Then
If dtPage.Table.Rows.Count > 0 Then
With rpttest.PrintOptions
If Not Convert.ToBoolean(dtPage.Table.Rows(0).Item("通常使うプリンタ")) Then
strPrintName = dtPage.Table.Rows(0).Item("プリンタ").ToString
strPrintName = strPrintName.Replace("@", gstrIpAddress)
For intI = 0 To PrinterSettings.InstalledPrinters.Count - 1
If PrinterSettings.InstalledPrinters.Item(intI).ToString.ToUpper = strPrintName.ToUpper Then
.PrinterName = PrinterSettings.InstalledPrinters.Item(intI)
blnPrintName = True
Exit For
End If
Next
If Not blnPrintName Then
MessageBox.Show("プリンタ '" & strPrintName & "' がありません。", gcstSystemName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
Else
strPageName = dtPage.Table.Rows(0).Item("用紙").ToString
pdPrintDoc.PrinterSettings.PrinterName = strPrintName
For intI = 0 To pdPrintDoc.PrinterSettings.PaperSizes.Count - 1
If pdPrintDoc.PrinterSettings.PaperSizes(intI).PaperName = strPageName Then
rawkind = pdPrintDoc.PrinterSettings.PaperSizes(intI).RawKind
.PaperSize = CType(rawkind, CrystalDecisions.Shared.PaperSize)
blnPageSize = True
Exit For
End If
Next
If Not blnPageSize Then
MessageBox.Show("用紙 '" & strPageName & "' がありません。", gcstSystemName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
Else
strPageSource = dtPage.Table.Rows(0).Item("用紙トレイ").ToString
For intI = 0 To pdPrintDoc.PrinterSettings.PaperSources.Count - 1
If pdPrintDoc.PrinterSettings.PaperSources(intI).SourceName = strPageSource Then
rawkind = pdPrintDoc.PrinterSettings.PaperSizes(intI).RawKind
.PaperSource = CType(rawkind, Crysta
4000
lDecisions.Shared.PaperSource)
blnPageSource = True
Exit For
End If
Next
If Not blnPageSource Then
MessageBox.Show("用紙トレイ '" & strPageSource & "' がありません。", gcstSystemName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
End If
End If
End If
pmPrintMargins.leftMargin = Convert.ToInt32(dtPage.Table.Rows(0).Item("余白左"))
pmPrintMargins.topMargin = Convert.ToInt32(dtPage.Table.Rows(0).Item("余白上"))
.ApplyPageMargins(pmPrintMargins)
End If
End With
Else
MessageShow("E0180")
Return False
End If
Else
MessageBox.Show(dtPage.Err.Message, gcstSystemName, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
rpttest.PrintOptions.PaperOrientation = CrystalDecisions.[Shared].PaperOrientation.Portrait
If (bolPreview) Then
urv_frm.RptName = rpttest
urv_frm.crvReport.ShowPrintButton = True
urv_frm.ShowDialog()
Else
rpttest.PrintToPrinter(1, True, 0, 0)
End If
Cursor.Current = Cursors.Default
Return True
Catch ex As Exception
ShowErrMessage(ex)
Finally
Cursor.Current = Cursors.Default
End Try
End Function
End Class
Dim rpttest As New SCJ_0140
Dim pdPrintDoc As New System.Drawing.Printing.PrintDocument
Dim rawkind As Integer = 1
Dim pmPrintMargins As CrystalDecisions.Shared.PageMargins
Dim intI As Integer
Dim dtPage As ReturnTable
Dim strPrintName, strPageName, strPageSource As String
Dim blnPrintName, blnPageSize, blnPageSource As Boolean
Dim ppd As New PrintPreviewDialog
Dim urv_frm As New UR_VIEW
Dim ColName As String
Dim txtobj As TextObject
Cursor.Current = Cursors.WaitCursor
Try
If (dtbPrint Is Nothing) Then
Return False
End If
If (dtbPrint.Rows.Count < 1) Then
Return True
End If
For intI = 0 To dtbPrint.Columns.Count - 1
ColName = dtbPrint.Columns(intI).ToString().Trim().Replace("_", "")
If (ColName <> "UserID" And ColName <> "MAWBNO") Then
txtobj = CType(rpttest.Section3.ReportObjects.Item("txt" + ColName), TextObject)
If (Not dtbPrint.Rows(0).Item(intI) Is System.DBNull.Value) Then
Select Case ColName
Case "SystemDate"
txtobj.Text = DateTime.Today.ToShortDateString().Substring(2)
Case "QuantityOfGoods"
txtobj.Text = "FREIGHT PREPAID " + Chr(13) + Chr(10) + "TOTAL DIM M3[" + Trim(CType(dtbPrint.Rows(0)("QuantityOfGoods"), String) + "") + "]"
Case "GrossWeight"
txtobj.Text = Trim(CType(dtbPrint.Rows(0)(intI), String) + "") + "KG"
Case "ChargeableWeight"
txtobj.Text = Trim(CType(dtbPrint.Rows(0)(intI), String) + "") + "kg"
Case Else
txtobj.Text = Trim(CType(dtbPrint.Rows(0)(intI), String) + "")
End Select
Else
If ColName = "SystemDate" Then
txtobj.Text = DateTime.Today.ToShortDateString().Substring(2)
Else
txtobj.Text = ""
End If
End If
End If
Next
'印字パラメータTBLSVデータ存在チェック
dtPage = PrintProcess.CheckPageset(strRptName, gstrIpAddress)
If dtPage.Flag Then
If dtPage.Table.Rows.Count > 0 Then
With rpttest.PrintOptions
If Not Convert.ToBoolean(dtPage.Table.Rows(0).Item("通常使うプリンタ")) Then
strPrintName = dtPage.Table.Rows(0).Item("プリンタ").ToString
strPrintName = strPrintName.Replace("@", gstrIpAddress)
For intI = 0 To PrinterSettings.InstalledPrinters.Count - 1
If PrinterSettings.InstalledPrinters.Item(intI).ToString.ToUpper = strPrintName.ToUpper Then
.PrinterName = PrinterSettings.InstalledPrinters.Item(intI)
blnPrintName = True
Exit For
End If
Next
If Not blnPrintName Then
MessageBox.Show("プリンタ '" & strPrintName & "' がありません。", gcstSystemName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
Else
strPageName = dtPage.Table.Rows(0).Item("用紙").ToString
pdPrintDoc.PrinterSettings.PrinterName = strPrintName
For intI = 0 To pdPrintDoc.PrinterSettings.PaperSizes.Count - 1
If pdPrintDoc.PrinterSettings.PaperSizes(intI).PaperName = strPageName Then
rawkind = pdPrintDoc.PrinterSettings.PaperSizes(intI).RawKind
.PaperSize = CType(rawkind, CrystalDecisions.Shared.PaperSize)
blnPageSize = True
Exit For
End If
Next
If Not blnPageSize Then
MessageBox.Show("用紙 '" & strPageName & "' がありません。", gcstSystemName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
Else
strPageSource = dtPage.Table.Rows(0).Item("用紙トレイ").ToString
For intI = 0 To pdPrintDoc.PrinterSettings.PaperSources.Count - 1
If pdPrintDoc.PrinterSettings.PaperSources(intI).SourceName = strPageSource Then
rawkind = pdPrintDoc.PrinterSettings.PaperSizes(intI).RawKind
.PaperSource = CType(rawkind, Crysta
4000
lDecisions.Shared.PaperSource)
blnPageSource = True
Exit For
End If
Next
If Not blnPageSource Then
MessageBox.Show("用紙トレイ '" & strPageSource & "' がありません。", gcstSystemName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Return False
End If
End If
End If
pmPrintMargins.leftMargin = Convert.ToInt32(dtPage.Table.Rows(0).Item("余白左"))
pmPrintMargins.topMargin = Convert.ToInt32(dtPage.Table.Rows(0).Item("余白上"))
.ApplyPageMargins(pmPrintMargins)
End If
End With
Else
MessageShow("E0180")
Return False
End If
Else
MessageBox.Show(dtPage.Err.Message, gcstSystemName, MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
rpttest.PrintOptions.PaperOrientation = CrystalDecisions.[Shared].PaperOrientation.Portrait
If (bolPreview) Then
urv_frm.RptName = rpttest
urv_frm.crvReport.ShowPrintButton = True
urv_frm.ShowDialog()
Else
rpttest.PrintToPrinter(1, True, 0, 0)
End If
Cursor.Current = Cursors.Default
Return True
Catch ex As Exception
ShowErrMessage(ex)
Finally
Cursor.Current = Cursors.Default
End Try
End Function
End Class
相关文章推荐
- VB.Net 操作打印机
- 使用VB.Net写一个简单的数据访问层(不能称ORM):CRUD操作
- VB.NET String数据类型操作技巧
- vb.net 教程 8-3 数据库操作2
- vb.net 快捷键盘的操作
- VB.NET中操作xml文件
- vb.net 教程 8-3 数据库操作9-3
- vb.net中xml操作实例
- vb.net的多窗体操作
- [VB.NET]简单的 XML 文档操作
- VB.net操作oracle数据库
- 嵌入式下VB.NET窗体操作 DLL
- VB.net 注册表操作API
- vb.net 操作barcode
- VB.NET操作WORD(转)
- 在vb.net 里面实现对sql server 存储过程的操作
- VB.NET & 职责链模式 (下机时间操作)
- vb.net 教程 4-12 ini文件操作 3 应用
- C#(VB.NET)操作Windows自带的防火墙 之 综述篇
- C#(VB.NET)操作Windows自带的防火墙 之 启用(开启)/禁用(关闭)防火墙