none
How do I get PrintPreviewDialog to show multiple pages?

    Question

  • Hello,

    I’m developing a Windows program using VS2010 and .NET 4.0.

    I want to preview the printing of a text file using the PrintPreviewDialog.ShowDialog method.

    The text being sent to the preview is 192 lines, but when the preview dialog box appears, it only shows one page of 60 lines.

    How do I get the print preview to show multiple pages?

    Any help would be gratefully appreciated.

    Thanks,
    Tony


    Stop The World, I want To Get Off!
    Monday, October 11, 2010 1:24 PM

Answers

  • How do I get the print preview to show multiple pages?

    Assuming that you have coded the PrintPage event of the PrintPreviewDialog.Document to show multiple pages, you can select the number of pages shown using the icons on the ToolStrip.  They have ToolTips.  You can select 1,2,3,4 or 6 pages.

    Not a BCL question.  Should be moved to Windows Forms forum.

    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 2:34 PM
  • It only shows one page no matter which icon i use in the PrintPreviewDialog screen.
    Consult with the coder of the PrintPreviewDialog.Document's PrintPage event.
    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 3:42 PM
  • That would be whoever did this: http://msdn.microsoft.com/en-us/library/system.windows.forms.printpreviewdialog.aspx
    That code only prints one line of text.  You'll need someone to code the PrintPage event to print what you want.
    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 4:03 PM
  • Actually it prints whatever is in "text".  "text" has 192 lines in it, but it only printpreviews and prints 60 lines(one page).

    This sentence seems to be a contradiction.

    Either get longer paper or only print what will fit on a page.  Print the remainder on succeeding pages.

    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 5:04 PM
  • You are the one who has to find the answer.  I can't see your code.  How are you measuring how much of the text that you want to print will fit on a page?  How are you printing only that text?  How do you determine when you have printed all of the pages?  You, that is you or a coder directed by you, must write code to print what you want where you want it.  Until I can find a way to see your actual code, that's all the advice I can offer.
    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 5:33 PM
  • Imports System.Drawing.Printing
    Public Class Form1
      
    Private Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button1.Click
        PrintPreviewDialog1.Document = PrintDocument1
        PrintDocument1.OriginAtMargins = 
    True
        PrepareTextForPrinting()
        PrintPreviewDialog1.ShowDialog()
      
    End Sub
      Dim TextToPrint As String
      Private Sub PrintDocument1_PrintPage(ByVal sender As ObjectByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
        
    Dim Chars, Lines As Integer
        e.Graphics.MeasureString(TextToPrint, Me.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, Chars, Lines)
        e.Graphics.DrawString(TextToPrint.Substring(0, Chars), 
    Me.Font, Brushes.Black, 0, 0)
        TextToPrint = TextToPrint.Substring(Chars)
        e.HasMorePages = TextToPrint.Length > 0
      
    End Sub
      Sub PrepareTextForPrinting()
        
    'Your text preparation code moved from the PrintPage event to here.
        For I As Integer = 0 To 200
          TextToPrint += 
    "Replace this For loop with the text you want to print." + Environment.NewLine
        
    Next
      End Sub
    End
     Class

    • Marked as answer by SamAgain Friday, November 05, 2010 7:43 AM
    Monday, October 11, 2010 6:34 PM
  • I can see that I needed to code some kind of loop in the PrintPage handler.  I will study that code to see exactly what it is doing.

    Is Environment.NewLine different than vbcrlf?

    ???

    What would the loop do?  Why can't you use the code I posted? 

    The code you had in the PrintPage event that I told you to remove and place in the PrepareTextForPrinting method should be the only thing you need to do.  The code I placed in the PrepareTextForPrinting method was only for test purposes. 

    Envirornment.NewLine is culture sensitive while vbcrlf is not.

    • Marked as answer by SamAgain Friday, November 05, 2010 7:43 AM
    Monday, October 11, 2010 10:11 PM

All replies

  • How do I get the print preview to show multiple pages?

    Assuming that you have coded the PrintPage event of the PrintPreviewDialog.Document to show multiple pages, you can select the number of pages shown using the icons on the ToolStrip.  They have ToolTips.  You can select 1,2,3,4 or 6 pages.

    Not a BCL question.  Should be moved to Windows Forms forum.

    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 2:34 PM
  • Hello John.

    It only shows one page no matter which icon i use in the PrintPreviewDialog screen.

    I think i need to use the show multiple pages you are referring to.  I'm not sure of how to set it.  Do i do it this way?

    document.PrinterSettings.PrintRange = Printing.

    PrintRange.AllPages

    Thanks,
    Tony


    Stop The World, I want To Get Off!
    Monday, October 11, 2010 3:20 PM
  • It only shows one page no matter which icon i use in the PrintPreviewDialog screen.
    Consult with the coder of the PrintPreviewDialog.Document's PrintPage event.
    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 3:42 PM
  • It only shows one page no matter which icon i use in the PrintPreviewDialog screen.
    Consult with the coder of the PrintPreviewDialog.Document's PrintPage event.


    Hello John.

    That would be whoever did this: http://msdn.microsoft.com/en-us/library/system.windows.forms.printpreviewdialog.aspx

    I used that as an example.

    Thanks,
    Tony


    Stop The World, I want To Get Off!
    Monday, October 11, 2010 3:57 PM
  • That would be whoever did this: http://msdn.microsoft.com/en-us/library/system.windows.forms.printpreviewdialog.aspx
    That code only prints one line of text.  You'll need someone to code the PrintPage event to print what you want.
    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 4:03 PM
  • Actually it prints whatever is in "text".  "text" has 192 lines in it, but it only printpreviews and prints 60 lines(one page).

    Thanks,
    Tony


    Stop The World, I want To Get Off!
    Monday, October 11, 2010 4:32 PM
  • Actually it prints whatever is in "text".  "text" has 192 lines in it, but it only printpreviews and prints 60 lines(one page).

    This sentence seems to be a contradiction.

    Either get longer paper or only print what will fit on a page.  Print the remainder on succeeding pages.

    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 5:04 PM
  • Actually it prints whatever is in "text".  "text" has 192 lines in it, but it only printpreviews and prints 60 lines(one page).

    This sentence seems to be a contradiction.

    Either get longer paper or only print what will fit on a page.  Print the remainder on succeeding pages.


    That's the problem i'm trying to resolve.  Longer paper won't help, we want it on 8 1/2 X 11" paper.

    Yes, that's what i want to do, print/printpreview on succeeding pages.

    You keep repeating what i am telling that i need help with.

    Please help me find an answer.

    Thanks,
    Tony


    Stop The World, I want To Get Off!
    Monday, October 11, 2010 5:23 PM
  • You are the one who has to find the answer.  I can't see your code.  How are you measuring how much of the text that you want to print will fit on a page?  How are you printing only that text?  How do you determine when you have printed all of the pages?  You, that is you or a coder directed by you, must write code to print what you want where you want it.  Until I can find a way to see your actual code, that's all the advice I can offer.
    • Marked as answer by SamAgain Friday, November 05, 2010 7:42 AM
    Monday, October 11, 2010 5:33 PM
  • OH.  OK.  Here is the code i am using.  Thanks for any help that you can provide.

     

    Friend WithEvents PrintPreviewDialog1 As PrintPreviewDialog
    Private WithEvents document As New System.Drawing.Printing.PrintDocument

    Private

     

    Sub printPreviewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printPreviewButton.Click

     

    If (TreeView1.SelectedNode IsNot Nothing) Then
    document.DocumentName = TreeView1.SelectedNode.Tag
    End If

    PrintPreviewDialog1.Document = document
    PrintPreviewDialog1.AutoSize =

    True
    PrintPreviewDialog1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
    PrintPreviewDialog1.ShowDialog()

     

    End Sub

     

    Private Sub InitializePrintPreviewDialog()
    Me.PrintPreviewDialog1 = New PrintPreviewDialog
    Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
    Me.PrintPreviewDialog1.Location = New System.Drawing.Point(29, 29)
    Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
    Me.PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250)
    Me.PrintPreviewDialog1.UseAntiAlias = True
    End Sub

     

    Private Sub document_PrintPage(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
    Handles document.PrintPage

    Dim summaryTable As DataTable
    summaryTable = DeliveryPaperworkDataSet.summaryDataTable
    Dim strExpr As String
    Dim strSort As String
    strExpr = "Type = 0"
    strSort = "Type"
    Dim text As String = ""
    Dim foundRows As DataRow() = summaryTable.Select(strExpr, strSort, DataViewRowState.CurrentRows)
    Dim printFont As New System.Drawing.Font("Arial", 10, System.Drawing.FontStyle.Regular)
    For i As Integer = 1 To DeliveryPaperworkDataSet.summaryDataTable.Rows.Count - 1

     

    If foundRows(i).Item(1).ToString = "1" Or foundRows(i).Item(1) = vbFormFeed Then
    text = vbFormFeed + text + foundRows(i).Item(2).ToString + vbCrLf
    Else
    text = text + foundRows(i).Item(2).ToString + vbCrLf
    End If

     

    Next

    e.Graphics.DrawString(text, printFont, System.Drawing.

    Brushes.Black, 0, 0)

     

    End Sub


    Stop The World, I want To Get Off!
    Monday, October 11, 2010 5:47 PM
  • Imports System.Drawing.Printing
    Public Class Form1
      
    Private Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button1.Click
        PrintPreviewDialog1.Document = PrintDocument1
        PrintDocument1.OriginAtMargins = 
    True
        PrepareTextForPrinting()
        PrintPreviewDialog1.ShowDialog()
      
    End Sub
      Dim TextToPrint As String
      Private Sub PrintDocument1_PrintPage(ByVal sender As ObjectByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
        
    Dim Chars, Lines As Integer
        e.Graphics.MeasureString(TextToPrint, Me.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, Chars, Lines)
        e.Graphics.DrawString(TextToPrint.Substring(0, Chars), 
    Me.Font, Brushes.Black, 0, 0)
        TextToPrint = TextToPrint.Substring(Chars)
        e.HasMorePages = TextToPrint.Length > 0
      
    End Sub
      Sub PrepareTextForPrinting()
        
    'Your text preparation code moved from the PrintPage event to here.
        For I As Integer = 0 To 200
          TextToPrint += 
    "Replace this For loop with the text you want to print." + Environment.NewLine
        
    Next
      End Sub
    End
     Class

    • Marked as answer by SamAgain Friday, November 05, 2010 7:43 AM
    Monday, October 11, 2010 6:34 PM
  • Thanks John.  That worked perfectly.

    I can see that I needed to code some kind of loop in the PrintPage handler.  I will study that code to see exactly what it is doing.

    Is Environment.NewLine different than vbcrlf?

    Thanks,
    Tony


    Stop The World, I want To Get Off!
    Monday, October 11, 2010 7:24 PM
  • I can see that I needed to code some kind of loop in the PrintPage handler.  I will study that code to see exactly what it is doing.

    Is Environment.NewLine different than vbcrlf?

    ???

    What would the loop do?  Why can't you use the code I posted? 

    The code you had in the PrintPage event that I told you to remove and place in the PrepareTextForPrinting method should be the only thing you need to do.  The code I placed in the PrepareTextForPrinting method was only for test purposes. 

    Envirornment.NewLine is culture sensitive while vbcrlf is not.

    • Marked as answer by SamAgain Friday, November 05, 2010 7:43 AM
    Monday, October 11, 2010 10:11 PM
  • John.

    Please disreagard my earlier post.  I really only meant to say that i needed to understand the changes you made to the PrintPage handler.

    Thanks again,
    Tony


    Stop The World, I want To Get Off!
    Tuesday, October 12, 2010 2:01 AM
  • Reading the content of this post is very entertaining. I enjoyed reading it and I also learned from it. Thank you for sharing this very useful post.

    Cara Beriklan di Internet Handphone STMIK AMIKOM menuju Research University
    Tuesday, December 28, 2010 1:11 PM