PrintPreview ok第一次,但第二次空白

时间:2014-01-20 03:46:06

标签: c# print-preview

我正在测试“打印预览”按钮,它工作正常。关闭PrintPreview窗口后,我再次单击“打印预览”按钮,这次显示空白页面!我无法弄清楚为什么会这样。搜索了这个网站和其他许多人,所以我发布了这个问题。任何想法都将不胜感激。

以下是相关代码:

    private void buttonPrintPreview_Click(object sender, EventArgs e)
    {
        page = 0;

        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }


    private void buttonPrint_Click(object sender, EventArgs e)
    {
        page = 0;

        printDialog1.Document = printDocument1;
        if (printDialog1.ShowDialog() == DialogResult.OK)
        {
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            printDocument1.DefaultPageSettings.Landscape = false;
            printDocument1.Print();
            //printDocument1.Dispose();
        }
    }



    private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
    {
        Graphics g = e.Graphics;

        //define margins
        float leftMargin = 70.0f; //good room for hole punches!
        float topMargin = 20.0f;
        float lineInc = 20.0f;
        //--------------------------------------------

        string eiNum = string.Empty;
        string eiDesc = string.Empty;
        string partNum = string.Empty;
        string partDesc = string.Empty;
        string price = string.Empty;
        string partType = string.Empty;
        string partQty = string.Empty;
        string thisEndItem = string.Empty;

        string textToPrint = string.Empty;

        Font printFontArial10 = new Font("Arial", 10, FontStyle.Regular);
        Font printFontArial10Bold = new Font("Arial", 10, FontStyle.Bold);
        Font printFontArial14 = new Font("Arial", 14, FontStyle.Bold);
        Font printFontCour8 = new Font("Courier New", 8, FontStyle.Regular);

        if (page == 0 && counter == 0)
        {
            // draw image/logo
            Image Logo = Image.FromFile(Settings.Default.LogoPath);
            g.DrawImage(Logo, leftMargin, 35f);

            // draw title
            textToPrint = "TIW Purchasing - Master Buy List";
            e.Graphics.DrawString(textToPrint, printFontArial14, Brushes.Black, leftMargin + 160f, 54f);

            // date
            DateTime thisDay = DateTime.Today;
            textToPrint = thisDay.ToString("d");
            e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 280f, 76f);

            eiNum = listView1.Items[0].Text;
            eiDesc = listView1.Items[0].SubItems[1].Text;

            if (eiDesc.Length > 80)
                eiDesc = eiDesc.Substring(0, 80) + "...";

            textToPrint = eiNum + " - " + eiDesc;
            e.Graphics.DrawString(textToPrint, printFontArial10Bold, Brushes.Black, leftMargin, topMargin + 90);
        }
        else if (page > 0)
        {
            double remainder = counter % amtperpage;
            if (remainder == 0) //---means we're at the top of the page
            {
                //title & version
                textToPrint = "TIW Purchasing - Master Buy List";
                e.Graphics.DrawString(textToPrint, printFontArial10Bold, Brushes.Black, leftMargin + 120, 54f);
                textToPrint = "(continued from page " + page + ")";
                e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 400, 54f);
            }
        }

        if (page == 0)
            lineInc = 90;
        else
            lineInc = 78;

        int stop = counter + amtperpage;

        if (stop > listView1.Items.Count)
            stop = listView1.Items.Count;

        while (counter < stop)
        {
            thisEndItem = listView1.Items[counter].SubItems[0].Text;

            partNum = listView1.Items[counter].SubItems[2].Text;
            partDesc = listView1.Items[counter].SubItems[3].Text;
            price = listView1.Items[counter].SubItems[4].Text;
            partType = listView1.Items[counter].SubItems[5].Text;
            partQty = listView1.Items[counter].SubItems[6].Text;

            if (thisEndItem == eiNum) //---still working on the same end item
            {
                lineInc += 12;

                textToPrint = partNum;
                e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 10, topMargin + lineInc);

                if (partDesc.Length > 70)
                    partDesc = partDesc.Substring(0, 70) + "...";
                textToPrint = partDesc;
                e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 70, topMargin + lineInc);

                textToPrint = price;
                e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 600, topMargin + lineInc);

                textToPrint = partType;
                e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 630, topMargin + lineInc);

                textToPrint = partQty;
                e.Graphics.DrawString(textToPrint, printFontArial10, Brushes.Black, leftMargin + 670, topMargin + lineInc);
            }
            else //---starting a new end item
            {
                lineInc += 16;

                eiNum = listView1.Items[counter].Text;
                eiDesc = listView1.Items[counter].SubItems[1].Text;

                if (eiDesc.Length > 80)
                    eiDesc = eiDesc.Substring(0, 80) + "...";

                textToPrint = eiNum + " - " + eiDesc;
                e.Graphics.DrawString(textToPrint, printFontArial10Bold, Brushes.Black, leftMargin, topMargin + lineInc);
            }

            counter++;
        }

        //---footer-------------------------------
        DateTime dateTime = DateTime.Now;
        textToPrint = "eView " + EViewMethods.eviewVersion + "                       " + Environment.UserName + "                     " + String.Format("{0:F}", dateTime);
        e.Graphics.DrawString(textToPrint, printFontCour8, Brushes.Black, leftMargin, 1060f);

        printpagenum = page + 1;
        textToPrint = printpagenum.ToString();
        e.Graphics.DrawString(textToPrint, printFontArial10Bold, Brushes.Black, leftMargin + 740, 1060f);
        //----------------------------------------

        page++;
        e.HasMorePages = counter < listView1.Items.Count;
    }

以下是更正后的代码:

    private void buttonPrintPreview_Click(object sender, EventArgs e)
    {
        page = 0;
        counter = 0;

        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.ShowDialog();
    }


    private void buttonPrint_Click(object sender, EventArgs e)
    {
        page = 0;
        counter = 0;

        printDialog1.Document = printDocument1;
        if (printDialog1.ShowDialog() == DialogResult.OK)
        {
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            printDocument1.DefaultPageSettings.Landscape = false;
            printDocument1.Print();
            //printDocument1.Dispose();
        }
    }

1 个答案:

答案 0 :(得分:0)

public Form1()
{
    InitializeComponent();
    printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}