It would have been easier if documentation simply said it is up to the implementer to actually control the printrange based on the PrinterSettings.PrintRange, but neither Microsoft nor Infragistics shared that knowledge. So after digging around in TheCodeProject for printing articles, I learned that the reponsibility of controlling what pages actually are printed is a function of the implementer of a PrintPreview control. Perhaps the PrintPreviewDialog does this automatically, but if you are using the UltraPrintPreviewControl (found in the Infragistics.Win.Printing namespace). The following code snippets reflect use of the Document engine (not the UltraWinPrintDocument) used in conjunction with the PrintPreview control noted above. The code essentially indexes the ReportPages collection to get the specific page requested during the printing. The page to print is derived from the PrintRange property, the FromPage and ToPage if the property is SomePages, or the current preview page for CurrentPage.
Private Sub btnPrinterSetup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrinterSetup.Click Try Dim pd As System.Windows.Forms.PrintDialog = New System.Windows.Forms.PrintDialog With pd .AllowPrintToFile = False .PrinterSettings = upDocument.PrinterSettings .UseEXDialog = True If (Not myReportPages Is Nothing) Then .AllowSomePages = True .AllowCurrentPage = True .PrinterSettings.FromPage = 1 .PrinterSettings.ToPage = myReportPages.Count End If If (.ShowDialog() = Windows.Forms.DialogResult.OK) Then ' Validate the PrintRange if selected If (.PrinterSettings.PrintRange = Drawing.Printing.PrintRange.SomePages) Then If (.PrinterSettings.FromPage < 1) Then .PrinterSettings.FromPage = 1 If (.PrinterSettings.ToPage > myReportPages.Count) Then .PrinterSettings.ToPage = myReportPages.Count End If End If upDocument.PrinterSettings = .PrinterSettings upDocument.DefaultPageSettings.PrinterSettings = .PrinterSettings ' User clicked the print button upPreview.Print(True) End If End With Catch ex As Exception mobjParent.PostError(ex) End Try End Sub
''' <summary> ''' This handler is called after the page is printed. ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks>Use this to determine if there are more pages to print.</remarks> Private Sub upDocument_PagePrinted(ByVal sender As Object, ByVal e As Infragistics.Win.Printing.PagePrintedEventArgs) Handles upDocument.PagePrinted If (Not myReportPages Is Nothing) Then Dim LastPage As Integer With e.Document.PrinterSettings Select Case .PrintRange Case Drawing.Printing.PrintRange.SomePages ' Determine the last page number we need to print ' Note that printed pages are 1, 2, ... LastPage = .ToPage - .FromPage + 1 Case Drawing.Printing.PrintRange.CurrentPage ' There would be only one page printed Exit Sub Case Else ' This handles print all pages; we do not permit 'print selection' LastPage = myReportPages.Count End Select End With If (LastPage > e.Document.PageNumber) Then e.HasMorePages = True End If End If End Sub
''' <summary> ''' Handler called before the document page is printed ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> ''' <remarks>Choose the correct page based on the document page number and the PrintRange.</remarks> Private Sub upDocument_PagePrinting(ByVal sender As Object, ByVal e As Infragistics.Win.Printing.PagePrintingEventArgs) Handles upDocument.PagePrinting If (myReportPages Is Nothing) Then Exit Sub Try Dim PageNumber As Integer = e.Document.PageNumber Dim CurrentReportPage As Integer With e.Document.PrinterSettings Select Case .PrintRange Case Drawing.Printing.PrintRange.SomePages CurrentReportPage = .FromPage - 1 + PageNumber Case Drawing.Printing.PrintRange.CurrentPage CurrentReportPage = upPreview.CurrentPage Case Else ' This handles print all pages; we do not permit 'print selection' CurrentReportPage = PageNumber End Select End With ' Update the document footer to actual page number rather than relative page number upDocument.Footer.TextCenter = String.Format("Page {0} of {1}", CurrentReportPage, myReportPages.Count) ' Get the base-0 report page that is identified by the (base-1) page number Dim Page As Infragistics.Documents.Report.Projection.IProjectionPage = myReportPages(CurrentReportPage - 1)
myMetafile = GetPageAsMetafile(Page, 1.0) ' Get the page as a metafile ' We always draw the page at the origin e.Graphics.EnumerateMetafile(myMetafile, New Point(0, 0), metafileDelegate) Catch ex As Exception mobjParent.PostError(ex) End Try End Sub
Thank you for posting it. I've used it and works great.
Emanuel