找到你要的答案

Q:How to print text files?

Q:如何打印文本文件?

I've created and saved a .txt file and now I'm truing to print it. The file could be big enough where multiple pages would be needed. Here is my code:

'Print file
Dim docName As String = name1 & " " & name2 & ".txt"
Dim docPath As String = "Z:\\Completed\"
printDocument1.DocumentName = docName
Dim stream As New FileStream(docPath & docName, FileMode.Open)
Try
    Dim reader As New StreamReader(stream)
    Try
        stringToPrint = reader.ReadToEnd()
    Finally
        reader.Dispose()
    End Try
Finally
    stream.Dispose()
End Try
printDocument1.Print()


Private Sub printDocument1_PrintPage(ByVal sender As Object, _
    ByVal e As PrintPageEventArgs)

    Dim charactersOnPage As Integer = 0
    Dim linesPerPage As Integer = 0

    ' Sets the value of charactersOnPage to the number of characters  
    ' of stringToPrint that will fit within the bounds of the page.
    e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _
        StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

    ' Draws the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _
        e.MarginBounds, StringFormat.GenericTypographic)

    ' Remove the portion of the string that has been printed.
    stringToPrint = stringToPrint.Substring(charactersOnPage)

    ' Check to see if more pages are to be printed.
    e.HasMorePages = stringToPrint.Length > 0

End Sub

My issue is that it prints a blank page every time. Stepping through the code I can see that "docName" and "docPath" are set correctly.

Example:

docName = Test 1.txt

docPath = Z:\Completed\

我已经创建并保存一个txt文件,现在我想把它打印。文件可能足够大,需要多个页面。这是我的代码:

'Print file
Dim docName As String = name1 & " " & name2 & ".txt"
Dim docPath As String = "Z:\\Completed\"
printDocument1.DocumentName = docName
Dim stream As New FileStream(docPath & docName, FileMode.Open)
Try
    Dim reader As New StreamReader(stream)
    Try
        stringToPrint = reader.ReadToEnd()
    Finally
        reader.Dispose()
    End Try
Finally
    stream.Dispose()
End Try
printDocument1.Print()


Private Sub printDocument1_PrintPage(ByVal sender As Object, _
    ByVal e As PrintPageEventArgs)

    Dim charactersOnPage As Integer = 0
    Dim linesPerPage As Integer = 0

    ' Sets the value of charactersOnPage to the number of characters  
    ' of stringToPrint that will fit within the bounds of the page.
    e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _
        StringFormat.GenericTypographic, charactersOnPage, linesPerPage)

    ' Draws the string within the bounds of the page
    e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _
        e.MarginBounds, StringFormat.GenericTypographic)

    ' Remove the portion of the string that has been printed.
    stringToPrint = stringToPrint.Substring(charactersOnPage)

    ' Check to see if more pages are to be printed.
    e.HasMorePages = stringToPrint.Length > 0

End Sub

我的问题是它每次打印一个空白页。逐句通过代码可以看出“docname”和“docpath”设置是否正确。

例子:

docname =测试1.txt

docpath = Z \完成\

answer1: 回答1:

The MSDN example is very unclear (the example code doesn't even show it), but you have to manually add the PrintPage event handler. Add this line after you declare printDocument1, but before you call printDocument1.Print():

AddHandler printDocument1.PrintPage, AddressOf printDocument1_PrintPage

Alternately, you could change your Sub declaration to:

Private Sub printDocument1_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage

Edit: To use this method, you have to declare printDocument1 using the WithEvents keyword, something like this:

Dim WithEvents printDocument1 As ...

MSDN的例子很清楚(示例代码甚至不显示出来),但你必须手动添加PrintPage事件处理程序。在你宣布printdocument1加入这一行,但在你打电话print() printdocument1:

AddHandler printDocument1.PrintPage, AddressOf printDocument1_PrintPage

或者,您可以将子声明更改为:

Private Sub printDocument1_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage

编辑:使用这种方法,你必须声明printdocument1使用WithEvents关键字,像这样的东西:

Dim WithEvents printDocument1 As ...
vb.net  file  printing