Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何打印文本文件?_Vb.net_File_Printing - Fatal编程技术网

Vb.net 如何打印文本文件?

Vb.net 如何打印文本文件?,vb.net,file,printing,Vb.net,File,Printing,我已经创建并保存了一个.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 & d

我已经创建并保存了一个.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=Test 1.txt

docPath=Z:\Completed\

非常不清楚(示例代码甚至没有显示),但您必须手动添加PrintPage事件处理程序。在声明
printDocument1
之后,但在调用
printDocument1.Print()之前,添加此行:

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

Private Sub printDocument1_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage
Edit:要使用此方法,必须使用关键字声明
printDocument1
,如下所示:

Dim WithEvents printDocument1 As ...

您使用的是
printDocument1.Print()
,但您正在将文件加载到
stringToPrint
变量中。另外,您的第二个代码段似乎不相关,因为它从未被调用(从您所展示的内容)。@JustinRyan据我所知,
printDocument1.Print()
调用了第二个代码段,然后打印了
stringToPrint
中的内容。在我的代码中,我可以看到第二个代码段从未被调用过,但我不知道该怎么做。如何打印
stringToPrint
中的内容?另外,打印多页文档是否不需要第二个代码段?我在更改子声明时收到以下错误:“Handles子句需要在包含类型或其基类型之一中定义WithEvents变量。”尽管如此,您提供的第一段代码仍然非常有效@Mikemetzer对此表示抱歉。我更新了答案以澄清问题。
Dim WithEvents printDocument1 As ...