none
How to print some pages of PrintDocument? RRS feed

  • Question

  •  

    Hi Friends,

     

    I need to provide the option for the user to specify the From & To Page to be printed.

     

    I have created the PrintDocument with multiple pages.

     

    A PrintDialog is also shown where the user can enter the From Page and To Page.

     

    I set the following

    PD.PrinterSetting.PrintRange.FromPages = PrintDialog.PrintRange.FromPages

    PD.PrinterSetting.PrintRange.ToPages = PrintDialog.PrintRange.ToPages

     

    I have also set

    PD.PrinterSetting.PrintRage = PrintRage.SomePages

    PrintDialog.PrinterSetting.PrintRage = PrintRage.SomePages

     

    Next, the command PD.print is given, but the PrintPage event is fired and all pages are again created and printed.

     

    How do I solve this problem?  Please help with some examples.

     

    Thanks,

    Krishi

    Bangalore

    Tuesday, April 1, 2008 5:14 AM

Answers

  • You can not draw until you reach the start page or clear the drawing when you reach the start page.  Here's a PrintPage example that skips drawing until the start page is reached:

    Code Snippet

    Private Sub PD_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD.PrintPage

    Dim Y As Integer

    Do While PageCounter < StartPage And LineCounter < TextBox1.Lines.Length - 1

    LineCounter += 1

    Y += TextBox1.Font.Height

    If Y > e.MarginBounds.Height Then

    Y = 0

    PageCounter += 1

    End If

    Loop

    Do While PageCounter < EndPage And LineCounter < TextBox1.Lines.Length - 1

    e.Graphics.DrawString(TextBox1.Lines(LineCounter), TextBox1.Font, Brushes.Black, 0, Y)

    LineCounter += 1

    Y += TextBox1.Font.Height

    If Y > e.MarginBounds.Height Then

    PageCounter += 1

    If PageCounter < EndPage Then e.HasMorePages = True

    Exit Do

    End If

    Loop

    End Sub

     

    Wednesday, April 2, 2008 8:54 AM

All replies

  • In the PrintDocument.PrintPage event, print only the requested pages.  You have to supply the code to do it.

    Tuesday, April 1, 2008 5:39 AM
  • Hi John,

     

    Thanks for the tips.

     

    I did read about passing the requested pages in PrintDocument.PrintPage event, but I am not sure about how to skip the pages that are not required to be printed.

     

    Example out of 10 pages, I need to print only pages 3 & 4. I could manage to prevent printing beyond page 4 but page 1 & 2 also gets printed.

    How to prevent printing the pages before the requested From Page.

     

    Thanks

    Krishi

     

    Tuesday, April 1, 2008 1:29 PM
  • Post your PrintPage event code.  You should have a counter for the pages.  The code in the PrintPage event is your code.  You should only print the requested pages.

    Tuesday, April 1, 2008 2:01 PM
  • Hi John,

     

    The following are the Codes in the PrintPage event handlers

     

    The following 2 handler are declared in the Load event of the Form.
     AddHandler PD.PrintPage, AddressOf PrintDate
     AddHandler PD.PrintPage, AddressOf MorePages

     

     Sub PrintData(ByVal sender As Object, ByVal e As PrintPageEventArgs)


      For i = 0 To DataTable.Rows.Count - 1  'for each row in the Data Table
     
       If Y > BottomMargin Then
          Call PrintPageFooter
          Y = TopMargin
          Exit Sub  'On Exit Sub, MorePages is triggered
       End If
      
       Select Case cond...
             Case 1 
                  g.DrawString(PrnStr, PrnFont, PrnBrush, X, Y, PrnFmt)
            
            
             Case 2
                  'Perform some calculation and print the result and also store the sum the result
                  '...
                  '...
                  g.DrawString(PrnStr, PrnFont, PrnBrush, X, Y, PrnFmt)
                 
                 
             Case 3
                  'Perform some calculation and print the result and also store the sum the result
                  '....
                 
             'This goes on for some more conditions
             
       End Select        
      
       Y = Y + PrnFont.GetHeight
            
      Next i
     
      Call PrintTotal

    End Sub 


    Sub MorePages(ByVal sender As Object, ByVal e As PrintPageEventArgs)
       
        If PageNo > EndPage - 1 Then  ' This prevents pages beyond the request page numbers from being printed  
         e.HasMorePages = False : Exit Sub
        End If
       
        if i < ParamTable.Rows.Count - 1 Then
           e.HasMorePages = True 
           PageNo += 1
           Call PrintData(sender, e)
        else
         e.HasMorePages = False
         Exit Sub
        End If

    End Sub 


    Private Sub BPrint_Click(ByVal sender As Object, _
                               ByVal e As System.EventArgs) _
                               Handles BPrint.Click
                              

       With CurrPrtDialog
        .Document = PD
        .AllowSomePages = True
        .AllowPrintToFile = False
        .AllowSelection = False
        .ShowNetwork = False
        .ShowHelp = False
        .PrintToFile = False
        .PrinterSettings.Copies = 1
        .PrinterSettings.Collate = True
        .PrinterSettings.FromPage = 1
        .PrinterSettings.ToPage = TotalPages
       
        If PD.PrinterSettings.DefaultPageSettings.Landscape = True Then
         .PrinterSettings.DefaultPageSettings.Landscape = True
        End If
       End With

       If (CurrPrtDialog.ShowDialog) = DialogResult.OK Then
        If CurrPrtDialog.PrinterSettings.PrintRange = PrintRange.SomePages Then
         StPage = CurrPrtDialog.PrinterSettings.FromPage
         EndPage = CurrPrtDialog.PrinterSettings.ToPage
         IsRange = True
         PD.PrinterSettings = CurrPrtDialog.PrinterSettings
         PD.PrinterSettings.FromPage = CurrPrtDialog.PrinterSettings.FromPage
         PD.PrinterSettings.ToPage = CurrPrtDialog.PrinterSettings.ToPage
         PD.Print()
         Exit Sub
        Else
         PD.PrinterSettings.PrintRange = PrintRange.AllPages
         PD.Print()
        End If
       Else 'Close the printing process
        Exit Sub
       End If
                              
    End Sub 

     

     

    I do have a counter to keep track of the Page Numbers. I don't know where to set the starting page number.

     

    Thanks

    Krishi

    Wednesday, April 2, 2008 7:16 AM
  • You can not draw until you reach the start page or clear the drawing when you reach the start page.  Here's a PrintPage example that skips drawing until the start page is reached:

    Code Snippet

    Private Sub PD_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD.PrintPage

    Dim Y As Integer

    Do While PageCounter < StartPage And LineCounter < TextBox1.Lines.Length - 1

    LineCounter += 1

    Y += TextBox1.Font.Height

    If Y > e.MarginBounds.Height Then

    Y = 0

    PageCounter += 1

    End If

    Loop

    Do While PageCounter < EndPage And LineCounter < TextBox1.Lines.Length - 1

    e.Graphics.DrawString(TextBox1.Lines(LineCounter), TextBox1.Font, Brushes.Black, 0, Y)

    LineCounter += 1

    Y += TextBox1.Font.Height

    If Y > e.MarginBounds.Height Then

    PageCounter += 1

    If PageCounter < EndPage Then e.HasMorePages = True

    Exit Do

    End If

    Loop

    End Sub

     

    Wednesday, April 2, 2008 8:54 AM