以编程方式在vb.net中插入分页符 [英] Programmatically insert page break in vb.net
问题描述
pre $ Private $ private printDocument1_PrintPage(ByVal sender As Object,_
ByVal e As PrintPageEventArgs)
Dim charactersOnPage As Integer = 0
Dim linesPerPage As Integer = 0
'将charactersOnPage的值设置为字符数$ stringToPrint的b $ b'将适合页面的边界。
e.Graphics.MeasureString(stringToPrint,Me.Font,e.MarginBounds.Size,_
StringFormat.GenericTypographic,charactersOnPage,linesPerPage)
'在边界内绘制字符串
e.Graphics.DrawString(stringToPrint,Me.Font,Brushes.Black,_
e.MarginBounds,StringFormat.GenericTypographic)
'删除部分已打印的字符串。
stringToPrint = stringToPrint.Substring(charactersOnPage)
检查是否打印更多的页面。
e.HasMorePages = stringToPrint.Length> 0
结束Sub
这样可以很好地打印,但我会喜欢在特定位置放置分页符。我已经尝试过e.HasMorePages = true,但我不明白这是如何让我在特定的位置打破。说我的stringToPrint是5000个字符长,我想在1000个字符后开始一个新的页面,然后在下一个2500个字符后再次开始。我怎样才能做到这一点?
另外,将linesOnPage和charactersOnPage更改为其他值并不会改变任何内容。
编辑:我想我的程序会帮助更多的信息。基本上它所做的是程序将创建大约4个完整的数据页面,并将其打印到.txt文件中。现在,我想打印整个.txt文件。我知道如何做到这一点的唯一方法是通过打印一个字符串,所以我已经逐行阅读整个.txt文件,并将其全部存储为一个字符串(即stringToPrint)。现在,使用上面的代码,我打印出stringToPrint。
你现在的代码基于打印到一个矩形: 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.
这篇关于以编程方式在vb.net中插入分页符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!