Results 1 to 3 of 3

Thread: VS 2010 PrintDocument Printing to non-default printer

  1. #1

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    VS 2010 PrintDocument Printing to non-default printer

    Something quick. I am trying to print many pages to a printer which is not the default printer.

    Here's the code called when the user clicks Print:

    Code:
            private void GeneratePrint()
            {
                PrintDocument document = new PrintDocument();
                document.BeginPrint += new PrintEventHandler(this.printDocument1_BeginPrint);
                document.QueryPageSettings += new QueryPageSettingsEventHandler(document_QueryPageSettings);
                document.PrintPage += new PrintPageEventHandler(this.printDocument1_PrintPage);
                document.DefaultPageSettings = this.pgSettings;
                this.printDialog1.Document = document;
                this.printDialog1.AllowSomePages = true;
                if (this.printDialog1.ShowDialog() == DialogResult.OK)
                {
                    document.PrinterSettings = this.printDialog1.PrinterSettings;
                    document.Print();
                    return;
                    //Other code not relevant
                }
            }
    The event handlers for BeginPrint and PrintPage are as follows (QueryPrintSettings handler is empty)

    Code:
            private void printDocument1_BeginPrint(object sender, PrintEventArgs e)
            {
                this.LoadLayoutData();
    
                PrinterSettings pSettings = ((PrintDocument)sender).PrinterSettings;
                //Some other code
            }
    Here's the PrintPage:

    Code:
            private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
            {
                //Prepares data for printing this page
                if (pageData[0].IsTopSheet == "True")
                {
                    //this.printDocument1.DefaultPageSettings.PaperSource = this.GetPaperSourceForPrinter(this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName, "Top Sheet");
                    e.PageSettings.PaperSource = this.GetPaperSourceForPrinter(e.PageSettings.PrinterSettings.PrinterName, "Top Sheet");
                    this.GeneratePrintOutput(e.Graphics, e.PageSettings, pageData, this.TopsheetLayoutSettings, nudgeLeft, nudgeTop);
                }
                else if (pageData[0].IsBottomSheet == "True")
                {
                    //this.printDocument1.DefaultPageSettings.PaperSource = this.GetPaperSourceForPrinter(this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName, "Bottom Sheet");
                    e.PageSettings.PaperSource = this.GetPaperSourceForPrinter(e.PageSettings.PrinterSettings.PrinterName, "Bottom Sheet");
                }
                else
                {
                    //this.printDocument1.DefaultPageSettings.PaperSource = this.GetPaperSourceForPrinter(this.printDocument1.DefaultPageSettings.PrinterSettings.PrinterName, pageData[0].CommodityCode);
                    e.PageSettings.PaperSource = this.GetPaperSourceForPrinter(e.PageSettings.PrinterSettings.PrinterName, pageData[0].CommodityCode);
                    this.GeneratePrintOutput(e.Graphics, e.PageSettings, pageData, this.VoucherLayoutSettings, nudgeLeft, nudgeTop);
                }
                this.currentPage++;
                e.HasMorePages = this.currentPage <= e.PageSettings.PrinterSettings.ToPage; // this.TotalPages;
            }
    Now the default printer on my machine is an HP model. Since I am testing out the code, I want to print to a non-default printer, which is "CutePDF". I don't want to make this the default printer.

    On clicking Print, the print dialog opens fine, and I am able to select CutePDF printer and set other settings. When I click Print from the print dialog, the BeginPrint and the PrintPage events fire in the correct order. When I check the BeginPrint event arguments, the "sender" property correctly points to the CutePDF printer. When I check the PrintPage event arguments, the "sender" property again correctly points to the CutePDF printer, but the "PrintEventArgs e" property still refers to the default HP printer. I am worried this will mess up my printing since I am trying to do some precision printing here, and printer settings would be important.

    Why is this happening and how can this be rectified?

    Thanks in advance.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  2. #2
    Frenzied Member HanneSThEGreaT's Avatar
    Join Date
    Nov 2003
    Location
    Vereeniging, South Africa
    Posts
    1,492

    Re: VS 2010 PrintDocument Printing to non-default printer

    Where do you set the value of PageSettings.PrinterSettings.PrinterName ?
    That will remain as the default printer, unless it has been changed.
    VB.NET MVP 2008 - Present

  3. #3

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Re: VS 2010 PrintDocument Printing to non-default printer

    The sequence of events goes (or should go) thus:

    I create a PrintDocument (which has an empty/null PageSettings property)
    I assign the PrintDocument instance to the PrintDialog and show the PrintDialog to the user
    User makes changes in the PrintDialog such as select printer, select number of pages to print etc.
    After user clicks Print in the PrintDialog, the PrintDocument.PageSettings should now reflect whatever the user has selected.
    So far so good.
    The PrintDocument has the BeginPrint and Print_Page event handlers assigned. The moment user clicks Print, the BeginPrint event handler is called - I do my print initialization here.
    After BeginPrint, for ever page being printed, the PrintPage event is called. This event takes two arguments: sender - the object which generated this event, and PrintEventArgs which contains more information about the printing - such as the page settings etc. and most importantly the graphics object for the printer.

    Now in the PrintPage event, when I check the Sender object, it shows the printer selected by the user by default, but when I examine PrintEventArgs, it always shows the default printer configured in Windows. This means the correct printer object raised the event, but the graphics object which I have to use is of a different printer.

    After associating the PrintDocument to the PrintDialog, when the user clicks Print, I check the code and sure enough, PrintDocument.PageSettings.PrinterSettings.PrinterName reflects the printer selected by the user. So I am not sure there's anything wrong in my code.

    For now, I have simply set whichever printer I want to print to as the default printer and things are going ok. However this still bugs me a lot.
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width