在 vb.net 中打印多页

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34134441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:37:30  来源:igfitidea点击:

Print multiple pages in vb.net

vb.netprintingprintdocument

提问by jane

How can i print multiple pages? In my form i have textboxes with their corresponding labels eg. (id, name, course, etc.) but the problem is 1 page is not enough to display all textboxes. i have to add another page to display the remaining textboxes with their labels. I tried to set e.hasmorepages to true but the textboxes appears in the second page are just the same with the first page it does not continue.

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

Here is my code :

这是我的代码:

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

回答by Joel Coehoorn

When you have more than one page, you need to ensure the single PrintPage()method is called once for everypage you need to print. Each time the method is called, it needs to know which page is current and what should be written to that page.

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

The e.HasMorePagesvariable is how you make the PrintDocumentobject call the method again. Also remember the printSisDoc_PrintPage()method is part of a class. You can set data in the class instance the method can use to know what page is current and what to print.

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