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

发布时间:2014-10-25 2:22:04
来源:分享查询网

 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 FunctionEnd Class

返回顶部
查看电脑版