更新时间:2023-11-30 15:35:04
你现在的代码基于打印到一个矩形: e.MarginBounds
对于测试,我使用了下面的代码:
Private pageNum As Integer
Private Sub Button1_Click(ByVal sender As Object,ByVal e As EventArgs)_
Handles Button1.Click
Dim sb As New StringBuilder
For i As Integer = 1 To 100
sb.AppendLine(i.ToString&)这是一行。)
下一个
stringToPrint = sb .ToString
$ b pageNum = 0
使用pvw作为新的PrintPreviewDialog()
pvw.Document = printDocument1
pvw.ShowDialog()
结束使用
End Sub
然后我改变你的例程在第一页上使用一个更小的矩形: p>
Private Sub printDocument1_PrintPage(ByVal sender As Object,_
ByVal e As PrintPageEventArgs)_
Handles printDocument1.PrintPage
Dim charactersOnPage As Integer = 0
Dim linesPerPage As Integer = 0
pageNum + = 1
Dim printSize As Size = e.MarginBounds.Size
如果pageNum = 1然后
printSize =新大小(printSize.Width,printSize.Height / 2)
End If
e.Graphics.MeasureString(stringToPrint,Me.Font,printSize,_
StringFormat.GenericTypographic,charactersOnPage,linesPerPage)
'在页面边界内绘制字符串
e.Graphics.DrawString(stringToPrint,Me.Font,Brushes.Black,_
New Rectangle(e.MarginBounds.Location,printSize),_
StringFormat.GenericTypographic)
删除已经打印的字符串部分。
stringToPrint = stringToPrint.Substring(charactersOnPage)
检查是否打印更多的页面。
e.HasMorePages = stringToPrint.Length> 0
End Sub
如果您需要控制基于字符的打印,去掉打印文本块的代码,并逐行打印所有内容。
So I am trying to force a page break while printing directly from my VB.NET program. I am basically using this code from MSDN to print my document:
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
This allows it to print fine, but I would like to put page breaks in specific spots. I've tried e.HasMorePages = true, but I don't understand how this allows me to break at specific locations. Say my stringToPrint is 5000 characters long, and I want to start a new page after 1000 characters, and then again after the next 2500 characters. How would I do this?
Also, changing linesOnPage and charactersOnPage to other values doesn't seem to change anything at all.
EDIT: I guess a little more info about what my program does will help. Basically what it's doing is the program will create about 4 full pages of data, and prints it to a .txt file. Now, I want to print the entire .txt file. The only way that I know how to do this is by printing a string, so I have it read the entire .txt file line by line and stores it all as one string (namely stringToPrint). Now, using the code above, I print stringToPrint.
Your current code is based on printing to a rectangle: e.MarginBounds
For testing, I used this code:
Private pageNum As Integer
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click
Dim sb As New StringBuilder
For i As Integer = 1 To 100
sb.AppendLine(i.ToString & ") This is a line.")
Next
stringToPrint = sb.ToString
pageNum = 0
Using pvw As New PrintPreviewDialog()
pvw.Document = printDocument1
pvw.ShowDialog()
End Using
End Sub
Then I changed your routine to use a smaller rectangle on the first page:
Private Sub printDocument1_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs) _
Handles printDocument1.PrintPage
Dim charactersOnPage As Integer = 0
Dim linesPerPage As Integer = 0
pageNum += 1
Dim printSize As Size = e.MarginBounds.Size
If pageNum = 1 Then
printSize = New Size(printSize.Width, printSize.Height / 2)
End If
e.Graphics.MeasureString(stringToPrint, Me.Font, printSize, _
StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
' Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _
New Rectangle(e.MarginBounds.Location, printSize), _
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
If you need to control the printing based on characters, you would have to ditch the code that prints the block of text and print everything on a line by line basis.