VB.NET 对打印机的操作 精点

 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, CrystalDecisions.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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值