5

I am trying to modify the System.Drawing.Printing.PrinterSettings object that I get from the System.Windows.Forms.PrintDialog after the dialog has been shown to the user. Although I am able to change property values on the PrinterSettings object, none of the changes that I make after the dialog has been shown is actually considered when printing a document.

Here is an example of what I mean:

//Show the printdialog and retreive the printersettings    
var printDialog = new PrintDialog();
if (printDialog.ShowDialog() != DialogResult.OK) 
            return;
var printerSettings = printDialog.PrinterSettings;

//Now modify the printersettings object
printerSettings.ToPage = 8;

Now use the printerSettings object for printing. I use the 3rd Party dll Aspose.Words for this, since I need to print Word, but this seems not to be the problem. It seems like that after the dialog has been shown all settings have already been commited to the Printer and changing the PrinterSettings achieves nothing. Any Ideas on how to get this to work?

EDIT: I have some workarounds for this. What I want here is to get an answer to these specific questions: Is it possible to change the PrinterSettings object after the dialog has been shown and are these changes considered in printing. If someone knows only one way of how this can work (you can decide on what API you want to use for printing, it does not matter as long as the PrinterSettings object is used), I would be very thankful.

4
  • Any reasons that you don't change settings before opening the dialog?
    – Arnaud F.
    Mar 14, 2012 at 15:14
  • Yes, in my application I have "merged documents", that consist of many different documents. In the print dialog they should appear as one document but for printing I need to handle them individually. So to be more precise: Some of the PrinterSettings should apply for all Documents, others I need to set for each Document individually. The individual properties/settings I can calculate without user interaction but for printing I need to inject those into the printer settings. Mar 14, 2012 at 15:19
  • It is completely unclear from the snippet how the printerSettings object is being applied to the actual print job. Also having that missing in your code explains the cause of the problem. Mar 14, 2012 at 15:30
  • No, it does not. Please read my post not only the code. I use Aspose for printing but the underlying problem is independent of Apsose. Mar 14, 2012 at 15:46

2 Answers 2

2

A few things I've noticed with PrintDialog (which may or may not answer your question).

First thing is that it is just a wrapper class for the windows com dialogue.

[DllImport("comdlg32.dll", CharSet=CharSet.Auto, SetLastError=true)]
        public static extern bool PrintDlg([In, Out] NativeMethods.PRINTDLG lppd);

and second, and most important with reference to you question I guess: The PrintDialog class has this routine which is called after closing of the PrintDlg call

if (!UnsafeNativeMethods.PrintDlg(data))
                return false;

            IntSecurity.AllPrintingAndUnmanagedCode.Assert();
            try { 
                UpdatePrinterSettings(data.hDevMode, data.hDevNames, data.nCopies, data.Flags, settings, PageSettings); 
            }
            finally { 
                CodeAccessPermission.RevertAssert();
            }

. . .

// VSWhidbey 93449: Due to the nature of PRINTDLGEX vs PRINTDLG, separate but similar methods 
// are required for updating the settings from the structure utilized by the dialog.
// Take information from print dialog and put in PrinterSettings
private static void UpdatePrinterSettings(IntPtr hDevMode, IntPtr hDevNames, short copies, int flags, PrinterSettings settings, PageSettings pageSettings) {
        // Mode 
        settings.SetHdevmode(hDevMode);
        settings.SetHdevnames(hDevNames); 

        if (pageSettings!= null)
            pageSettings.SetHdevmode(hDevMode); 

        //Check for Copies == 1 since we might get the Right number of Copies from hdevMode.dmCopies...
        //this is Native PrintDialogs
        if (settings.Copies == 1) 
            settings.Copies = copies;

        settings.PrintRange = (PrintRange) (flags & printRangeMask); 
    }

There is also a rather interesting interplay here (bearing in mind you set PrinterSettings.ToPage):

public PrinterSettings PrinterSettings {
   get { 
        if (settings == null)
        {
            settings = new PrinterSettings(); 
        }
        return settings; 
    } 
    set {
        if (value != PrinterSettings) 
        {
            settings = value;
            **printDocument = null;**
        } 
    }
} 

and then

public PrintDocument Document {
            get { return printDocument;}
            set {
                printDocument = value; 
                **if (printDocument == null)
                    settings = new PrinterSettings();** 
                else 
                    settings = printDocument.PrinterSettings;
            } 
        }

Not a direct answer I know, but I think should point you in the right direction of why it is not working. Seems to me that during the dialogue's use, it can happily nullify the settings on changes as it will be recreated on completion, but when the Dialogue is complete, changing settings actually invalidates the document print settings until it is set again. It may be possible to do this manually, or it may be loch\ked by M$ in the usual internal/private way many internals are.

There is an option of course (not as nice I know) to just use the Win API diectly after the call - code could be hythed from the above dialgues to build your own wrapper if required.

2
  • I haven't checked this for a while and therefore I am a little late with my replay: Thank you very much. Jun 29, 2012 at 14:16
  • And btw. I think the question got a downvote because some people on this website have a tendency not to take into consideration the real issue and content of the question. There are not willing to acknowledge that the author has more than only a superfical idea of programming. With regards to this question the easy route to take is to think I made some mistake in some other part of the code and the issue is in reallity not existing. And sorry, I have not enough credits or points or whatever to upvote your answer. Jun 29, 2012 at 14:26
1

From Aspose documentation:

AsposeWordsPrintDocument awPrintDoc = new AsposeWordsPrintDocument(doc);
awPrintDoc.PrinterSettings = printDlg.PrinterSettings;

So it seems that you can pass yuor modified PrinterSettings object to the word document you are trying to print. Could you tell me if this works?

1
  • Hi Steve, the result is the same. Only the settings from the printDialog are considered. Not the manual changes. I now follow a different approach and convert all the documents to xps (with Aspose), merge them into one xps, and then print this one xps (or the selected pages) by using the wpf printdialog. Mar 16, 2012 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.