Changing Printer Settings After Displaying PrintDialog

I am trying to modify the System.Drawing.Printing.PrinterSettings object that I get from System.Windows.Forms.PrintDialog after the dialog has been shown to the user. Although I can change the property values ​​in the PrinterSettings object, none of the changes that I make after the dialog was shown were actually considered when the document was printed.

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 to print. I am using a third-party Aspose.Words dll for this, since I need to print Word, but this does not seem to be a problem. It seems that after the dialog was shown, all the settings have already been sent to the printer, and changing the PrinterSettings does not give anything. Any ideas on how to make this work?

EDIT: . : PrinterSettings , , . - , ( , API , , PrinterSettings), .

+5
2

, , ????

, , PrintDialog ( ).

, - windows com.

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

-, , : PrintDialog , PrintDlg

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); 
    }

( , PrinterSettings.ToPage):

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

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

, , , , . , , , , , . , , loch\ked by M $ / , .

, ( , ) Win API - , , , .

.

+2

:

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

, , PrinterSettings yuor , . , ?

+1

All Articles