Home All Groups Group Topic Archive Search About

print selected pages from print preview

Author
5 Jun 2006 9:47 PM
Daniel
Hello Everyone,
I have to generate all the pages, because at run time we do not know which
records will be on each page since the report is databound, but how can I
send only selected pages to the printer?

Here are some snippets of code:

**************************************
Private Sub ReportDocument_BeginPrint(ByVal sender As Object, _
    ByVal e As System.Drawing.Printing.PrintEventArgs) Handles
MyBase.BeginPrint

        mPageNumber = 0
        mRow = 0

        If DataBound Then
            'some code
        End If

        If DataBound And GroupBy <> "" Then
            'some more code
        End If

        If mPreviewed Then
            'mPreviewed is set to true after the preview generation
            'I want to prevent showing the PrintDialog when generating the
preview
            'let the user select a printer and number of copies
            Dim prn As New PrintDialog
            prn.AllowSomePages = True
            prn.Document = Me

            If prn.ShowDialog() = DialogResult.Cancel Then
                e.Cancel = True
            End If
        End If
    End Sub

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

        mPageNumber += 1

        mFooterLineHeight = e.Graphics.MeasureString("Test",
FooterFont).Height
        mPageBottom = e.MarginBounds.Bottom
        mPageStart = e.MarginBounds.Left
        mWIdx = e.MarginBounds.Width / CallingForm.ClientRectangle.Width
        mHIdx = e.MarginBounds.Height / CallingForm.ClientRectangle.Height

        ' if we're generating page 1 raise the ReportBegin event
        If mPageNumber = 1 Then
            RaiseEvent ReportBeginA(Me, e)
        End If

        Try
            ' generate the page header/body/footer
            GeneratePage(e)

        Catch ex As Exception
            MessageBox.Show(ex.Message & ControlChars.CrLf & ex.StackTrace)
        End Try

        ' if there are no more pages to generate then raise
        ' the ReportEnd event
        If Not e.HasMorePages Then
            RaiseEvent ReportEndA(Me, e)
        End If
    End Sub

**************************************
Thank you,

Author
5 Jun 2006 9:58 PM
Daniel
I am sorry everyone,

The beggining of my question got cut off for some reason. Here it is:

I have a vb.net application, where I let the user generate a preview of a
databound report. When the user prints the whole report everything works
well, but I would like to be able to print only selected pages and I cannot
make it work.

Show quote
"Daniel" wrote:

> Hello Everyone,
> I have to generate all the pages, because at run time we do not know which
> records will be on each page since the report is databound, but how can I
> send only selected pages to the printer?
>
> Here are some snippets of code:
>
> **************************************
> Private Sub ReportDocument_BeginPrint(ByVal sender As Object, _
>     ByVal e As System.Drawing.Printing.PrintEventArgs) Handles
> MyBase.BeginPrint
>
>         mPageNumber = 0
>         mRow = 0
>
>         If DataBound Then
>             'some code
>         End If
>
>         If DataBound And GroupBy <> "" Then
>             'some more code
>         End If
>
>         If mPreviewed Then
>             'mPreviewed is set to true after the preview generation
>             'I want to prevent showing the PrintDialog when generating the
> preview
>             'let the user select a printer and number of copies
>             Dim prn As New PrintDialog
>             prn.AllowSomePages = True
>             prn.Document = Me
>
>             If prn.ShowDialog() = DialogResult.Cancel Then
>                 e.Cancel = True
>             End If
>         End If
>     End Sub
>
> Private Sub ReportDocument_PrintPage(ByVal sender As Object, _
>     ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles
> MyBase.PrintPage
>
>         mPageNumber += 1
>
>         mFooterLineHeight = e.Graphics.MeasureString("Test",
> FooterFont).Height
>         mPageBottom = e.MarginBounds.Bottom
>         mPageStart = e.MarginBounds.Left
>         mWIdx = e.MarginBounds.Width / CallingForm.ClientRectangle.Width
>         mHIdx = e.MarginBounds.Height / CallingForm.ClientRectangle.Height
>
>         ' if we're generating page 1 raise the ReportBegin event
>         If mPageNumber = 1 Then
>             RaiseEvent ReportBeginA(Me, e)
>         End If
>
>         Try
>             ' generate the page header/body/footer
>             GeneratePage(e)
>
>         Catch ex As Exception
>             MessageBox.Show(ex.Message & ControlChars.CrLf & ex.StackTrace)
>         End Try
>
>         ' if there are no more pages to generate then raise
>         ' the ReportEnd event
>         If Not e.HasMorePages Then
>             RaiseEvent ReportEndA(Me, e)
>         End If
>     End Sub
>
> **************************************
> Thank you,

AddThis Social Bookmark Button