打印预览对话框打印按钮不起作用

时间:2014-11-08 05:58:10

标签: c# winforms printing

我在打印时遇到一个小问题。当我尝试从打印预览对话框中打印文档时。它没有打印任何东西。如果我隐藏预览对话框并调用pdoc.Print();,它就会完美无缺。预览对话框按钮是否有任何特殊事件......?

        public void print()
    {
        PrintDialog pd = new PrintDialog();
        PrintDocument pdoc = new PrintDocument();
        int w=Convert.ToInt32(Length/2.54)*100;
        int h = Convert.ToInt32(Width / 2.54) * 100;           
       // pdoc.DefaultPageSettings.Landscape = true;
        PaperSize psize = new PaperSize("Custom", w, h);
        pdoc.DefaultPageSettings.PaperSize = psize;
        pd.Document = pdoc;            
        pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);
        DialogResult result = pd.ShowDialog();
        if (result == DialogResult.OK)
        {
            PrintPreviewDialog ppd = new PrintPreviewDialog();
            ppd.Document = pdoc;
            ppd.PrintPreviewControl.Zoom = 1.0;
            ((Form)ppd).WindowState = FormWindowState.Maximized;

            if (cid.Length > 1)// 
            {
                ppd.PrintPreviewControl.Zoom = 0.75;
                ppd.PrintPreviewControl.Columns = 2;
                ppd.PrintPreviewControl.Rows = 3;
            }

            DialogResult ppdResult = ppd.ShowDialog();

            //Newly Added Code
            if (ppdResult == DialogResult.OK)
            {
                MessageBox.Show("hai");//this code not executing
                 pdoc.Print();//this code not executing
            }
        }
    }

更新代码

   void pdoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        if (PrintCount>pc)
        {
            Graphics g = e.Graphics;
            if(CPayAgainstX>0 && CPayAgainstY>0)
                 positionsettings(g, "payagainst", CPayAgainstX, CPayAgainstY, CPayAgainstW, CPayAgainstF);
            if (ChqDateX > 0 && ChqDateY > 0)
                 positionsettings(g, "date", ChqDateX, ChqDateY, ChqDateW, ChqDateF);
            if (ChqAmtWrdsX > 0 && ChqAmtWrdsY > 0)
                 positionsettings(g, "amountword", ChqAmtWrdsX, ChqAmtWrdsY, ChqAmtWrdsW, ChqAmtWrdsF);
            if (ChqAmtDgtX > 0 && ChqAmtDgtY > 0)
                positionsettings(g, "amount", ChqAmtDgtX, ChqAmtDgtY, ChqAmtDgtW, ChqAmtDgtF);


            if(AccountPayeeX>0 && AccountPayeeY>0)
                positionsettings(g, "AccountPayee", AccountPayeeX, AccountPayeeY, AccountPayeeW, AccountPayeeF);

            if (TextField1X > 0 && TextField1Y > 0)
                positionsettings(g, "TextField1", TextField1X, TextField1Y, TextField1W, TextField1F);
            if (TextField2X > 0 && TextField2Y > 0)
                positionsettings(g, "TextField2", TextField2X, TextField2Y, TextField2W, TextField2F);
            if (TextField3X > 0 && TextField3Y > 0)
                positionsettings(g, "TextField3", TextField3X, TextField3Y, TextField3W, TextField3F);

            appendxml_transaction(cid[pc], bankname, templatename, dt[pc], payagainst, amt[pc]);
            pc += 1;
            if (PrintCount > pc)
            {
                e.HasMorePages = true;                   
            }               
        }
    }

1 个答案:

答案 0 :(得分:0)

通常使用Winforms对话框:

  • 首先创建对话框,
  • 然后显示它,收集结果
  • 最后根据结果行事

但是PrintPreviewDialog并没有这样的工作,即使许多人认为其他方面也是如此。 (我有一本通常可靠的书也错了..)

此处通过按“打印”开始打印。按钮/图标以及关闭对话框的任何方式都会返回cancel

当你想到它时,显而易见的目的:PrintPreviewDialog为用户提供了选择要打印的页面或页面的选择,因此在用户完成所有页面并关闭之前保持打开是有意义的它

那为什么你的代码不起作用?

当您按下'打印'按钮系统为每个页面调用PrintDocument的PrintPage事件。如果没有任何反应,你要么没有编写该事件,要么在那里出错。如果您对其进行了编码,请将其代码添加到您的问题中。我知道你必须创建它,因为你已经分配了它:

pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);

您可能还想为它设置断点!我有点惊讶,因为根据文档,pDoc.Print命令也只是调用PagePrint ..所以我想知道行为的任何差异可能来自哪里..