posts - 114, comments - 204, trackbacks - 82

My Links

News

View Anthony Trudeau's profile on LinkedIn

Add to Technorati Favorites

Article Categories

Archives

Post Categories

Image Galleries

Other Links

Changing Printer Settings with PrintDocument

The System.Drawing.Printing.PrintDocument class has the Print mode which will print your document directly to the printer.  The Infragistics NetAdvantage suite has a couple of subclasses of PrintDocument.  One of them is the Infragistics.Win.UltraWinGrid.UltraGridPrintDocument which is designed to print the contents of a specific data grid.  The problem I ran into with the PrintDocument is that it prints to the default printer.  There is no prompt for a different printer.  However, as it turns out getting those details is very simple.

There are two ways to approach the problem.  You can have the user specify the settings and then use those settings for every call you make until the settings are specifically changed, or you can prompt when a print request is made.  I chose the latter, because it makes sense for my application and all you have to do is add a handler for the BeginPrint event of the PrintDocument.

private void ultraGridPrintDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)

{

    /* include this check otherwise the dialog will be

    * displayed when a print preview is shown */

    if (e.PrintAction == System.Drawing.Printing.PrintAction.PrintToPreview)

        return;

 

    using (PrintDialog dialog = new PrintDialog())

    {

        /* record the current printer settings for the document;

        * otherwise, the default system settings are used */

        dialog.PrinterSettings = ultraGridPrintDocument1.PrinterSettings;

 

        if (dialog.ShowDialog(this) == DialogResult.OK)

            ultraGridPrintDocument1.PrinterSettings = dialog.PrinterSettings;

        else

            e.Cancel = true;

    }

}

 

The Infragistics NetAdvantage suite also has a preview dialog named Infragistics.Win.Printing.UltraPrintPreviewDialog.  This class is similar to the one provided by the Framework, but it uses Infragistics controls and has a matching look-and-feel.  The solution provided also works for using the print preview dialog.  The one thing of note is that the print preview dialog allows the printer settings to be changed, but it's not direct (File --> Page Setup, click Printer, change settings, click OK).  I ignore this possibility and display the dialog, because it's not something that will be an issue to my users.

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Thursday, October 22, 2009 2:27 PM | Filed Under [ Infragistics .NET ]

Feedback

Gravatar

# re: Changing Printer Settings with PrintDocument

I never thought this can be possible.I really appreaciate your effort to write down all this information for everybody who needs the tip.
6/20/2011 12:18 PM | Printing Company
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: