如何在VB中打印多个页面?

时间:2019-01-17 09:32:44

标签: vb.net

我应该在代码中进行哪些更改,以便在“打印预览”中看到多个页面,并且还要打印多个页面?

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDoc.PrintPage
    Static intStart As Integer
    Dim font0 As New Font("arial", 24, FontStyle.Underline)
    Dim fntText As Font = font0
    Dim txtHeight As Integer
    Dim LeftMargin As Integer = PrintDoc.DefaultPageSettings.Margins.Left
    Dim TopMargin As Integer = PrintDoc.DefaultPageSettings.Margins.Top
    txtHeight = PrintDoc.DefaultPageSettings.PaperSize.Height - PrintDoc.DefaultPageSettings.Margins.Top - PrintDoc.DefaultPageSettings.Margins.Bottom

    Dim LinesPerPage As Integer = CInt(Math.Round(txtHeight / (fntText.Height + 0.025)))


    e.Graphics.DrawRectangle(Pens.White, e.MarginBounds)
    Dim intLineNumber As Integer
    e.Graphics.DrawString("Support", font0, Brushes.Black, 75, 50)
    For intCounter = intStart To lstLinesToPrint.Count - 1

        e.Graphics.DrawString(lstLinesToPrint(intCounter), fntText, Brushes.Black, LeftMargin, fntText.Height * intLineNumber + TopMargin)
        intLineNumber += 1
        If intLineNumber > LinesPerPage - 1 Then
            intStart = intCounter
            e.HasMorePages = True
            Exit For
        End If

    Next
End Sub    

1 个答案:

答案 0 :(得分:0)

照原样,您的代码可以打印多页...只要提供其他代码即可。

我在窗体上放置了两个按钮bnPrint和bnPreview,并创建了一些数据进行测试:

Option Strict On

Imports System.Drawing.Printing

Public Class Form1

    Enum PrintOperation
        Print
        Preview
    End Enum

    Dim pageSetupDialog1 As PageSetupDialog
    Dim pd As PrintDocument
    Dim selectedPrinter As String

    Dim lstLinesToPrint As New List(Of String)

    Sub PrintDoc(operation As PrintOperation)

        Try
            pd = New PrintDocument()

            pageSetupDialog1 = New PageSetupDialog
            pageSetupDialog1.PageSettings = New PageSettings

            ' initialize dialog's PrinterSettings property to hold user-set printer settings.
            pageSetupDialog1.PrinterSettings = New PrinterSettings

            pageSetupDialog1.AllowMargins = True
            pageSetupDialog1.ShowNetwork = True

            If selectedPrinter <> "" Then
                pageSetupDialog1.PrinterSettings.PrinterName = selectedPrinter
            End If

            Dim result As DialogResult = pageSetupDialog1.ShowDialog()

            If result = DialogResult.OK Then
                selectedPrinter = pageSetupDialog1.PrinterSettings.PrinterName
                AddHandler pd.BeginPrint, AddressOf pd_BeginPrint
                AddHandler pd.QueryPageSettings, AddressOf pd_QueryPageSettings
                AddHandler pd.PrintPage, AddressOf pd_PrintPage

                Try
                    If operation = PrintOperation.Print Then
                        pd.Print()
                    Else
                        Using ppd As New PrintPreviewDialog
                            ppd.Document = pd
                            ppd.ShowDialog()
                        End Using
                    End If

                Catch ex As Exception
                    MsgBox("Oops: " & ex.Message)

                Finally
                    RemoveHandler pd.BeginPrint, AddressOf pd_BeginPrint
                    RemoveHandler pd.QueryPageSettings, AddressOf pd_QueryPageSettings
                    RemoveHandler pd.PrintPage, AddressOf pd_PrintPage
                End Try

            End If

        Finally
            pageSetupDialog1.Dispose()
            pd.Dispose()

        End Try

    End Sub

    Sub pd_BeginPrint(ByVal sender As Object, ByVal ev As PrintEventArgs)
        pd.PrinterSettings.PrinterName = selectedPrinter
        pd.DocumentName = "Test document"
    End Sub

    Sub pd_QueryPageSettings(ByVal sender As Object, ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs)
        ' make sure the orientation is set /before/ it gets to pd_PrintPage
        e.PageSettings.Landscape = pageSetupDialog1.PageSettings.Landscape
    End Sub

    Private Sub pd_PrintPage(sender As Object, e As Printing.PrintPageEventArgs)
        Static intStart As Integer

        Using font0 As New Font("Arial", 24, FontStyle.Underline), fntText As Font = font0

            Dim LeftMargin As Integer = pd.DefaultPageSettings.Margins.Left
            Dim TopMargin As Integer = pd.DefaultPageSettings.Margins.Top
            Dim txtHeight = pd.DefaultPageSettings.PaperSize.Height - pd.DefaultPageSettings.Margins.Top - pd.DefaultPageSettings.Margins.Bottom

            Dim LinesPerPage As Integer = CInt(Math.Round(txtHeight / (fntText.Height + 0.025)))

            e.Graphics.DrawRectangle(Pens.White, e.MarginBounds)
            Dim intLineNumber As Integer
            e.Graphics.DrawString("Support", font0, Brushes.Black, 75, 50)

            For intCounter = intStart To lstLinesToPrint.Count - 1
                e.Graphics.DrawString(lstLinesToPrint(intCounter), fntText, Brushes.Black, LeftMargin, fntText.Height * intLineNumber + TopMargin)
                intLineNumber += 1

                If intLineNumber > LinesPerPage - 1 Then
                    intStart = intCounter
                    e.HasMorePages = True
                    Exit For
                End If

            Next

        End Using


        If Not e.HasMorePages Then
            ' reset in case the user does a preview then a print etc.
            intStart = 0
        End If

    End Sub

    Private Sub bnPrint_Click(sender As Object, e As EventArgs) Handles bnPrint.Click
        PrintDoc(PrintOperation.Print)

    End Sub

    Private Sub bnPreview_Click(sender As Object, e As EventArgs) Handles bnPreview.Click
        PrintDoc(PrintOperation.Preview)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Create some test data...
        For i = 1 To 100
            lstLinesToPrint.Add("Line" & i)
        Next

    End Sub


End Class

我对您的代码进行了一些调整:字体需要使用.Dispose()才能在使用完毕后释放系统资源-Using构造可解决此问题。我可以在其中放置更多的Using语句,但是有时我使用Try的Final子句代替。我更改了PrintDoc的名称,因为它已经是我从不得不处理的其他程序中复制的方法的名称。

如果要保持以横向格式打印的可能性,请注意,如果以横向格式打印,则页边距符合预期(左边等),但是宽度和高度却是错误的。我没有更改显示的代码以考虑到这一点。

打印预览最后一页的示例输出:

Print preview dialog

相关问题