在 vb.net 中打印多页

0 vb.net printing printdocument

如何打印多页?在我的表单中,我有带有相应标签的文本框,例如。(id、name、course 等)但问题是 1 页不足以显示所有文本框。我必须添加另一个页面来显示带有标签的剩余文本框。我试图将 e.hasmorepages 设置为 true,但第二页中出现的文本框与第一页中出现的文本框相同,它不会继续。

这是我的代码:

Private Sub printSisDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printSisDoc.PrintPage

    Dim labelFont As New Font("Arial", 11, FontStyle.Bold)
    Dim textFont As New Font("Arial", 11, FontStyle.Regular)
    Dim headerFont As New Font("Arial", 12, FontStyle.Bold)

    e.Graphics.DrawString(lblGrade.Text, headerFont, Brushes.Black, 650, 660)
    e.Graphics.DrawString(grade11.Text, textFont, Brushes.Black, 660, 690)
    e.Graphics.DrawString(underline.Text, labelFont, Brushes.Black, 643, 692)
    e.Graphics.DrawString(grade12.Text, textFont, Brushes.Black, 660, 715)
    e.Graphics.DrawString(grade13.Text, textFont, Brushes.Black, 660, 740)
    e.Graphics.DrawString(grade14.Text, textFont, Brushes.Black, 660, 765)
    e.Graphics.DrawString(grade15.Text, textFont, Brushes.Black, 660, 790)
    e.Graphics.DrawString(grade16.Text, textFont, Brushes.Black, 660, 815)
    e.Graphics.DrawString(grade17.Text, textFont, Brushes.Black, 660, 840)
    e.Graphics.DrawString(grade18.Text, textFont, Brushes.Black, 660, 865)
    e.Graphics.DrawString(grade19.Text, textFont, Brushes.Black, 660, 890)
    e.Graphics.DrawString(grade20.Text, textFont, Brushes.Black, 0, 1500)

    mPageNumber += 1

    e.HasMorePages = (mPageNumber <= 2)
End Sub
Run Code Online (Sandbox Code Playgroud)

Joe*_*orn 5

当您有多个页面时,您需要确保PrintPage()为您需要打印的每一页调用一次单一方法。每次调用该方法时,它都需要知道哪个页面是当前页面以及应该向该页面写入什么内容。

e.HasMorePages变量是如何使PrintDocument对象再次调用该方法。还要记住该printSisDoc_PrintPage()方法是类的一部分。您可以在类实例中设置数据,该方法可以使用该方法来了解当前页面和要打印的页面。

Private Sub printSisDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles printSisDoc.PrintPage

    Dim labelFont As New Font("Arial", 11, FontStyle.Bold)
    Dim textFont As New Font("Arial", 11, FontStyle.Regular)
    Dim headerFont As New Font("Arial", 12, FontStyle.Bold)

    Select mPageNumber
     Case 1
        e.Graphics.DrawString(lblGrade.Text, headerFont, Brushes.Black, 650, 660)
        e.Graphics.DrawString(grade11.Text, textFont, Brushes.Black, 660, 690)
        e.Graphics.DrawString(underline.Text, labelFont, Brushes.Black, 643, 692)
        e.Graphics.DrawString(grade12.Text, textFont, Brushes.Black, 660, 715)
        e.Graphics.DrawString(grade13.Text, textFont, Brushes.Black, 660, 740)
        e.Graphics.DrawString(grade14.Text, textFont, Brushes.Black, 660, 765)
        e.Graphics.DrawString(grade15.Text, textFont, Brushes.Black, 660, 790)
        e.Graphics.DrawString(grade16.Text, textFont, Brushes.Black, 660, 815)
        e.Graphics.DrawString(grade17.Text, textFont, Brushes.Black, 660, 840)
        e.Graphics.DrawString(grade18.Text, textFont, Brushes.Black, 660, 865)
        e.Graphics.DrawString(grade19.Text, textFont, Brushes.Black, 660, 890)
        e.HasMorePages = True

     Case 2

        e.Graphics.DrawString(grade20.Text, textFont, Brushes.Black, 0, 400)
        e.HasMorePages = False

    End Select

    mPageNumber += 1

End Sub
Run Code Online (Sandbox Code Playgroud)