שתף באמצעות


PrintPreviewDialog Multi-page printing doesn't work

Question

Tuesday, March 20, 2007 2:35 AM

Hello..

I've written a vb.net in vs2005 program that outputs printed data over multiple pages.  The printing code generation works fine and I can view all the pages inside of the print preview dialog.  Furthermore, if I bypass the print preview and execute the printdocument's printpage event, ALL pages print correctly to the printer.  However, if I choose to print directly from the Print button of the preview control, it only prints the first page, and even that page it doesn't print correctly (even though it displayed correctly in the preview display). 

I am trying to figure out if there is a certain property that isn't set correctly (by default?) or needs to be set before the preview control's print functionality works correctly.  I've searched and searched through the reference help on the control and cannot find a single reference to the "print button" that is contained inside the preview dialog.  It would seem logical to me that if there are 10 pages that are viewable in the preview screen, and you then hit the print button, that the next step is for all 10 pages to be sent to the default printer, just as it did correctly when I bypassed the preview control.

Any answers/solutions to this issue would be greatly appreciated!

swpod1959

 

 

All replies (3)

Thursday, April 5, 2007 4:16 AM ✅Answered

swpod1959, GroundWalker, Anonymous,

 

According to your description, I checked the bug site in Microsoft website, however, there is no related information on PrintPreviewDialog Multi-page printing.

 

I searched MSDN and found an article titled How to: Print n Windows Forms Using Print Preview and tried the sample code. In the application, I can preview the multipages text file, then printing the pages just work fine. I recommend you to read the article here:

 

http://msdn2.microsoft.com/zh-cn/library/ms404294.aspx

 

There is the sample code:

 

Code Snippet

Imports System

Imports System.Drawing

Imports System.IO

Imports System.Drawing.Printing

Imports System.Windows.Forms

 

Class Form1

    Inherits Form

 

    Private WithEvents printPreviewButton As Button

 

    Private printPreviewDialog1 As New PrintPreviewDialog()

    Private WithEvents printDocument1 As New PrintDocument()

 

    ' Declare a string to hold the entire document contents.

    Private documentContents As String

 

    ' Declare a variable to hold the portion of the document that

    ' is not printed.

    Private stringToPrint As String

 

    Public Sub New()

        Me.printPreviewButton = New System.Windows.Forms.Button()

        Me.printPreviewButton.Location = New System.Drawing.Point(12, 12)

        Me.printPreviewButton.Size = New System.Drawing.Size(125, 23)

        Me.printPreviewButton.Text = "Print Preview"

        Me.ClientSize = New System.Drawing.Size(292, 266)

        Me.Controls.Add(Me.printPreviewButton)

 

    End Sub

 

    Private Sub ReadDocument()

        Dim docName As String = "testPage.txt"

        Dim docPath As String = "c:\

        printDocument1.DocumentName = docName

        Dim stream As New FileStream(docPath + docName, FileMode.Open)

        Try

            Dim reader As New StreamReader(stream)

            Try

                documentContents = reader.ReadToEnd()

            Finally

                reader.Dispose()

            End Try

        Finally

            stream.Dispose()

        End Try

        stringToPrint = documentContents

 

    End Sub

 

    Sub printDocument1_PrintPage(ByVal sender As Object, _

        ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage

 

        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

 

        ' If there are no more pages, reset the string to be printed.

        If Not e.HasMorePages Then

            stringToPrint = documentContents

        End If

 

    End Sub

 

    Private Sub printPreviewButton_Click(ByVal sender As Object, _

        ByVal e As EventArgs) Handles printPreviewButton.Click

 

        ReadDocument()

        printPreviewDialog1.Document = printDocument1

        printPreviewDialog1.ShowDialog()

    End Sub

 

    <STAThread()> _

    Shared Sub Main()

        Application.EnableVisualStyles()

        Application.SetCompatibleTextRenderingDefault(False)

        Application.Run(New Form1())

 

    End Sub

End Class

 

 


Thursday, March 22, 2007 9:07 PM

I had the same problem.  What I didn't realize is that when you click the print button, the printing process starts again and the document is regenerated.  All variables used to control the building of the document have to be reinitialized.


Saturday, March 31, 2007 11:14 AM

HI

I have also similar problem. After previewing u needs to reset all ur variables that being used to generate preview and only then the print button from print preview will be able to print all pages as is being displayed in preview.

Do u tried showing printerdialog from printpreviewcontrol that shows printer list, print to file options and page range selection among other options. if so I want to ask do u be able to implement From/to Page range [also setting allowsomepages=true]. if so please tell me how?

Thanks