Vb.net 如何在Visual Basic中使用多个Windows窗体进行多页双面打印?

Vb.net 如何在Visual Basic中使用多个Windows窗体进行多页双面打印?,vb.net,forms,printing,printdocument,Vb.net,Forms,Printing,Printdocument,我正在设法将多个Windows窗体打印为报表。我有多个表格可以计算我们客户的退休福利,我正试图打印这些表格,以便我们的客户得到救济。此时,我在每个表单上都有一个PrintDocument组件,我可以将每个表单作为不同的文档打印在单独的页面上,但我试图实现的是,用一个打印按钮在一个PrintDocument文档中重复打印所有这些表单。单击另一个表单 给你举个例子。我有两个Windows窗体(FormAOW和FormBasisInfo),每个窗体都有一个PrintDocument组件。我已经绘制了文

我正在设法将多个Windows窗体打印为报表。我有多个表格可以计算我们客户的退休福利,我正试图打印这些表格,以便我们的客户得到救济。此时,我在每个表单上都有一个PrintDocument组件,我可以将每个表单作为不同的文档打印在单独的页面上,但我试图实现的是,用一个打印按钮在一个PrintDocument文档中重复打印所有这些表单。单击另一个表单

给你举个例子。我有两个Windows窗体(FormAOW和FormBasisInfo),每个窗体都有一个PrintDocument组件。我已经绘制了文本、图像、图表等。按照打印文档1_PrintPage sub中的正确位置绘制。我想在第三个表单CloseReport中打印表单AOW和基本信息

我已有的代码没有双面打印:

基本信息:

Public Class Basisinfo


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles ButtonPrinten.Click

    For teller = 1 To 30
        If inhoudlijst(teller, 0) = "" Then
            inhoudlijst(teller, 0) = "Basisinfo"
            inhoudlijst(teller, 1) = "True"
            PrintDocument1.DefaultPageSettings.Landscape = True
            PrintDocument1.DefaultPageSettings.PrinterSettings.Duplex = Printing.Duplex.Default
            printarray3(teller) = PrintDocument1
            Exit For
        End If
    Next
End Sub


Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    Dim stringFormatCenter As New StringFormat()
    stringFormatCenter.Alignment = StringAlignment.Center
    stringFormatCenter.LineAlignment = StringAlignment.Center

    Dim stringFormatFar As New StringFormat()
    stringFormatFar.Alignment = StringAlignment.Far
    stringFormatFar.LineAlignment = StringAlignment.Center

    Dim stringFormatNear As New StringFormat()
    stringFormatNear.Alignment = StringAlignment.Near
    stringFormatNear.LineAlignment = StringAlignment.Center


    'Alle TextBoxes met witte achtergrond
    For Each TextBox In {TextBox17, TextBox18, TextBox35, TextBox1, TextBox29, TextBox2, TextBox3, TextBox4, TextBox12, TextBox13, TextBox10, TextBox9, TextBox26, TextBox8, TextBox7, TextBox16, TextBox21, TextBox14, TextBox28, TextBox27, TextBox6, TextBox23, TextBox19, TextBox31, TextBox24, TextBox25, TextBox30, TextBox15, TextBox34, TextBox22, TextBox20, TextBox33, TextBox32}
        e.Graphics.DrawRectangle(Pens.Black, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height))
        e.Graphics.FillRectangle(Brushes.White, TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height)
    Next

    'Alle TextbBoxes met gele achtergrond
    For Each TextBox In {TextBox17, TextBox18, TextBox35, TextBox1, TextBox29}
        e.Graphics.FillRectangle(Brushes.LightYellow, TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height)
    Next


    'Textboxes Centered
    For Each TextBox In {TextBox17, TextBox18, TextBox35, TextBox1, TextBox29}
        e.Graphics.DrawString(TextBox.Text, TextBox.Font, Brushes.Blue, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height), stringFormatCenter)
    Next

    'Textboxes Near
    For Each TextBox In {TextBox2, TextBox3, TextBox4, TextBox12, TextBox13, TextBox10, TextBox9, TextBox26, TextBox8, TextBox7, TextBox16, TextBox21, TextBox14, TextBox28, TextBox27, TextBox6, TextBox23, TextBox19, TextBox31, TextBox24, TextBox25, TextBox30, TextBox15, TextBox34, TextBox22, TextBox20, TextBox33, TextBox32}
        e.Graphics.DrawString(TextBox.Text, TextBox.Font, Brushes.Blue, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width + 20, TextBox.Height), stringFormatNear)
    Next


    'Textboxes Far
    For Each TextBox In {}
        e.Graphics.DrawString(TextBox.Text, TextBox.Font, Brushes.Blue, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height), stringFormatFar)
    Next



    For Each c As Control In Me.Controls
        If c.GetType Is GetType(Label) Then
            e.Graphics.DrawString(c.Text, c.Font, Brushes.Black, c.Left, c.Top)
        End If
    Next
End Sub
AOW:

关闭报告:

Public Class CloseReport


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    PrintDocument1.DefaultPageSettings.Landscape = True
    PrintDialog1.Document = PrintDocument1
    If PrintDialog1.ShowDialog() = DialogResult.OK Then
        For teller = 1 To 30
            If IsNothing(printarray3(teller)) = False Then
                printen6(printarray3(teller))
            End If
        Next teller
    End If
End Sub
End Class
如果您想知道printen6函数的功能:

    PrintDocument.PrinterSettings.DefaultPageSettings.PrinterResolution.Kind = Printing.PrinterResolutionKind.High
    PrintDocument.PrinterSettings.DefaultPageSettings.Landscape = True
    PrintDocument.PrinterSettings.DefaultPageSettings.Margins = New Printing.Margins(0, 0, 0, 0)

    PrintDocument.DefaultPageSettings.PrinterSettings.Duplex = Printing.Duplex.Default


    PrintDocument.Print()
我正在考虑如何合并PrintDocument文件以实现双面打印,或者如何使用HasMorePages创建多个页面。我读过很多关于HasMorePages的文章,但这只提供了加载文本文件的代码


我希望你能帮助我,因为我真的被困在这件事上了。提前谢谢

对于那些对答案感兴趣的人来说:这是e.HasMorePages=True。。但唯一缺少的是一个(公共)的“页码”计数器,它可以在两种情况之间进行选择对于e.HasMorePages来说,到达子页面的末尾很重要。否则就没有效果了。请参阅下面的代码

Public Class Inhoudsopgave
Dim PageNumber As Integer = 0
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

    PrintDocument1.DefaultPageSettings.Margins = New Printing.Margins(0, 0, 0, 0)
    PrintDocument1.DefaultPageSettings.Landscape = True
    PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.
    If PrintDialog1.ShowDialog() = DialogResult.OK Then
        PrintDocument1.Print()
    End If
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    PrintDocument1.DefaultPageSettings.Margins = New Printing.Margins(0, 0, 0, 0)

    Select Case PageNumber
        Case 0
            For Each c As Control In Me.Controls
                If c.GetType Is GetType(Label) Then
                    e.Graphics.DrawString(c.Text, c.Font, Brushes.Black, c.Left, c.Top)
                End If
                e.HasMorePages = True
            Next
        Case 1
            'aow
            Dim stringFormatCenter As New StringFormat()
            stringFormatCenter.Alignment = StringAlignment.Center
            stringFormatCenter.LineAlignment = StringAlignment.Center

            Dim stringFormatFar As New StringFormat()
            stringFormatFar.Alignment = StringAlignment.Far
            stringFormatFar.LineAlignment = StringAlignment.Center

            Dim stringFormatNear As New StringFormat()
            stringFormatNear.Alignment = StringAlignment.Near
            stringFormatNear.LineAlignment = StringAlignment.Center


            'Doe dit voor iedere Groupbox en Chart
            Dim BMPGroupbox1 As Bitmap = New Bitmap(AOW.GroupBox1.Width, AOW.GroupBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim BMPGroupbox2 As Bitmap = New Bitmap(AOW.GroupBox2.Width, AOW.GroupBox2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim BMPGroupbox3 As Bitmap = New Bitmap(AOW.GroupBox3.Width, AOW.GroupBox3.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim BMPChart1 As Bitmap = New Bitmap(AOW.Chart1.Width, AOW.Chart1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)

            'Doe dit voor iedere Groupbox en Chart
            AOW.GroupBox1.DrawToBitmap(BMPGroupbox1, New Rectangle(0, 0, AOW.GroupBox1.Width, AOW.GroupBox1.Height))
            AOW.GroupBox2.DrawToBitmap(BMPGroupbox2, New Rectangle(0, 0, AOW.GroupBox2.Width, AOW.GroupBox2.Height))
            AOW.GroupBox3.DrawToBitmap(BMPGroupbox3, New Rectangle(0, 0, AOW.GroupBox3.Width, AOW.GroupBox3.Height))
            AOW.Chart1.DrawToBitmap(BMPChart1, New Rectangle(0, 0, AOW.Chart1.Width, AOW.Chart1.Height))

            'Doe dit voor iedere Groupbox en Chart
            e.Graphics.DrawImage(BMPGroupbox1, AOW.GroupBox1.Left, AOW.GroupBox1.Top, AOW.GroupBox1.Width, AOW.GroupBox1.Height)
            e.Graphics.DrawImage(BMPGroupbox2, AOW.GroupBox2.Left, AOW.GroupBox2.Top, AOW.GroupBox2.Width, AOW.GroupBox2.Height)
            e.Graphics.DrawImage(BMPGroupbox3, AOW.GroupBox3.Left, AOW.GroupBox3.Top, AOW.GroupBox3.Width, AOW.GroupBox3.Height)
            e.Graphics.DrawImage(BMPChart1, AOW.Chart1.Left, AOW.Chart1.Top, AOW.Chart1.Width, AOW.Chart1.Height)

            'Alle TextBoxes met witte achtergrond
            For Each TextBox In {AOW.Koptextbox, AOW.TextBox99, AOW.TextBox19, AOW.TextBox21, AOW.TextBox11, AOW.TextBox12, AOW.TextBox14, AOW.TextBox13, AOW.TextBox17, AOW.TextBox15, AOW.TextBox16, AOW.TextBox18}
                e.Graphics.DrawRectangle(Pens.Black, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height))
                e.Graphics.FillRectangle(Brushes.White, TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height)
            Next

            'Alle TextbBoxes met gele achtergrond
            For Each TextBox In {AOW.Koptextbox, AOW.TextBox99, AOW.TextBox19, AOW.TextBox21, AOW.TextBox13, AOW.TextBox17, AOW.TextBox18, AOW.TextBox16}
                e.Graphics.FillRectangle(Brushes.LightYellow, TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height)
            Next

            'Textboxes Centered
            For Each TextBox In {AOW.Koptextbox, AOW.TextBox99, AOW.TextBox21, AOW.TextBox19, AOW.TextBox14, AOW.TextBox13, AOW.TextBox17, AOW.TextBox15, AOW.TextBox16, AOW.TextBox18}
                e.Graphics.DrawString(TextBox.Text, TextBox.Font, Brushes.Blue, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height), stringFormatCenter)
            Next

            'Textboxes Near
            For Each TextBox In {AOW.TextBox1, AOW.TextBox2, AOW.TextBox3, AOW.TextBox4, AOW.TextBox5, AOW.TextBox6, AOW.TextBox7, AOW.TextBox8, AOW.TextBox9, AOW.TextBox10, AOW.TextBox11, AOW.TextBox24, AOW.TextBox23, AOW.TextBox22}
                e.Graphics.DrawString(TextBox.Text, TextBox.Font, Brushes.Blue, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width + 20, TextBox.Height), stringFormatNear)
            Next

            'Textboxes Far
            For Each TextBox In {AOW.TextBox12}
                e.Graphics.DrawString(TextBox.Text, TextBox.Font, Brushes.Blue, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height), stringFormatFar)
            Next

            For Each c As Control In AOW.Controls
                If c.GetType Is GetType(Label) Then
                    e.Graphics.DrawString(c.Text, c.Font, Brushes.Black, c.Left, c.Top)
                End If
            Next

            e.HasMorePages = False
    End Select

    PageNumber += 1
End Sub
End Class

对于那些对答案感兴趣的人来说:这是e.HasMorePages=True。。但唯一缺少的是一个(公共)的“页码”计数器,它可以在两种情况之间进行选择对于e.HasMorePages来说,到达子页面的末尾很重要。否则就没有效果了。请参阅下面的代码

Public Class Inhoudsopgave
Dim PageNumber As Integer = 0
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

    PrintDocument1.DefaultPageSettings.Margins = New Printing.Margins(0, 0, 0, 0)
    PrintDocument1.DefaultPageSettings.Landscape = True
    PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.
    If PrintDialog1.ShowDialog() = DialogResult.OK Then
        PrintDocument1.Print()
    End If
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

    PrintDocument1.DefaultPageSettings.Margins = New Printing.Margins(0, 0, 0, 0)

    Select Case PageNumber
        Case 0
            For Each c As Control In Me.Controls
                If c.GetType Is GetType(Label) Then
                    e.Graphics.DrawString(c.Text, c.Font, Brushes.Black, c.Left, c.Top)
                End If
                e.HasMorePages = True
            Next
        Case 1
            'aow
            Dim stringFormatCenter As New StringFormat()
            stringFormatCenter.Alignment = StringAlignment.Center
            stringFormatCenter.LineAlignment = StringAlignment.Center

            Dim stringFormatFar As New StringFormat()
            stringFormatFar.Alignment = StringAlignment.Far
            stringFormatFar.LineAlignment = StringAlignment.Center

            Dim stringFormatNear As New StringFormat()
            stringFormatNear.Alignment = StringAlignment.Near
            stringFormatNear.LineAlignment = StringAlignment.Center


            'Doe dit voor iedere Groupbox en Chart
            Dim BMPGroupbox1 As Bitmap = New Bitmap(AOW.GroupBox1.Width, AOW.GroupBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim BMPGroupbox2 As Bitmap = New Bitmap(AOW.GroupBox2.Width, AOW.GroupBox2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim BMPGroupbox3 As Bitmap = New Bitmap(AOW.GroupBox3.Width, AOW.GroupBox3.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim BMPChart1 As Bitmap = New Bitmap(AOW.Chart1.Width, AOW.Chart1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)

            'Doe dit voor iedere Groupbox en Chart
            AOW.GroupBox1.DrawToBitmap(BMPGroupbox1, New Rectangle(0, 0, AOW.GroupBox1.Width, AOW.GroupBox1.Height))
            AOW.GroupBox2.DrawToBitmap(BMPGroupbox2, New Rectangle(0, 0, AOW.GroupBox2.Width, AOW.GroupBox2.Height))
            AOW.GroupBox3.DrawToBitmap(BMPGroupbox3, New Rectangle(0, 0, AOW.GroupBox3.Width, AOW.GroupBox3.Height))
            AOW.Chart1.DrawToBitmap(BMPChart1, New Rectangle(0, 0, AOW.Chart1.Width, AOW.Chart1.Height))

            'Doe dit voor iedere Groupbox en Chart
            e.Graphics.DrawImage(BMPGroupbox1, AOW.GroupBox1.Left, AOW.GroupBox1.Top, AOW.GroupBox1.Width, AOW.GroupBox1.Height)
            e.Graphics.DrawImage(BMPGroupbox2, AOW.GroupBox2.Left, AOW.GroupBox2.Top, AOW.GroupBox2.Width, AOW.GroupBox2.Height)
            e.Graphics.DrawImage(BMPGroupbox3, AOW.GroupBox3.Left, AOW.GroupBox3.Top, AOW.GroupBox3.Width, AOW.GroupBox3.Height)
            e.Graphics.DrawImage(BMPChart1, AOW.Chart1.Left, AOW.Chart1.Top, AOW.Chart1.Width, AOW.Chart1.Height)

            'Alle TextBoxes met witte achtergrond
            For Each TextBox In {AOW.Koptextbox, AOW.TextBox99, AOW.TextBox19, AOW.TextBox21, AOW.TextBox11, AOW.TextBox12, AOW.TextBox14, AOW.TextBox13, AOW.TextBox17, AOW.TextBox15, AOW.TextBox16, AOW.TextBox18}
                e.Graphics.DrawRectangle(Pens.Black, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height))
                e.Graphics.FillRectangle(Brushes.White, TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height)
            Next

            'Alle TextbBoxes met gele achtergrond
            For Each TextBox In {AOW.Koptextbox, AOW.TextBox99, AOW.TextBox19, AOW.TextBox21, AOW.TextBox13, AOW.TextBox17, AOW.TextBox18, AOW.TextBox16}
                e.Graphics.FillRectangle(Brushes.LightYellow, TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height)
            Next

            'Textboxes Centered
            For Each TextBox In {AOW.Koptextbox, AOW.TextBox99, AOW.TextBox21, AOW.TextBox19, AOW.TextBox14, AOW.TextBox13, AOW.TextBox17, AOW.TextBox15, AOW.TextBox16, AOW.TextBox18}
                e.Graphics.DrawString(TextBox.Text, TextBox.Font, Brushes.Blue, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height), stringFormatCenter)
            Next

            'Textboxes Near
            For Each TextBox In {AOW.TextBox1, AOW.TextBox2, AOW.TextBox3, AOW.TextBox4, AOW.TextBox5, AOW.TextBox6, AOW.TextBox7, AOW.TextBox8, AOW.TextBox9, AOW.TextBox10, AOW.TextBox11, AOW.TextBox24, AOW.TextBox23, AOW.TextBox22}
                e.Graphics.DrawString(TextBox.Text, TextBox.Font, Brushes.Blue, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width + 20, TextBox.Height), stringFormatNear)
            Next

            'Textboxes Far
            For Each TextBox In {AOW.TextBox12}
                e.Graphics.DrawString(TextBox.Text, TextBox.Font, Brushes.Blue, New Rectangle(TextBox.Left, TextBox.Top, TextBox.Width, TextBox.Height), stringFormatFar)
            Next

            For Each c As Control In AOW.Controls
                If c.GetType Is GetType(Label) Then
                    e.Graphics.DrawString(c.Text, c.Font, Brushes.Black, c.Left, c.Top)
                End If
            Next

            e.HasMorePages = False
    End Select

    PageNumber += 1
End Sub
End Class

有人能帮帮我吗?我在寻找一种不用读文本文件就能进行双面打印的方法。我需要打印表单上的控件,而不是文本文件阅读器。每个表单都有足够的控件放在正确的位置,以完全填充页面。控件是图表和组框的位图,还有文本框和标签。仍然没有找到解决我问题的方法。有人能帮我吗?有人能帮我吗?我在寻找一种不用读文本文件就能进行双面打印的方法。我需要打印表单上的控件,而不是文本文件阅读器。每个表单都有足够的控件放在正确的位置,以完全填充页面。控件是图表和组框的位图,还有文本框和标签。仍然没有找到解决我问题的方法。有人能帮我吗?