Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示PrintDialog后修改PrinterSettings_C#_.net_Winforms_Printing_Printer Properties - Fatal编程技术网

C# 显示PrintDialog后修改PrinterSettings

C# 显示PrintDialog后修改PrinterSettings,c#,.net,winforms,printing,printer-properties,C#,.net,Winforms,Printing,Printer Properties,我正在尝试修改System.Drawing.Printing.PrinterSettings对象,该对象是在向用户显示对话框后从System.Windows.Forms.PrintDialog获得的。虽然我可以更改PrinterSettings对象的属性值,但在打印文档时,我在显示对话框后所做的任何更改实际上都不会被考虑 以下是我的意思的一个例子: //Show the printdialog and retreive the printersettings var printDialo

我正在尝试修改System.Drawing.Printing.PrinterSettings对象,该对象是在向用户显示对话框后从System.Windows.Forms.PrintDialog获得的。虽然我可以更改PrinterSettings对象的属性值,但在打印文档时,我在显示对话框后所做的任何更改实际上都不会被考虑

以下是我的意思的一个例子:

//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;
现在使用printerSettings对象进行打印。我使用第三方dll Aspose.Words,因为我需要打印Word,但这似乎不是问题所在。似乎在显示对话框后,所有设置都已提交给打印机,而更改打印机设置则一事无成。你有什么办法让它发挥作用吗


编辑:我有一些解决办法。我想在这里得到这些特定问题的答案:在显示对话框后是否可以更改PrinterSettings对象,以及在打印时是否考虑这些更改。如果有人只知道一种工作方式(您可以决定要使用什么API进行打印,只要使用PrinterSettings对象就无所谓),我将非常感谢。

来自Aspose文档:

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

因此,您似乎可以将YOUR修改后的PrinterSettings对象传递给您尝试打印的word文档。你能告诉我这是否有效吗?

不知道你的问题为什么会被否决,我觉得这很合理

无论如何,我注意到了一些关于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();
            }
。 .

这里还有一个非常有趣的相互作用(请记住您设置了PrinterSettings.ToPage):

然后

public PrintDocument Document {
            get { return printDocument;}
            set {
                printDocument = value; 
                **if (printDocument == null)
                    settings = new PrinterSettings();** 
                else 
                    settings = printDocument.PrinterSettings;
            } 
        }
我知道这不是一个直接的答案,但我认为应该为你指出为什么它不起作用的正确方向。在我看来,在对话的使用过程中,它可以愉快地取消更改设置,因为它将在完成时重新创建,但当对话完成时,更改设置实际上会使文档打印设置无效,直到再次设置为止。可以手动执行此操作,也可以通过许多内部构件通常的内部/私有方式将其锁定在M$

当然,有一个选择(我知道不是很好),就是在调用代码可以从上面的拨号中提取出来之后直接使用WinAPI,如果需要的话,可以构建自己的包装器


祝你好运

在打开对话框之前不更改设置的原因是什么?是的,在我的应用程序中,我有“合并文档”,由许多不同的文档组成。在打印对话框中,它们应该显示为一个文档,但对于打印,我需要单独处理它们。所以更准确地说:一些PrinterSettings应该适用于所有文档,其他我需要为每个文档单独设置。我可以在没有用户交互的情况下计算各个属性/设置,但对于打印,我需要将这些属性/设置注入打印机设置。从片段中完全不清楚printerSettings对象是如何应用于实际打印作业的。代码中缺少这一点也可以解释问题的原因。不,不是这样。请阅读我的帖子,不仅仅是代码。我使用Aspose进行打印,但根本的问题与Apose无关。嗨,Steve,结果是一样的。仅考虑打印对话框中的设置。不是手动更改。我现在采用另一种方法,将所有文档转换为xps(使用Aspose),将它们合并为一个xps,然后打印这一个xps(或所选页面)通过使用wpf打印对话框。我已经有一段时间没有检查这个了,因此我的重播有点晚了:非常感谢。顺便说一句,我认为这个问题得到了否决票,因为这个网站上的一些人倾向于不考虑问题的真实问题和内容。人们不愿意承认作者不仅仅是一个肤浅的编程思想。关于这个问题,最简单的方法是认为我在代码的其他部分犯了一些错误,而这个问题实际上并不存在。对不起,我没有足够的学分或分数或其他任何东西来支持你的答案。
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;
            } 
        }