Print Preview works but report won't print

I am working on an application which has several reports which are written to disk files and then previewed or printed.

The Print Preview of these reports is working perfectly however when I click the Print icon on the Print Preview toolbar the report fails to actually print.

I guess I a missing something rather fundamental here I would appreciate a sanity check of my code.

Private Sub printPreview(ByVal theTitle As String, ByVal theFile As String)
        Dim objPrint As New HSPrinter(theTitle, theFile)
        Dim previewDialog As New PrintPreviewDialog
        Dim pageSetup As New PageSetupDialog
        pageSetup.Document = objPrint.printDocument
 
        Dim dialogResult As DialogResult = pageSetup.ShowDialog
 
        If dialogResult = Windows.Forms.DialogResult.Cancel Then
            objPrint.Dispose()
            previewDialog.Dispose()
            pageSetup.Dispose()
            Exit Sub
        End If
 
        With previewDialog
            .Document = objPrint.printDocument
            .PrintPreviewControl.Zoom = 0.9
            .WindowState = FormWindowState.Maximized
            .ShowInTaskbar = True
            .ShowDialog()
        End With
 
        previewDialog.Dispose()
        pageSetup.Dispose()
        objPrint.Dispose()
 
    End Sub

Open in new window

KevinGaynorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
What is 'HSPrinter'?
0
KevinGaynorAuthor Commented:
HSPrinter contains the code to read the report in from a text file and create the document - this part seems to work fine as the print preview shows the document.
0
Bob LearnedCommented:
If you use a regular PrintDocument, will the print button work?
0
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

KevinGaynorAuthor Commented:
I'll give that a try next thank you.
0
Bob LearnedCommented:
Good luck!!  I am looking at your code, and there is nothing to explain why this is happening, but there are some unknowns that I am trying to "know".
0
KevinGaynorAuthor Commented:
Thanks for the help with this I haven't forgotten about it I've had another project jump the queue!!

The HSPrinter class code (HS is the prefix for the application in case you was wondering) is attached - this part works fine the preview is spot-on.




Imports System
Imports System.Drawing
Imports System.IO
Imports System.Drawing.Printing
Imports System.Windows.Forms
Public Class HSPrinter
    Inherits PrintDocument
    Private stringToPrint As String = ""
    Private fileToPrint As String = ""
    Public printDocument As New Printing.PrintDocument
    Public printerName As String = ""
    Public printerCopies As Integer = 1
    Public documentTitle As String = "Document"
    Public pageCount As Integer = 0
    Private printFont As New Font("Courier New", 9, FontStyle.Regular, GraphicsUnit.Point, CType(0, Byte))
 
    Public Sub New(ByVal theDocument As String, ByVal theFile As String)
        MyBase.New()
        Me.documentTitle = theDocument
        Me.fileToPrint = theFile
        Dim theLine As String = ""
        Dim theChar As String = ""
        Dim thePos As Integer = 0
        Dim thePage As Integer = 0
        Dim booNewPage As Boolean = False
 
        printDocument.DocumentName = documentTitle
        AddHandler printDocument.PrintPage, AddressOf printDocument_PrintPage
        Dim stream As New FileStream(fileToPrint, FileMode.Open)
        Try
            Dim reader As New StreamReader(stream)
            Try
                stringToPrint = reader.ReadToEnd
            Finally
                reader.Dispose()
            End Try
        Finally
            stream.Dispose()
        End Try
    End Sub
 
    Private Sub printDocument_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        MyBase.OnPrintPage(e)
        Dim charactersOnPage As Integer = 0
        Dim linesPerPage As Integer = 0
        Dim pageToPrint As String = ""
 
        pageCount += 1
 
        e.Graphics.MeasureString(stringToPrint, printFont, e.MarginBounds.Size, _
                StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
 
        pageToPrint = Mid(stringToPrint, 1, charactersOnPage)
 
        e.Graphics.DrawString(pageToPrint, printFont, Brushes.Black, _
            e.MarginBounds, StringFormat.GenericTypographic)
 
        stringToPrint = stringToPrint.Substring(charactersOnPage)
 
        e.HasMorePages = stringToPrint.Length > 0
    End Sub
 
End Class

Open in new window

0
Bob LearnedCommented:
Hmmm...you are inheriting from PrintDocument, and then including an internal PrintDocument instance.  I would think that if you are inheriting from PrintDocument, that the instance of the HSPrinter class should be sufficient.  Why do you have the extra PrintDocument instance?
0
KevinGaynorAuthor Commented:
"Why do you have the extra PrintDocument instance?"

It's just a legacy from my many attempts I will be tidying it up in due course.


0
Bob LearnedCommented:
Here is the code that the .NET framework calls:

Private Sub OnprintToolStripButtonClick(ByVal sender As Object, ByVal e As EventArgs)
    If (Not Me.previewControl.Document Is Nothing) Then
        Me.previewControl.Document.Print
    End If
End Sub

That would mean that you need to set the PrintPreviewControl property and the Document property for the PrintPreviewDialog component.  It doesn't look like you are setting the PrintPreviewControl property.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KevinGaynorAuthor Commented:
Thanks for all the help with this - I had to work on another project for a while but was able to revisit this one.

I traced the problem to my PrintPage sub in the routine that generates the PrintDocument.

I have attached the code.

Basically what I needed to do was reset the PageCount and also the contents of the stringToPrint variable which always ends up as an empty string each time the PrintPage is invoked.

What I failed to realise was when the Print button is clicked on the PrintPreview it invokes the same code - the second time through of course the string was empty and the page number was sitting on the final page!!

TheLearnedOne steered me towards this train of thought by providing me with the code for the print button - I began to wonder whether the documented needed to be reset somehow before printing 'for real' occured.

At the end of the PrintPage sub I put in:

If Not e.HasMorePages Then
   pageCount = 1
   stringToPrint = stringToPrintCopy
End If

And of course I save stringToPrint to stringToPrintCopy after the text file to print has been read in full.

Happy days.


Imports System
Imports System.Drawing
Imports System.IO
Imports System.Drawing.Printing
Imports System.Windows.Forms
Public Class HSPrinter
    Inherits PrintDocument
    Private stringToPrint As String = ""
    Private stringToPrintCopy As String = ""
    Private fileToPrint As String = ""
    Public printDocument As New Printing.PrintDocument
    Public printerName As String = ""
    Public printerCopies As Integer = 1
    Public documentTitle As String = "Document"
    Public pageCount As Integer = 0
    Private printFont As New Font("Courier New", 9, FontStyle.Regular, GraphicsUnit.Point, CType(0, Byte))
 
    Public Sub New(ByVal theDocument As String, ByVal theFile As String)
        MyBase.New()
        Me.documentTitle = theDocument
        Me.fileToPrint = theFile
        Dim theLine As String = ""
        Dim theChar As String = ""
        Dim thePos As Integer = 0
        Dim thePage As Integer = 0
        Dim booNewPage As Boolean = False
        printDocument.DocumentName = documentTitle
        AddHandler printDocument.PrintPage, AddressOf printDocument_PrintPage
        Dim stream As New FileStream(fileToPrint, FileMode.Open)
        Try
            Dim reader As New StreamReader(stream)
            Try
                stringToPrint = reader.ReadToEnd
            Finally
                stringToPrintCopy = stringToPrint
                reader.Dispose()
            End Try
        Finally
            stream.Dispose()
        End Try
    End Sub
 
    Private Sub printDocument_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
        MyBase.OnPrintPage(e)
        Dim charactersOnPage As Integer = 0
        Dim linesPerPage As Integer = 0
        Dim pageToPrint As String = ""
 
        pageCount += 1
 
        e.Graphics.MeasureString(stringToPrint, printFont, e.MarginBounds.Size, _
                StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
 
        pageToPrint = Mid(stringToPrint, 1, charactersOnPage)
 
        e.Graphics.DrawString(pageToPrint, printFont, Brushes.Black, _
            e.MarginBounds, StringFormat.GenericTypographic)
 
        stringToPrint = stringToPrint.Substring(charactersOnPage)
 
        e.HasMorePages = stringToPrint.Length > 0
 
        If Not e.HasMorePages Then
            pageCount = 1
            stringToPrint = stringToPrintCopy
        End If
    End Sub
 
End Class

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.