即使设置了值,打印范围也不起作用 [英] Print range not working even if values are set
问题描述
我设置了一个简单的打印解决方案,并且正常打印可以正常工作(测试了几次),但是当我使用PrintDialog指定自定义页面范围时,就好像该范围已过.调试时,我检查printDocument对象并确认范围值正确,但是打印机产生的最终产品与我给它的值相差不大.
I have a simple printing solution set up and normal printing works fine(tested it a couple of times), however when I use the PrintDialog to specify a custom page range, it is as if the range is ingored. When I debug I inspect the printDocument object and confirm that the range values are correct but the end product that the printer produces does not much the values I gave it.
这是我的代码:
printDialog.Document = printdoc;
printDialog.AllowSomePages = true;
if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
printdoc.PrinterSettings.FromPage = printDialog.PrinterSettings.FromPage;
printdoc.PrinterSettings.ToPage = printDialog.PrinterSettings.ToPage;
printdoc.PrinterSettings.PrintRange = printDialog.PrinterSettings.PrintRange;
printPreviewDialog.Document = printdoc;
printPreviewDialog.FindForm().WindowState = FormWindowState.Maximized;
printPreviewDialog.ShowDialog();
}
*注意-printdoc是System.Drawing.Printing.PrintDocument的实例.我在PrintDocument的PrintPage事件处理程序中添加了代码,以填充我正在打印的页面.
*Note - printdoc is a instance of System.Drawing.Printing.PrintDocument. I added code in the PrintDocument's PrintPage event handler to populate the page I'm printing.
推荐答案
您需要告诉打印对话框它应该接受用户输入的页面范围.为此,您可以指定 PrinterSettings.PrintRange
.
You need to tell the print dialog that it should accept user input for page ranges. To do this, you can specify the PrinterSettings.PrintRange
.
var printDialog = new PrintDialog();
printDialog.AllowSomePages = true; //May not be needed
printDialog.PrinterSettings.PrintRange = PrintRange.SomePages; //Needed
if(printDialog.ShowDialog() == DialogResult.OK)
{
// ... do the rest here
}
注意:您应该得到的主要建议是,需要设置PrintDialog.AllowSomePages = true
(以及From/ToPage),以便告诉对话框仅打印这些范围.另外,我不确定在对话框关闭后设置AllowSomePages
是否会生效,所以这就是为什么我将代码放在ShowDialog
之前的原因.您可以尝试在方便时在if语句中设置它.
Notes: The main takeaway you should get is that you need to set PrintDialog.AllowSomePages = true
(along with From/ToPage) in order to tell the dialog to only print those ranges. Also, I am not sure if setting the AllowSomePages
after the dialog closes will take effect, so that's why I put the code before ShowDialog
. You can try to set it inside the if-statement at your convenience.
这篇关于即使设置了值,打印范围也不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!