Results 1 to 8 of 8

Thread: Printing multipage documents

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Buckingham, England
    Posts
    197

    Printing multipage documents

    I am printing a page of text and graphics to the printer using grid references for drawing lines and positioning text. Now I have hit a problem, once I am at the bottom of the page how do I set up the second, third page etc. Do I continue referencing from the top/left corner of the first page or do I somehow create a second page? Any advice welcome does anyone know of a simple tutorial for this.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing multipage documents

    no. to print a second page, you set e.hasmorepages to true at the end of the PrintDocument_PrintPage event. this causes the PrintDocument_PrintPage event to run again after exiting. so for your second page your coordinates start at 0 again.

    obviously you need to setup some method for checking which page you're currently printing (within the PrintDocument_PrintPage event) + print the correct lines + text for that page

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Buckingham, England
    Posts
    197

    Re: Printing multipage documents

    e.hasmorepages = true is setting it back to the top of the page but instead of printing page 1 then filling page 2 it is overwriting page 1.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing multipage documents

    ok. post your code + i'll tell you where to change it.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Buckingham, England
    Posts
    197

    Re: Printing multipage documents

    To reduce the amount of code I need to post I have removed all the lines that fill each sheet. Hopefully what is left is sufficient to reflect how I am trying to do it. If not I will post the whole form.

    Code:
     Private Sub StockTake_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            RadioButton4.Checked = True
            StartPageBox.Text = "1"
            EndPageBox.Text = "Last"
        End Sub
        Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim MorePages As Boolean = False
            Dim TitleFont As Font
            TitleFont = New Font("Times New Roman", 20, FontStyle.Underline)
            Dim HeadingFont As Font
            HeadingFont = New Font("Arial", 12)
            Dim ContentFont As Font
            ContentFont = New Font("Times New Roman", 12)
            Dim ItemLocated As String = ""
            If RadioButton1.Checked = True Then
                ItemLocated = "Attic"
            ElseIf RadioButton2.Checked = True Then
                ItemLocated = "Home"
            ElseIf RadioButton3.Checked = True Then
                ItemLocated = "Post Office"
            End If
            Dim connection As New SqlConnection(PubConnString)
            Dim SQLdr As SqlDataReader
            Dim SQLStr As String
            If RadioButton4.Checked = True Then
                SQLStr = ("SELECT StockItems.ItemCode, ItemCodes.ItemName, WhereLocated from " _
                     & "StockItems INNER JOIN Itemcodes on ItemCodes.ItemCode=StockItems.ItemCode order by StockItems.ItemCode")
            Else
                SQLStr = ("SELECT StockItems.ItemCode, ItemCodes.ItemName, WhereLocated from " _
                     & "StockItems INNER JOIN Itemcodes on ItemCodes.ItemCode=StockItems.ItemCode " _
                     & "where  WhereLocated = @ItemLocated order by StockItems.ItemCode, WhereLocated")
            End If
            Dim SQLConn As New SqlConnection()
            Dim SQLCmd As New SqlCommand()
            SQLConn.ConnectionString = PubConnString
            SQLConn.Open()
            SQLCmd.Connection = SQLConn
            SQLCmd.CommandText = SQLStr
            SQLCmd.Parameters.AddWithValue("@ItemLocated", ItemLocated)
            SQLdr = SQLCmd.ExecuteReader()
            Dim FirstRecord As Boolean = True
            Dim CurrentItemCode As String = ""
            Dim CurrentLocation As String = ""
            Dim ItemCount As Integer = 0
            Dim RowPosition As Integer = 100
            While SQLdr.Read()
                If FirstRecord = True Then
                    Dim CurrentDate As Date = Date.Now
                    Dim StocktakeDate As String
                    StocktakeDate = CurrentDate.ToString("dd/MM/yyyy")
    
                End If
            End While
            'Process last item
            If CurrentLocation = "Attic" Then
                e.Graphics.DrawString(ItemCount, ContentFont, Brushes.Black, 482, RowPosition)
            Else
                If CurrentLocation = "Home" Then
                    e.Graphics.DrawString(ItemCount, ContentFont, Brushes.Black, 532, RowPosition)
                Else
                    e.Graphics.DrawString(ItemCount, ContentFont, Brushes.Black, 582, RowPosition)
                End If
            End If
            If MorePages = True Then
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
        End Sub
    
        Private Sub StartPageBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles StartPageBox.KeyPress
            If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
            If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
            If e.KeyChar = Chr(13) Then StartPageBox.Focus()
        End Sub
        Private Sub EndPageBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles EndPageBox.KeyPress
            If EndPageBox.Text = "Last" Then EndPageBox.Text = ""
            If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
            If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
            If e.KeyChar = Chr(13) Then EndPageBox.Focus()
        End Sub
    
        Private Sub FirstPageButton_Click(sender As System.Object, e As System.EventArgs) Handles FirstPageButton.Click
            StartPageBox.Text = "1"
            Label4.Visible = False
        End Sub
    
        Private Sub LastPageButton_Click(sender As System.Object, e As System.EventArgs) Handles LastPageButton.Click
            EndPageBox.Text = "Last"
            Label4.Visible = False
        End Sub
    
        Private Sub ContinueButton_Click(sender As System.Object, e As System.EventArgs) Handles ContinueButton.Click
            If EndPageBox.Text <> "Last" Then
                If CInt(StartPageBox.Text) > CInt(EndPageBox.Text) Then
                    Label4.Visible = True
                    GoTo restart
                End If
            End If
            Dim PrintDialog1 As New PrintDialog()
            PrintDialog1.Document = PrintDocument1
            Dim result As DialogResult = PrintDialog1.ShowDialog
            If (result = Windows.Forms.DialogResult.OK) Then
                PrintPreviewDialog1.Document = PrintDocument1
                PrintPreviewDialog1.ShowDialog()
                PrintDocument1.Print()
            End If
        End Sub

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing multipage documents

    your code is complicated by the db. here's a template for how you should approach your printing:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
    4.         'read your db into a datatable here
    5.         'we'll call it dt
    6.     End Sub
    7.  
    8.     Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    9.         Static startAt As Integer = 0
    10.         Dim startY As Integer = CInt(PrintDocument1.DefaultPageSettings.PrintableArea.Top)
    11.  
    12.         'you can put additional code here
    13.  
    14.         For x As Integer = startAt To dt.rows.count - 1
    15.             'print your fields
    16.             'increment startY
    17.  
    18.             If startY > PrintDocument1.DefaultPageSettings.PrintableArea.Height Then
    19.                 startAt = x + 1
    20.                 If startAt < dt.rows.count Then
    21.                     e.HasMorePages = True
    22.                     Return
    23.                 Else
    24.                     startAt = 0
    25.                 End If
    26.             End If
    27.         Next
    28.         'don't put any additional code here
    29.     End Sub
    30.  
    31. End Class

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2011
    Location
    Buckingham, England
    Posts
    197

    Re: Printing multipage documents

    Thanks Paul, not sure why my original code wasn't working but it is now working fine and a lot less complicated.
    How can I control what pages are to be printed. The printdialog box 'From page' and 'To page' doesn't appear to be active. I have created 2 text boxes on the form so the user can select a start and end page but I am struggling with the From Page. The To page is easy I just set hasmorepages to false.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Printing multipage documents

    The printdialog box 'From page' and 'To page' is just for choosing which pages to print. you have to code the actual printing yourself.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width