nbrege Posted February 14, 2006 Posted February 14, 2006 Suppose I have an array of 500 strings that I want to print out on multiple pages. I started with the code below, but I just cant figure out how to make it print the first pageful, invoke the .HasMorePages property & then continue printing the next page of items, and so forth, until all the elements in the array are printed. I have tried setting the .HasMorePages property to True when the print location reaches the bottom of the page, but then my loop starts over at the beginning again, giving the same output for all pages. I know I am close but I just can't figure how to do this. Can anyone help me get this working? Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage Dim printFont As New Font("Arial", 12) Dim lineHeightSingle As Single = printFont.GetHeight + 2 Dim horizontalPrintLocationSingle As Single = e.MarginBounds.Left Dim verticalPrintLocationSingle As Single = e.MarginBounds.Top ' Count pages for multiple-page output. Static pageCountInteger As Integer = 1 ' Print the page number. e.Graphics.DrawString("Page " & pageCountInteger.ToString(), printFont, Brushes.Black, 600, verticalPrintLocationSingle) verticalPrintLocationSingle = verticalPrintLocationSingle + lineHeightSingle * 2 For xInt As Integer = 1 To 500 ' Print a line. e.Graphics.DrawString("This is line " & xInt.ToString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle) ' Double space. verticalPrintLocationSingle = verticalPrintLocationSingle + lineHeightSingle * 2 Next End Sub Thanks for your help... Quote
tfowler Posted February 15, 2006 Posted February 15, 2006 Calculate number of lines per page You first have to figure out how many lines will fit on a printed page: Dim numberOfLinesPerPage As Integer = [size=2][color=#0000ff]CType[/color][/size][size=2](e.PageBounds.Height / lineHeightSingle, [/size][size=2][color=#0000ff]Integer[/color][/size][size=2]) [/size] Then you have to loop through your strings until you get to the last line that will fit on the page, save that spot, so you know where to start looping the next time through, and set .HasMorePages = True. When you reach the end of the 500 strings, set .HasMorePages = False. Todd Suppose I have an array of 500 strings that I want to print out on multiple pages. I started with the code below, but I just cant figure out how to make it print the first pageful, invoke the .HasMorePages property & then continue printing the next page of items, and so forth, until all the elements in the array are printed. I have tried setting the .HasMorePages property to True when the print location reaches the bottom of the page, but then my loop starts over at the beginning again, giving the same output for all pages. I know I am close but I just can't figure how to do this. Can anyone help me get this working? Private Sub PrintDocument2_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage Dim printFont As New Font("Arial", 12) Dim lineHeightSingle As Single = printFont.GetHeight + 2 Dim horizontalPrintLocationSingle As Single = e.MarginBounds.Left Dim verticalPrintLocationSingle As Single = e.MarginBounds.Top ' Count pages for multiple-page output. Static pageCountInteger As Integer = 1 ' Print the page number. e.Graphics.DrawString("Page " & pageCountInteger.ToString(), printFont, Brushes.Black, 600, verticalPrintLocationSingle) verticalPrintLocationSingle = verticalPrintLocationSingle + lineHeightSingle * 2 For xInt As Integer = 1 To 500 ' Print a line. e.Graphics.DrawString("This is line " & xInt.ToString, printFont, Brushes.Black, horizontalPrintLocationSingle, verticalPrintLocationSingle) ' Double space. verticalPrintLocationSingle = verticalPrintLocationSingle + lineHeightSingle * 2 Next End Sub Thanks for your help... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.